michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * JS allocation policies. michael@0: * michael@0: * The allocators here are for system memory with lifetimes which are not michael@0: * managed by the GC. See the comment at the top of vm/MallocProvider.h. michael@0: */ michael@0: michael@0: #ifndef jsalloc_h michael@0: #define jsalloc_h michael@0: michael@0: #include "js/TypeDecls.h" michael@0: #include "js/Utility.h" michael@0: michael@0: namespace js { michael@0: michael@0: class ContextFriendFields; michael@0: michael@0: /* Policy for using system memory functions and doing no error reporting. */ michael@0: class SystemAllocPolicy michael@0: { michael@0: public: michael@0: void *malloc_(size_t bytes) { return js_malloc(bytes); } michael@0: void *calloc_(size_t bytes) { return js_calloc(bytes); } michael@0: void *realloc_(void *p, size_t oldBytes, size_t bytes) { return js_realloc(p, bytes); } michael@0: void free_(void *p) { js_free(p); } michael@0: void reportAllocOverflow() const {} michael@0: }; michael@0: michael@0: /* michael@0: * Allocation policy that calls the system memory functions and reports errors michael@0: * to the context. Since the JSContext given on construction is stored for michael@0: * the lifetime of the container, this policy may only be used for containers michael@0: * whose lifetime is a shorter than the given JSContext. michael@0: * michael@0: * FIXME bug 647103 - rewrite this in terms of temporary allocation functions, michael@0: * not the system ones. michael@0: */ michael@0: class TempAllocPolicy michael@0: { michael@0: ContextFriendFields *const cx_; michael@0: michael@0: /* michael@0: * Non-inline helper to call JSRuntime::onOutOfMemory with minimal michael@0: * code bloat. michael@0: */ michael@0: JS_FRIEND_API(void *) onOutOfMemory(void *p, size_t nbytes); michael@0: michael@0: public: michael@0: TempAllocPolicy(JSContext *cx) : cx_((ContextFriendFields *) cx) {} // :( michael@0: TempAllocPolicy(ContextFriendFields *cx) : cx_(cx) {} michael@0: michael@0: void *malloc_(size_t bytes) { michael@0: void *p = js_malloc(bytes); michael@0: if (MOZ_UNLIKELY(!p)) michael@0: p = onOutOfMemory(nullptr, bytes); michael@0: return p; michael@0: } michael@0: michael@0: void *calloc_(size_t bytes) { michael@0: void *p = js_calloc(bytes); michael@0: if (MOZ_UNLIKELY(!p)) michael@0: p = onOutOfMemory(nullptr, bytes); michael@0: return p; michael@0: } michael@0: michael@0: void *realloc_(void *p, size_t oldBytes, size_t bytes) { michael@0: void *p2 = js_realloc(p, bytes); michael@0: if (MOZ_UNLIKELY(!p2)) michael@0: p2 = onOutOfMemory(p2, bytes); michael@0: return p2; michael@0: } michael@0: michael@0: void free_(void *p) { michael@0: js_free(p); michael@0: } michael@0: michael@0: JS_FRIEND_API(void) reportAllocOverflow() const; michael@0: }; michael@0: michael@0: } /* namespace js */ michael@0: michael@0: #endif /* jsalloc_h */