daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 7 | #include "compiler/translator/PoolAlloc.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 8 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 9 | #include <assert.h> |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 10 | #include <stdint.h> |
| 11 | #include <stdio.h> |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 12 | |
Jamie Madill | 438dbcf | 2016-06-17 14:20:05 -0400 | [diff] [blame] | 13 | #include "common/angleutils.h" |
| 14 | #include "common/debug.h" |
| 15 | #include "common/platform.h" |
| 16 | #include "common/tls.h" |
| 17 | #include "compiler/translator/InitializeGlobals.h" |
| 18 | |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 19 | TLSIndex PoolIndex = TLS_INVALID_INDEX; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 20 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 21 | bool InitializePoolIndex() |
| 22 | { |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 23 | assert(PoolIndex == TLS_INVALID_INDEX); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 24 | |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 25 | PoolIndex = CreateTLSIndex(); |
| 26 | return PoolIndex != TLS_INVALID_INDEX; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void FreePoolIndex() |
| 30 | { |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 31 | assert(PoolIndex != TLS_INVALID_INDEX); |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 32 | |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 33 | DestroyTLSIndex(PoolIndex); |
| 34 | PoolIndex = TLS_INVALID_INDEX; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 37 | TPoolAllocator *GetGlobalPoolAllocator() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 38 | { |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 39 | assert(PoolIndex != TLS_INVALID_INDEX); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 40 | return static_cast<TPoolAllocator *>(GetTLSValue(PoolIndex)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 43 | void SetGlobalPoolAllocator(TPoolAllocator *poolAllocator) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 44 | { |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 45 | assert(PoolIndex != TLS_INVALID_INDEX); |
| 46 | SetTLSValue(PoolIndex, poolAllocator); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // |
| 50 | // Implement the functionality of the TPoolAllocator class, which |
| 51 | // is documented in PoolAlloc.h. |
| 52 | // |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 53 | TPoolAllocator::TPoolAllocator(int growthIncrement, int allocationAlignment) |
| 54 | : alignment(allocationAlignment), |
| 55 | #if !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
| 56 | pageSize(growthIncrement), |
| 57 | freeList(0), |
| 58 | inUseList(0), |
| 59 | numCalls(0), |
| 60 | totalBytes(0), |
| 61 | #endif |
| 62 | mLocked(false) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 63 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 64 | // |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 65 | // Adjust alignment to be at least pointer aligned and |
| 66 | // power of 2. |
| 67 | // |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 68 | size_t minAlign = sizeof(void *); |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 69 | alignment &= ~(minAlign - 1); |
| 70 | if (alignment < minAlign) |
| 71 | alignment = minAlign; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 72 | size_t a = 1; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 73 | while (a < alignment) |
| 74 | a <<= 1; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 75 | alignment = a; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 76 | alignmentMask = a - 1; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 77 | |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 78 | #if !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
| 79 | // |
| 80 | // Don't allow page sizes we know are smaller than all common |
| 81 | // OS page sizes. |
| 82 | // |
| 83 | if (pageSize < 4 * 1024) |
| 84 | pageSize = 4 * 1024; |
| 85 | |
| 86 | // |
| 87 | // A large currentPageOffset indicates a new page needs to |
| 88 | // be obtained to allocate memory. |
| 89 | // |
| 90 | currentPageOffset = pageSize; |
| 91 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 92 | // |
| 93 | // Align header skip |
| 94 | // |
| 95 | headerSkip = minAlign; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 96 | if (headerSkip < sizeof(tHeader)) |
| 97 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 98 | headerSkip = (sizeof(tHeader) + alignmentMask) & ~alignmentMask; |
| 99 | } |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 100 | #else // !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
| 101 | mStack.push_back({}); |
| 102 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TPoolAllocator::~TPoolAllocator() |
| 106 | { |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 107 | #if !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 108 | while (inUseList) |
| 109 | { |
| 110 | tHeader *next = inUseList->nextPage; |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 111 | inUseList->~tHeader(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 112 | delete[] reinterpret_cast<char *>(inUseList); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 113 | inUseList = next; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 114 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 115 | |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 116 | // We should not check the guard blocks |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 117 | // here, because we did it already when the block was |
| 118 | // placed into the free list. |
| 119 | // |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 120 | while (freeList) |
| 121 | { |
| 122 | tHeader *next = freeList->nextPage; |
| 123 | delete[] reinterpret_cast<char *>(freeList); |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 124 | freeList = next; |
| 125 | } |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 126 | #else // !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
| 127 | for (auto &allocs : mStack) |
| 128 | { |
| 129 | for (auto alloc : allocs) |
| 130 | { |
| 131 | free(alloc); |
| 132 | } |
| 133 | } |
| 134 | mStack.clear(); |
| 135 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Support MSVC++ 6.0 |
| 139 | const unsigned char TAllocation::guardBlockBeginVal = 0xfb; |
| 140 | const unsigned char TAllocation::guardBlockEndVal = 0xfe; |
| 141 | const unsigned char TAllocation::userDataFill = 0xcd; |
| 142 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 143 | #ifdef GUARD_BLOCKS |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 144 | const size_t TAllocation::guardBlockSize = 16; |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 145 | #else |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 146 | const size_t TAllocation::guardBlockSize = 0; |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 147 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 148 | |
| 149 | // |
| 150 | // Check a single guard block for damage |
| 151 | // |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 152 | void TAllocation::checkGuardBlock(unsigned char *blockMem, |
| 153 | unsigned char val, |
| 154 | const char *locText) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 155 | { |
daniel@transgaming.com | b969cc5 | 2011-03-15 18:23:59 +0000 | [diff] [blame] | 156 | #ifdef GUARD_BLOCKS |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 157 | for (size_t x = 0; x < guardBlockSize; x++) |
| 158 | { |
| 159 | if (blockMem[x] != val) |
| 160 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 161 | char assertMsg[80]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 162 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 163 | // We don't print the assert message. It's here just to be helpful. |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 164 | # if defined(_MSC_VER) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 165 | snprintf(assertMsg, sizeof(assertMsg), |
| 166 | "PoolAlloc: Damage %s %Iu byte allocation at 0x%p\n", locText, size, data()); |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 167 | # else |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 168 | snprintf(assertMsg, sizeof(assertMsg), |
| 169 | "PoolAlloc: Damage %s %zu byte allocation at 0x%p\n", locText, size, data()); |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 170 | # endif |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 171 | assert(0 && "PoolAlloc: Damage in guard block"); |
| 172 | } |
| 173 | } |
daniel@transgaming.com | b969cc5 | 2011-03-15 18:23:59 +0000 | [diff] [blame] | 174 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 175 | } |
| 176 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 177 | void TPoolAllocator::push() |
| 178 | { |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 179 | #if !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 180 | tAllocState state = {currentPageOffset, inUseList}; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 181 | |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 182 | mStack.push_back(state); |
| 183 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 184 | // |
| 185 | // Indicate there is no current page to allocate from. |
| 186 | // |
| 187 | currentPageOffset = pageSize; |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 188 | #else // !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
| 189 | mStack.push_back({}); |
| 190 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // |
| 194 | // Do a mass-deallocation of all the individual allocations |
| 195 | // that have occurred since the last push(), or since the |
| 196 | // last pop(), or since the object's creation. |
| 197 | // |
| 198 | // The deallocated pages are saved for future allocations. |
| 199 | // |
| 200 | void TPoolAllocator::pop() |
| 201 | { |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 202 | if (mStack.size() < 1) |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 203 | return; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 204 | |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 205 | #if !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
| 206 | tHeader *page = mStack.back().page; |
| 207 | currentPageOffset = mStack.back().offset; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 208 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 209 | while (inUseList != page) |
| 210 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 211 | // invoke destructor to free allocation list |
| 212 | inUseList->~tHeader(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 213 | |
| 214 | tHeader *nextInUse = inUseList->nextPage; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 215 | if (inUseList->pageCount > 1) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 216 | delete[] reinterpret_cast<char *>(inUseList); |
| 217 | else |
| 218 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 219 | inUseList->nextPage = freeList; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 220 | freeList = inUseList; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 221 | } |
| 222 | inUseList = nextInUse; |
| 223 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 224 | |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 225 | mStack.pop_back(); |
| 226 | #else // !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
| 227 | for (auto &alloc : mStack.back()) |
| 228 | { |
| 229 | free(alloc); |
| 230 | } |
| 231 | mStack.pop_back(); |
| 232 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // |
| 236 | // Do a mass-deallocation of all the individual allocations |
| 237 | // that have occurred. |
| 238 | // |
| 239 | void TPoolAllocator::popAll() |
| 240 | { |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 241 | while (mStack.size() > 0) |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 242 | pop(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 245 | void *TPoolAllocator::allocate(size_t numBytes) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 246 | { |
Jamie Madill | 438dbcf | 2016-06-17 14:20:05 -0400 | [diff] [blame] | 247 | ASSERT(!mLocked); |
| 248 | |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 249 | #if !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 250 | // |
| 251 | // Just keep some interesting statistics. |
| 252 | // |
| 253 | ++numCalls; |
| 254 | totalBytes += numBytes; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 255 | |
shannonwoods@chromium.org | c796484 | 2013-05-30 00:10:41 +0000 | [diff] [blame] | 256 | // If we are using guard blocks, all allocations are bracketed by |
| 257 | // them: [guardblock][allocation][guardblock]. numBytes is how |
| 258 | // much memory the caller asked for. allocationSize is the total |
| 259 | // size including guard blocks. In release build, |
| 260 | // guardBlockSize=0 and this all gets optimized away. |
| 261 | size_t allocationSize = TAllocation::allocationSize(numBytes); |
| 262 | // Detect integer overflow. |
| 263 | if (allocationSize < numBytes) |
| 264 | return 0; |
| 265 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 266 | // |
| 267 | // Do the allocation, most likely case first, for efficiency. |
| 268 | // This step could be moved to be inline sometime. |
| 269 | // |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 270 | if (allocationSize <= pageSize - currentPageOffset) |
| 271 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 272 | // |
| 273 | // Safe to allocate from currentPageOffset. |
| 274 | // |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 275 | unsigned char *memory = reinterpret_cast<unsigned char *>(inUseList) + currentPageOffset; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 276 | currentPageOffset += allocationSize; |
| 277 | currentPageOffset = (currentPageOffset + alignmentMask) & ~alignmentMask; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 278 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 279 | return initializeAllocation(inUseList, memory, numBytes); |
| 280 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 281 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 282 | if (allocationSize > pageSize - headerSkip) |
| 283 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 284 | // |
| 285 | // Do a multi-page allocation. Don't mix these with the others. |
| 286 | // The OS is efficient and allocating and free-ing multiple pages. |
| 287 | // |
| 288 | size_t numBytesToAlloc = allocationSize + headerSkip; |
shannonwoods@chromium.org | c796484 | 2013-05-30 00:10:41 +0000 | [diff] [blame] | 289 | // Detect integer overflow. |
| 290 | if (numBytesToAlloc < allocationSize) |
| 291 | return 0; |
| 292 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 293 | tHeader *memory = reinterpret_cast<tHeader *>(::new char[numBytesToAlloc]); |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 294 | if (memory == 0) |
| 295 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 296 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 297 | // Use placement-new to initialize header |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 298 | new (memory) tHeader(inUseList, (numBytesToAlloc + pageSize - 1) / pageSize); |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 299 | inUseList = memory; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 300 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 301 | currentPageOffset = pageSize; // make next allocation come from a new page |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 302 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 303 | // No guard blocks for multi-page allocations (yet) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 304 | return reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(memory) + headerSkip); |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 305 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 306 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 307 | // |
| 308 | // Need a simple page to allocate from. |
| 309 | // |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 310 | tHeader *memory; |
| 311 | if (freeList) |
| 312 | { |
| 313 | memory = freeList; |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 314 | freeList = freeList->nextPage; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 315 | } |
| 316 | else |
| 317 | { |
| 318 | memory = reinterpret_cast<tHeader *>(::new char[pageSize]); |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 319 | if (memory == 0) |
| 320 | return 0; |
| 321 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 322 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 323 | // Use placement-new to initialize header |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 324 | new (memory) tHeader(inUseList, 1); |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 325 | inUseList = memory; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 326 | |
| 327 | unsigned char *ret = reinterpret_cast<unsigned char *>(inUseList) + headerSkip; |
| 328 | currentPageOffset = (headerSkip + allocationSize + alignmentMask) & ~alignmentMask; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 329 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 330 | return initializeAllocation(inUseList, ret, numBytes); |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 331 | #else // !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC) |
| 332 | void *alloc = malloc(numBytes + alignmentMask); |
| 333 | mStack.back().push_back(alloc); |
| 334 | |
| 335 | intptr_t intAlloc = reinterpret_cast<intptr_t>(alloc); |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 336 | intAlloc = (intAlloc + alignmentMask) & ~alignmentMask; |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 337 | return reinterpret_cast<void *>(intAlloc); |
| 338 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Jamie Madill | 438dbcf | 2016-06-17 14:20:05 -0400 | [diff] [blame] | 341 | void TPoolAllocator::lock() |
| 342 | { |
| 343 | ASSERT(!mLocked); |
| 344 | mLocked = true; |
| 345 | } |
| 346 | |
| 347 | void TPoolAllocator::unlock() |
| 348 | { |
| 349 | ASSERT(mLocked); |
| 350 | mLocked = false; |
| 351 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 352 | |
| 353 | // |
| 354 | // Check all allocations in a list for damage by calling check on each. |
| 355 | // |
| 356 | void TAllocation::checkAllocList() const |
| 357 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 358 | for (const TAllocation *alloc = this; alloc != 0; alloc = alloc->prevAlloc) |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 359 | alloc->check(); |
daniel@transgaming.com | b969cc5 | 2011-03-15 18:23:59 +0000 | [diff] [blame] | 360 | } |