Bug 661567: Implement RuntimeAllocPolicy, providing proper memory accounting in GC'd objects that live longer than a JSContext. r=luke

This commit is contained in:
Jim Blandy 2011-06-14 19:21:47 -07:00
parent fe837eb8a0
commit 900b1f6059

View File

@ -1430,6 +1430,28 @@ struct JSContext
namespace js {
/*
* Allocation policy that uses JSRuntime::malloc_ and friends, so that
* memory pressure is properly accounted for. This is suitable for
* long-lived objects owned by the JSRuntime.
*
* Since it doesn't hold a JSContext (those may not live long enough), it
* can't report out-of-memory conditions itself; the caller must check for
* OOM and take the appropriate action.
*/
class RuntimeAllocPolicy
{
JSRuntime *const runtime;
public:
RuntimeAllocPolicy(JSRuntime *rt) : runtime(rt) {}
RuntimeAllocPolicy(JSContext *cx) : runtime(cx->runtime) {}
void *malloc_(size_t bytes) { return runtime->malloc_(bytes); }
void *realloc_(void *p, size_t bytes) { return runtime->realloc_(p, bytes); }
void free_(void *p) { runtime->free_(p); }
void reportAllocOverflow() const {}
};
#ifdef JS_THREADSAFE
# define JS_THREAD_ID(cx) ((cx)->thread() ? (cx)->thread()->id : 0)
#endif