Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | #ifndef jit_IonAllocPolicy_h |
michael@0 | 8 | #define jit_IonAllocPolicy_h |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/GuardObjects.h" |
michael@0 | 11 | #include "mozilla/TypeTraits.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "jscntxt.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "ds/LifoAlloc.h" |
michael@0 | 16 | #include "jit/InlineList.h" |
michael@0 | 17 | #include "jit/Ion.h" |
michael@0 | 18 | |
michael@0 | 19 | namespace js { |
michael@0 | 20 | namespace jit { |
michael@0 | 21 | |
michael@0 | 22 | class TempAllocator |
michael@0 | 23 | { |
michael@0 | 24 | LifoAllocScope lifoScope_; |
michael@0 | 25 | |
michael@0 | 26 | // Linked list of GCThings rooted by this allocator. |
michael@0 | 27 | CompilerRootNode *rootList_; |
michael@0 | 28 | |
michael@0 | 29 | public: |
michael@0 | 30 | TempAllocator(LifoAlloc *lifoAlloc) |
michael@0 | 31 | : lifoScope_(lifoAlloc), |
michael@0 | 32 | rootList_(nullptr) |
michael@0 | 33 | { } |
michael@0 | 34 | |
michael@0 | 35 | void *allocateOrCrash(size_t bytes) |
michael@0 | 36 | { |
michael@0 | 37 | void *p = lifoScope_.alloc().alloc(bytes); |
michael@0 | 38 | if (!p) |
michael@0 | 39 | js::CrashAtUnhandlableOOM("LifoAlloc::allocOrCrash"); |
michael@0 | 40 | return p; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | void *allocate(size_t bytes) |
michael@0 | 44 | { |
michael@0 | 45 | void *p = lifoScope_.alloc().alloc(bytes); |
michael@0 | 46 | if (!ensureBallast()) |
michael@0 | 47 | return nullptr; |
michael@0 | 48 | return p; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | template <size_t ElemSize> |
michael@0 | 52 | void *allocateArray(size_t n) |
michael@0 | 53 | { |
michael@0 | 54 | if (n & mozilla::tl::MulOverflowMask<ElemSize>::value) |
michael@0 | 55 | return nullptr; |
michael@0 | 56 | void *p = lifoScope_.alloc().alloc(n * ElemSize); |
michael@0 | 57 | if (!ensureBallast()) |
michael@0 | 58 | return nullptr; |
michael@0 | 59 | return p; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | LifoAlloc *lifoAlloc() |
michael@0 | 63 | { |
michael@0 | 64 | return &lifoScope_.alloc(); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | CompilerRootNode *&rootList() |
michael@0 | 68 | { |
michael@0 | 69 | return rootList_; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | bool ensureBallast() { |
michael@0 | 73 | // Most infallible Ion allocations are small, so we use a ballast of |
michael@0 | 74 | // ~16K for now. |
michael@0 | 75 | return lifoScope_.alloc().ensureUnusedApproximate(16 * 1024); |
michael@0 | 76 | } |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | // Stack allocated rooter for all roots associated with a TempAllocator |
michael@0 | 80 | class AutoTempAllocatorRooter : private AutoGCRooter |
michael@0 | 81 | { |
michael@0 | 82 | public: |
michael@0 | 83 | explicit AutoTempAllocatorRooter(JSContext *cx, TempAllocator *temp |
michael@0 | 84 | MOZ_GUARD_OBJECT_NOTIFIER_PARAM) |
michael@0 | 85 | : AutoGCRooter(cx, IONALLOC), temp(temp) |
michael@0 | 86 | { |
michael@0 | 87 | MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | friend void AutoGCRooter::trace(JSTracer *trc); |
michael@0 | 91 | void trace(JSTracer *trc); |
michael@0 | 92 | |
michael@0 | 93 | private: |
michael@0 | 94 | TempAllocator *temp; |
michael@0 | 95 | MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER |
michael@0 | 96 | }; |
michael@0 | 97 | |
michael@0 | 98 | class IonAllocPolicy |
michael@0 | 99 | { |
michael@0 | 100 | TempAllocator &alloc_; |
michael@0 | 101 | |
michael@0 | 102 | public: |
michael@0 | 103 | IonAllocPolicy(TempAllocator &alloc) |
michael@0 | 104 | : alloc_(alloc) |
michael@0 | 105 | {} |
michael@0 | 106 | void *malloc_(size_t bytes) { |
michael@0 | 107 | return alloc_.allocate(bytes); |
michael@0 | 108 | } |
michael@0 | 109 | void *calloc_(size_t bytes) { |
michael@0 | 110 | void *p = alloc_.allocate(bytes); |
michael@0 | 111 | if (p) |
michael@0 | 112 | memset(p, 0, bytes); |
michael@0 | 113 | return p; |
michael@0 | 114 | } |
michael@0 | 115 | void *realloc_(void *p, size_t oldBytes, size_t bytes) { |
michael@0 | 116 | void *n = malloc_(bytes); |
michael@0 | 117 | if (!n) |
michael@0 | 118 | return n; |
michael@0 | 119 | memcpy(n, p, Min(oldBytes, bytes)); |
michael@0 | 120 | return n; |
michael@0 | 121 | } |
michael@0 | 122 | void free_(void *p) { |
michael@0 | 123 | } |
michael@0 | 124 | void reportAllocOverflow() const { |
michael@0 | 125 | } |
michael@0 | 126 | }; |
michael@0 | 127 | |
michael@0 | 128 | // Deprecated. Don't use this. Will be removed after everything has been |
michael@0 | 129 | // converted to IonAllocPolicy. |
michael@0 | 130 | class OldIonAllocPolicy |
michael@0 | 131 | { |
michael@0 | 132 | public: |
michael@0 | 133 | OldIonAllocPolicy() |
michael@0 | 134 | {} |
michael@0 | 135 | void *malloc_(size_t bytes) { |
michael@0 | 136 | return GetIonContext()->temp->allocate(bytes); |
michael@0 | 137 | } |
michael@0 | 138 | void *calloc_(size_t bytes) { |
michael@0 | 139 | void *p = GetIonContext()->temp->allocate(bytes); |
michael@0 | 140 | if (p) |
michael@0 | 141 | memset(p, 0, bytes); |
michael@0 | 142 | return p; |
michael@0 | 143 | } |
michael@0 | 144 | void *realloc_(void *p, size_t oldBytes, size_t bytes) { |
michael@0 | 145 | void *n = malloc_(bytes); |
michael@0 | 146 | if (!n) |
michael@0 | 147 | return n; |
michael@0 | 148 | memcpy(n, p, Min(oldBytes, bytes)); |
michael@0 | 149 | return n; |
michael@0 | 150 | } |
michael@0 | 151 | void free_(void *p) { |
michael@0 | 152 | } |
michael@0 | 153 | void reportAllocOverflow() const { |
michael@0 | 154 | } |
michael@0 | 155 | }; |
michael@0 | 156 | |
michael@0 | 157 | class AutoIonContextAlloc |
michael@0 | 158 | { |
michael@0 | 159 | TempAllocator tempAlloc_; |
michael@0 | 160 | IonContext *icx_; |
michael@0 | 161 | TempAllocator *prevAlloc_; |
michael@0 | 162 | |
michael@0 | 163 | public: |
michael@0 | 164 | explicit AutoIonContextAlloc(JSContext *cx) |
michael@0 | 165 | : tempAlloc_(&cx->tempLifoAlloc()), |
michael@0 | 166 | icx_(GetIonContext()), |
michael@0 | 167 | prevAlloc_(icx_->temp) |
michael@0 | 168 | { |
michael@0 | 169 | icx_->temp = &tempAlloc_; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | ~AutoIonContextAlloc() { |
michael@0 | 173 | JS_ASSERT(icx_->temp == &tempAlloc_); |
michael@0 | 174 | icx_->temp = prevAlloc_; |
michael@0 | 175 | } |
michael@0 | 176 | }; |
michael@0 | 177 | |
michael@0 | 178 | struct TempObject |
michael@0 | 179 | { |
michael@0 | 180 | inline void *operator new(size_t nbytes, TempAllocator &alloc) { |
michael@0 | 181 | return alloc.allocateOrCrash(nbytes); |
michael@0 | 182 | } |
michael@0 | 183 | template <class T> |
michael@0 | 184 | inline void *operator new(size_t nbytes, T *pos) { |
michael@0 | 185 | static_assert(mozilla::IsConvertible<T*, TempObject*>::value, |
michael@0 | 186 | "Placement new argument type must inherit from TempObject"); |
michael@0 | 187 | return pos; |
michael@0 | 188 | } |
michael@0 | 189 | }; |
michael@0 | 190 | |
michael@0 | 191 | template <typename T> |
michael@0 | 192 | class TempObjectPool |
michael@0 | 193 | { |
michael@0 | 194 | TempAllocator *alloc_; |
michael@0 | 195 | InlineForwardList<T> freed_; |
michael@0 | 196 | |
michael@0 | 197 | public: |
michael@0 | 198 | TempObjectPool() |
michael@0 | 199 | : alloc_(nullptr) |
michael@0 | 200 | {} |
michael@0 | 201 | void setAllocator(TempAllocator &alloc) { |
michael@0 | 202 | JS_ASSERT(freed_.empty()); |
michael@0 | 203 | alloc_ = &alloc; |
michael@0 | 204 | } |
michael@0 | 205 | T *allocate() { |
michael@0 | 206 | JS_ASSERT(alloc_); |
michael@0 | 207 | if (freed_.empty()) |
michael@0 | 208 | return new(*alloc_) T(); |
michael@0 | 209 | return freed_.popFront(); |
michael@0 | 210 | } |
michael@0 | 211 | void free(T *obj) { |
michael@0 | 212 | freed_.pushFront(obj); |
michael@0 | 213 | } |
michael@0 | 214 | void clear() { |
michael@0 | 215 | freed_.clear(); |
michael@0 | 216 | } |
michael@0 | 217 | }; |
michael@0 | 218 | |
michael@0 | 219 | } // namespace jit |
michael@0 | 220 | } // namespace js |
michael@0 | 221 | |
michael@0 | 222 | #endif /* jit_IonAllocPolicy_h */ |