From 900b1f60596c93f7cf02817e51da669555f391f2 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Tue, 14 Jun 2011 19:21:47 -0700 Subject: [PATCH] Bug 661567: Implement RuntimeAllocPolicy, providing proper memory accounting in GC'd objects that live longer than a JSContext. r=luke --- js/src/jscntxt.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/js/src/jscntxt.h b/js/src/jscntxt.h index 8067aedf2ad..285fc0430dd 100644 --- a/js/src/jscntxt.h +++ b/js/src/jscntxt.h @@ -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