1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsalloc.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* 1.11 + * JS allocation policies. 1.12 + * 1.13 + * The allocators here are for system memory with lifetimes which are not 1.14 + * managed by the GC. See the comment at the top of vm/MallocProvider.h. 1.15 + */ 1.16 + 1.17 +#ifndef jsalloc_h 1.18 +#define jsalloc_h 1.19 + 1.20 +#include "js/TypeDecls.h" 1.21 +#include "js/Utility.h" 1.22 + 1.23 +namespace js { 1.24 + 1.25 +class ContextFriendFields; 1.26 + 1.27 +/* Policy for using system memory functions and doing no error reporting. */ 1.28 +class SystemAllocPolicy 1.29 +{ 1.30 + public: 1.31 + void *malloc_(size_t bytes) { return js_malloc(bytes); } 1.32 + void *calloc_(size_t bytes) { return js_calloc(bytes); } 1.33 + void *realloc_(void *p, size_t oldBytes, size_t bytes) { return js_realloc(p, bytes); } 1.34 + void free_(void *p) { js_free(p); } 1.35 + void reportAllocOverflow() const {} 1.36 +}; 1.37 + 1.38 +/* 1.39 + * Allocation policy that calls the system memory functions and reports errors 1.40 + * to the context. Since the JSContext given on construction is stored for 1.41 + * the lifetime of the container, this policy may only be used for containers 1.42 + * whose lifetime is a shorter than the given JSContext. 1.43 + * 1.44 + * FIXME bug 647103 - rewrite this in terms of temporary allocation functions, 1.45 + * not the system ones. 1.46 + */ 1.47 +class TempAllocPolicy 1.48 +{ 1.49 + ContextFriendFields *const cx_; 1.50 + 1.51 + /* 1.52 + * Non-inline helper to call JSRuntime::onOutOfMemory with minimal 1.53 + * code bloat. 1.54 + */ 1.55 + JS_FRIEND_API(void *) onOutOfMemory(void *p, size_t nbytes); 1.56 + 1.57 + public: 1.58 + TempAllocPolicy(JSContext *cx) : cx_((ContextFriendFields *) cx) {} // :( 1.59 + TempAllocPolicy(ContextFriendFields *cx) : cx_(cx) {} 1.60 + 1.61 + void *malloc_(size_t bytes) { 1.62 + void *p = js_malloc(bytes); 1.63 + if (MOZ_UNLIKELY(!p)) 1.64 + p = onOutOfMemory(nullptr, bytes); 1.65 + return p; 1.66 + } 1.67 + 1.68 + void *calloc_(size_t bytes) { 1.69 + void *p = js_calloc(bytes); 1.70 + if (MOZ_UNLIKELY(!p)) 1.71 + p = onOutOfMemory(nullptr, bytes); 1.72 + return p; 1.73 + } 1.74 + 1.75 + void *realloc_(void *p, size_t oldBytes, size_t bytes) { 1.76 + void *p2 = js_realloc(p, bytes); 1.77 + if (MOZ_UNLIKELY(!p2)) 1.78 + p2 = onOutOfMemory(p2, bytes); 1.79 + return p2; 1.80 + } 1.81 + 1.82 + void free_(void *p) { 1.83 + js_free(p); 1.84 + } 1.85 + 1.86 + JS_FRIEND_API(void) reportAllocOverflow() const; 1.87 +}; 1.88 + 1.89 +} /* namespace js */ 1.90 + 1.91 +#endif /* jsalloc_h */