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 | #include "ds/LifoAlloc.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/MathAlgorithms.h" |
michael@0 | 10 | |
michael@0 | 11 | using namespace js; |
michael@0 | 12 | |
michael@0 | 13 | using mozilla::RoundUpPow2; |
michael@0 | 14 | using mozilla::tl::BitSize; |
michael@0 | 15 | |
michael@0 | 16 | namespace js { |
michael@0 | 17 | namespace detail { |
michael@0 | 18 | |
michael@0 | 19 | BumpChunk * |
michael@0 | 20 | BumpChunk::new_(size_t chunkSize) |
michael@0 | 21 | { |
michael@0 | 22 | JS_ASSERT(RoundUpPow2(chunkSize) == chunkSize); |
michael@0 | 23 | void *mem = js_malloc(chunkSize); |
michael@0 | 24 | if (!mem) |
michael@0 | 25 | return nullptr; |
michael@0 | 26 | BumpChunk *result = new (mem) BumpChunk(chunkSize - sizeof(BumpChunk)); |
michael@0 | 27 | |
michael@0 | 28 | // We assume that the alignment of sAlign is less than that of |
michael@0 | 29 | // the underlying memory allocator -- creating a new BumpChunk should |
michael@0 | 30 | // always satisfy the sAlign alignment constraint. |
michael@0 | 31 | JS_ASSERT(AlignPtr(result->bump) == result->bump); |
michael@0 | 32 | return result; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | void |
michael@0 | 36 | BumpChunk::delete_(BumpChunk *chunk) |
michael@0 | 37 | { |
michael@0 | 38 | #ifdef DEBUG |
michael@0 | 39 | // Part of the chunk may have been marked as poisoned/noaccess. Undo that |
michael@0 | 40 | // before writing the 0xcd bytes. |
michael@0 | 41 | size_t size = sizeof(*chunk) + chunk->bumpSpaceSize; |
michael@0 | 42 | MOZ_MAKE_MEM_UNDEFINED(chunk, size); |
michael@0 | 43 | memset(chunk, 0xcd, size); |
michael@0 | 44 | #endif |
michael@0 | 45 | js_free(chunk); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | bool |
michael@0 | 49 | BumpChunk::canAlloc(size_t n) |
michael@0 | 50 | { |
michael@0 | 51 | char *aligned = AlignPtr(bump); |
michael@0 | 52 | char *bumped = aligned + n; |
michael@0 | 53 | return bumped <= limit && bumped > headerBase(); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | } // namespace detail |
michael@0 | 57 | } // namespace js |
michael@0 | 58 | |
michael@0 | 59 | void |
michael@0 | 60 | LifoAlloc::freeAll() |
michael@0 | 61 | { |
michael@0 | 62 | while (first) { |
michael@0 | 63 | BumpChunk *victim = first; |
michael@0 | 64 | first = first->next(); |
michael@0 | 65 | decrementCurSize(victim->computedSizeOfIncludingThis()); |
michael@0 | 66 | BumpChunk::delete_(victim); |
michael@0 | 67 | } |
michael@0 | 68 | first = latest = last = nullptr; |
michael@0 | 69 | |
michael@0 | 70 | // Nb: maintaining curSize_ correctly isn't easy. Fortunately, this is an |
michael@0 | 71 | // excellent sanity check. |
michael@0 | 72 | JS_ASSERT(curSize_ == 0); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | LifoAlloc::BumpChunk * |
michael@0 | 76 | LifoAlloc::getOrCreateChunk(size_t n) |
michael@0 | 77 | { |
michael@0 | 78 | if (first) { |
michael@0 | 79 | // Look for existing, unused BumpChunks to satisfy the request. |
michael@0 | 80 | while (latest->next()) { |
michael@0 | 81 | latest = latest->next(); |
michael@0 | 82 | latest->resetBump(); // This was an unused BumpChunk on the chain. |
michael@0 | 83 | if (latest->canAlloc(n)) |
michael@0 | 84 | return latest; |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | size_t defaultChunkFreeSpace = defaultChunkSize_ - sizeof(BumpChunk); |
michael@0 | 89 | size_t chunkSize; |
michael@0 | 90 | if (n > defaultChunkFreeSpace) { |
michael@0 | 91 | size_t allocSizeWithHeader = n + sizeof(BumpChunk); |
michael@0 | 92 | |
michael@0 | 93 | // Guard for overflow. |
michael@0 | 94 | if (allocSizeWithHeader < n || |
michael@0 | 95 | (allocSizeWithHeader & (size_t(1) << (BitSize<size_t>::value - 1)))) { |
michael@0 | 96 | return nullptr; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | chunkSize = RoundUpPow2(allocSizeWithHeader); |
michael@0 | 100 | } else { |
michael@0 | 101 | chunkSize = defaultChunkSize_; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | // If we get here, we couldn't find an existing BumpChunk to fill the request. |
michael@0 | 105 | BumpChunk *newChunk = BumpChunk::new_(chunkSize); |
michael@0 | 106 | if (!newChunk) |
michael@0 | 107 | return nullptr; |
michael@0 | 108 | if (!first) { |
michael@0 | 109 | latest = first = last = newChunk; |
michael@0 | 110 | } else { |
michael@0 | 111 | JS_ASSERT(latest && !latest->next()); |
michael@0 | 112 | latest->setNext(newChunk); |
michael@0 | 113 | latest = last = newChunk; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | size_t computedChunkSize = newChunk->computedSizeOfIncludingThis(); |
michael@0 | 117 | JS_ASSERT(computedChunkSize == chunkSize); |
michael@0 | 118 | incrementCurSize(computedChunkSize); |
michael@0 | 119 | |
michael@0 | 120 | return newChunk; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | void |
michael@0 | 124 | LifoAlloc::transferFrom(LifoAlloc *other) |
michael@0 | 125 | { |
michael@0 | 126 | JS_ASSERT(!markCount); |
michael@0 | 127 | JS_ASSERT(!other->markCount); |
michael@0 | 128 | |
michael@0 | 129 | if (!other->first) |
michael@0 | 130 | return; |
michael@0 | 131 | |
michael@0 | 132 | incrementCurSize(other->curSize_); |
michael@0 | 133 | if (other->isEmpty()) |
michael@0 | 134 | appendUnused(other->first, other->last); |
michael@0 | 135 | else |
michael@0 | 136 | appendUsed(other->first, other->latest, other->last); |
michael@0 | 137 | other->first = other->last = other->latest = nullptr; |
michael@0 | 138 | other->curSize_ = 0; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | void |
michael@0 | 142 | LifoAlloc::transferUnusedFrom(LifoAlloc *other) |
michael@0 | 143 | { |
michael@0 | 144 | JS_ASSERT(!markCount); |
michael@0 | 145 | JS_ASSERT(latest == first); |
michael@0 | 146 | |
michael@0 | 147 | if (other->markCount || !other->first) |
michael@0 | 148 | return; |
michael@0 | 149 | |
michael@0 | 150 | // Transfer all chunks *after* |latest|. |
michael@0 | 151 | |
michael@0 | 152 | if (other->latest->next()) { |
michael@0 | 153 | if (other->latest == other->first) { |
michael@0 | 154 | // We're transferring everything except the first chunk. |
michael@0 | 155 | size_t delta = other->curSize_ - other->first->computedSizeOfIncludingThis(); |
michael@0 | 156 | other->decrementCurSize(delta); |
michael@0 | 157 | incrementCurSize(delta); |
michael@0 | 158 | } else { |
michael@0 | 159 | for (BumpChunk *chunk = other->latest->next(); chunk; chunk = chunk->next()) { |
michael@0 | 160 | size_t size = chunk->computedSizeOfIncludingThis(); |
michael@0 | 161 | incrementCurSize(size); |
michael@0 | 162 | other->decrementCurSize(size); |
michael@0 | 163 | } |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | appendUnused(other->latest->next(), other->last); |
michael@0 | 167 | other->latest->setNext(nullptr); |
michael@0 | 168 | other->last = other->latest; |
michael@0 | 169 | } |
michael@0 | 170 | } |