Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* |
michael@0 | 8 | * JS allocation policies. |
michael@0 | 9 | * |
michael@0 | 10 | * The allocators here are for system memory with lifetimes which are not |
michael@0 | 11 | * managed by the GC. See the comment at the top of vm/MallocProvider.h. |
michael@0 | 12 | */ |
michael@0 | 13 | |
michael@0 | 14 | #ifndef jsalloc_h |
michael@0 | 15 | #define jsalloc_h |
michael@0 | 16 | |
michael@0 | 17 | #include "js/TypeDecls.h" |
michael@0 | 18 | #include "js/Utility.h" |
michael@0 | 19 | |
michael@0 | 20 | namespace js { |
michael@0 | 21 | |
michael@0 | 22 | class ContextFriendFields; |
michael@0 | 23 | |
michael@0 | 24 | /* Policy for using system memory functions and doing no error reporting. */ |
michael@0 | 25 | class SystemAllocPolicy |
michael@0 | 26 | { |
michael@0 | 27 | public: |
michael@0 | 28 | void *malloc_(size_t bytes) { return js_malloc(bytes); } |
michael@0 | 29 | void *calloc_(size_t bytes) { return js_calloc(bytes); } |
michael@0 | 30 | void *realloc_(void *p, size_t oldBytes, size_t bytes) { return js_realloc(p, bytes); } |
michael@0 | 31 | void free_(void *p) { js_free(p); } |
michael@0 | 32 | void reportAllocOverflow() const {} |
michael@0 | 33 | }; |
michael@0 | 34 | |
michael@0 | 35 | /* |
michael@0 | 36 | * Allocation policy that calls the system memory functions and reports errors |
michael@0 | 37 | * to the context. Since the JSContext given on construction is stored for |
michael@0 | 38 | * the lifetime of the container, this policy may only be used for containers |
michael@0 | 39 | * whose lifetime is a shorter than the given JSContext. |
michael@0 | 40 | * |
michael@0 | 41 | * FIXME bug 647103 - rewrite this in terms of temporary allocation functions, |
michael@0 | 42 | * not the system ones. |
michael@0 | 43 | */ |
michael@0 | 44 | class TempAllocPolicy |
michael@0 | 45 | { |
michael@0 | 46 | ContextFriendFields *const cx_; |
michael@0 | 47 | |
michael@0 | 48 | /* |
michael@0 | 49 | * Non-inline helper to call JSRuntime::onOutOfMemory with minimal |
michael@0 | 50 | * code bloat. |
michael@0 | 51 | */ |
michael@0 | 52 | JS_FRIEND_API(void *) onOutOfMemory(void *p, size_t nbytes); |
michael@0 | 53 | |
michael@0 | 54 | public: |
michael@0 | 55 | TempAllocPolicy(JSContext *cx) : cx_((ContextFriendFields *) cx) {} // :( |
michael@0 | 56 | TempAllocPolicy(ContextFriendFields *cx) : cx_(cx) {} |
michael@0 | 57 | |
michael@0 | 58 | void *malloc_(size_t bytes) { |
michael@0 | 59 | void *p = js_malloc(bytes); |
michael@0 | 60 | if (MOZ_UNLIKELY(!p)) |
michael@0 | 61 | p = onOutOfMemory(nullptr, bytes); |
michael@0 | 62 | return p; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | void *calloc_(size_t bytes) { |
michael@0 | 66 | void *p = js_calloc(bytes); |
michael@0 | 67 | if (MOZ_UNLIKELY(!p)) |
michael@0 | 68 | p = onOutOfMemory(nullptr, bytes); |
michael@0 | 69 | return p; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | void *realloc_(void *p, size_t oldBytes, size_t bytes) { |
michael@0 | 73 | void *p2 = js_realloc(p, bytes); |
michael@0 | 74 | if (MOZ_UNLIKELY(!p2)) |
michael@0 | 75 | p2 = onOutOfMemory(p2, bytes); |
michael@0 | 76 | return p2; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | void free_(void *p) { |
michael@0 | 80 | js_free(p); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | JS_FRIEND_API(void) reportAllocOverflow() const; |
michael@0 | 84 | }; |
michael@0 | 85 | |
michael@0 | 86 | } /* namespace js */ |
michael@0 | 87 | |
michael@0 | 88 | #endif /* jsalloc_h */ |