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 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 7 | #include "compiler/PoolAlloc.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 8 | |
alokp@chromium.org | 1bcc3fd | 2010-05-19 17:08:44 +0000 | [diff] [blame^] | 9 | #ifndef _MSC_VER |
| 10 | #include <stdint.h> |
| 11 | #endif |
| 12 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 13 | #include "compiler/InitializeGlobals.h" |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 14 | #include "compiler/osinclude.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 15 | |
alokp@chromium.org | ff42c63 | 2010-05-10 15:14:30 +0000 | [diff] [blame] | 16 | OS_TLSIndex PoolIndex = OS_INVALID_TLS_INDEX; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | |
| 18 | void InitializeGlobalPools() |
| 19 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 20 | TThreadGlobalPools* globalPools= static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex)); |
| 21 | if (globalPools) |
| 22 | return; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 23 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 24 | TPoolAllocator *globalPoolAllocator = new TPoolAllocator(true); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 25 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 26 | TThreadGlobalPools* threadData = new TThreadGlobalPools(); |
| 27 | |
| 28 | threadData->globalPoolAllocator = globalPoolAllocator; |
| 29 | |
| 30 | OS_SetTLSValue(PoolIndex, threadData); |
| 31 | globalPoolAllocator->push(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void FreeGlobalPools() |
| 35 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 36 | // Release the allocated memory for this thread. |
| 37 | TThreadGlobalPools* globalPools= static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex)); |
| 38 | if (!globalPools) |
| 39 | return; |
| 40 | |
| 41 | GlobalPoolAllocator.popAll(); |
| 42 | delete &GlobalPoolAllocator; |
| 43 | delete globalPools; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | bool InitializePoolIndex() |
| 47 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 48 | // Allocate a TLS index. |
| 49 | if ((PoolIndex = OS_AllocTLSIndex()) == OS_INVALID_TLS_INDEX) |
| 50 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 51 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 52 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void FreePoolIndex() |
| 56 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 57 | // Release the TLS index. |
| 58 | OS_FreeTLSIndex(PoolIndex); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TPoolAllocator& GetGlobalPoolAllocator() |
| 62 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 63 | TThreadGlobalPools* threadData = static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 64 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 65 | return *threadData->globalPoolAllocator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void SetGlobalPoolAllocatorPtr(TPoolAllocator* poolAllocator) |
| 69 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 70 | TThreadGlobalPools* threadData = static_cast<TThreadGlobalPools*>(OS_GetTLSValue(PoolIndex)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 71 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 72 | threadData->globalPoolAllocator = poolAllocator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // |
| 76 | // Implement the functionality of the TPoolAllocator class, which |
| 77 | // is documented in PoolAlloc.h. |
| 78 | // |
| 79 | TPoolAllocator::TPoolAllocator(bool g, int growthIncrement, int allocationAlignment) : |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 80 | global(g), |
| 81 | pageSize(growthIncrement), |
| 82 | alignment(allocationAlignment), |
| 83 | freeList(0), |
| 84 | inUseList(0), |
| 85 | numCalls(0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 86 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 87 | // |
| 88 | // Don't allow page sizes we know are smaller than all common |
| 89 | // OS page sizes. |
| 90 | // |
| 91 | if (pageSize < 4*1024) |
| 92 | pageSize = 4*1024; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 93 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 94 | // |
| 95 | // A large currentPageOffset indicates a new page needs to |
| 96 | // be obtained to allocate memory. |
| 97 | // |
| 98 | currentPageOffset = pageSize; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 99 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 100 | // |
| 101 | // Adjust alignment to be at least pointer aligned and |
| 102 | // power of 2. |
| 103 | // |
| 104 | size_t minAlign = sizeof(void*); |
| 105 | alignment &= ~(minAlign - 1); |
| 106 | if (alignment < minAlign) |
| 107 | alignment = minAlign; |
| 108 | size_t a = 1; |
| 109 | while (a < alignment) |
| 110 | a <<= 1; |
| 111 | alignment = a; |
| 112 | alignmentMask = a - 1; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 113 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 114 | // |
| 115 | // Align header skip |
| 116 | // |
| 117 | headerSkip = minAlign; |
| 118 | if (headerSkip < sizeof(tHeader)) { |
| 119 | headerSkip = (sizeof(tHeader) + alignmentMask) & ~alignmentMask; |
| 120 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TPoolAllocator::~TPoolAllocator() |
| 124 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 125 | if (!global) { |
| 126 | // |
| 127 | // Then we know that this object is not being |
| 128 | // allocated after other, globally scoped objects |
| 129 | // that depend on it. So we can delete the "in use" memory. |
| 130 | // |
| 131 | while (inUseList) { |
| 132 | tHeader* next = inUseList->nextPage; |
| 133 | inUseList->~tHeader(); |
| 134 | delete [] reinterpret_cast<char*>(inUseList); |
| 135 | inUseList = next; |
| 136 | } |
| 137 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 138 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 139 | // |
| 140 | // Always delete the free list memory - it can't be being |
| 141 | // (correctly) referenced, whether the pool allocator was |
| 142 | // global or not. We should not check the guard blocks |
| 143 | // here, because we did it already when the block was |
| 144 | // placed into the free list. |
| 145 | // |
| 146 | while (freeList) { |
| 147 | tHeader* next = freeList->nextPage; |
| 148 | delete [] reinterpret_cast<char*>(freeList); |
| 149 | freeList = next; |
| 150 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Support MSVC++ 6.0 |
| 154 | const unsigned char TAllocation::guardBlockBeginVal = 0xfb; |
| 155 | const unsigned char TAllocation::guardBlockEndVal = 0xfe; |
| 156 | const unsigned char TAllocation::userDataFill = 0xcd; |
| 157 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 158 | #ifdef GUARD_BLOCKS |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 159 | const size_t TAllocation::guardBlockSize = 16; |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 160 | #else |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 161 | const size_t TAllocation::guardBlockSize = 0; |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 162 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 163 | |
| 164 | // |
| 165 | // Check a single guard block for damage |
| 166 | // |
| 167 | void TAllocation::checkGuardBlock(unsigned char* blockMem, unsigned char val, const char* locText) const |
| 168 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 169 | for (size_t x = 0; x < guardBlockSize; x++) { |
| 170 | if (blockMem[x] != val) { |
| 171 | char assertMsg[80]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 172 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 173 | // We don't print the assert message. It's here just to be helpful. |
alokp@chromium.org | 1bcc3fd | 2010-05-19 17:08:44 +0000 | [diff] [blame^] | 174 | sprintf(assertMsg, "PoolAlloc: Damage %s %u byte allocation at 0x%p\n", |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 175 | locText, size, data()); |
| 176 | assert(0 && "PoolAlloc: Damage in guard block"); |
| 177 | } |
| 178 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
| 182 | void TPoolAllocator::push() |
| 183 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 184 | tAllocState state = { currentPageOffset, inUseList }; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 185 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 186 | stack.push_back(state); |
| 187 | |
| 188 | // |
| 189 | // Indicate there is no current page to allocate from. |
| 190 | // |
| 191 | currentPageOffset = pageSize; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // |
| 195 | // Do a mass-deallocation of all the individual allocations |
| 196 | // that have occurred since the last push(), or since the |
| 197 | // last pop(), or since the object's creation. |
| 198 | // |
| 199 | // The deallocated pages are saved for future allocations. |
| 200 | // |
| 201 | void TPoolAllocator::pop() |
| 202 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 203 | if (stack.size() < 1) |
| 204 | return; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 205 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 206 | tHeader* page = stack.back().page; |
| 207 | currentPageOffset = stack.back().offset; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 208 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 209 | while (inUseList != page) { |
| 210 | // invoke destructor to free allocation list |
| 211 | inUseList->~tHeader(); |
| 212 | |
| 213 | tHeader* nextInUse = inUseList->nextPage; |
| 214 | if (inUseList->pageCount > 1) |
| 215 | delete [] reinterpret_cast<char*>(inUseList); |
| 216 | else { |
| 217 | inUseList->nextPage = freeList; |
| 218 | freeList = inUseList; |
| 219 | } |
| 220 | inUseList = nextInUse; |
| 221 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 222 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 223 | stack.pop_back(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // |
| 227 | // Do a mass-deallocation of all the individual allocations |
| 228 | // that have occurred. |
| 229 | // |
| 230 | void TPoolAllocator::popAll() |
| 231 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 232 | while (stack.size() > 0) |
| 233 | pop(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void* TPoolAllocator::allocate(size_t numBytes) |
| 237 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 238 | // If we are using guard blocks, all allocations are bracketed by |
| 239 | // them: [guardblock][allocation][guardblock]. numBytes is how |
| 240 | // much memory the caller asked for. allocationSize is the total |
| 241 | // size including guard blocks. In release build, |
| 242 | // guardBlockSize=0 and this all gets optimized away. |
| 243 | size_t allocationSize = TAllocation::allocationSize(numBytes); |
| 244 | |
| 245 | // |
| 246 | // Just keep some interesting statistics. |
| 247 | // |
| 248 | ++numCalls; |
| 249 | totalBytes += numBytes; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 250 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 251 | // |
| 252 | // Do the allocation, most likely case first, for efficiency. |
| 253 | // This step could be moved to be inline sometime. |
| 254 | // |
| 255 | if (currentPageOffset + allocationSize <= pageSize) { |
| 256 | // |
| 257 | // Safe to allocate from currentPageOffset. |
| 258 | // |
| 259 | unsigned char* memory = reinterpret_cast<unsigned char *>(inUseList) + currentPageOffset; |
| 260 | currentPageOffset += allocationSize; |
| 261 | currentPageOffset = (currentPageOffset + alignmentMask) & ~alignmentMask; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 262 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 263 | return initializeAllocation(inUseList, memory, numBytes); |
| 264 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 265 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 266 | if (allocationSize + headerSkip > pageSize) { |
| 267 | // |
| 268 | // Do a multi-page allocation. Don't mix these with the others. |
| 269 | // The OS is efficient and allocating and free-ing multiple pages. |
| 270 | // |
| 271 | size_t numBytesToAlloc = allocationSize + headerSkip; |
| 272 | tHeader* memory = reinterpret_cast<tHeader*>(::new char[numBytesToAlloc]); |
| 273 | if (memory == 0) |
| 274 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 275 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 276 | // Use placement-new to initialize header |
| 277 | new(memory) tHeader(inUseList, (numBytesToAlloc + pageSize - 1) / pageSize); |
| 278 | inUseList = memory; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 279 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 280 | currentPageOffset = pageSize; // make next allocation come from a new page |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 281 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 282 | // No guard blocks for multi-page allocations (yet) |
| 283 | return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(memory) + headerSkip); |
| 284 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 285 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 286 | // |
| 287 | // Need a simple page to allocate from. |
| 288 | // |
| 289 | tHeader* memory; |
| 290 | if (freeList) { |
| 291 | memory = freeList; |
| 292 | freeList = freeList->nextPage; |
| 293 | } else { |
| 294 | memory = reinterpret_cast<tHeader*>(::new char[pageSize]); |
| 295 | if (memory == 0) |
| 296 | return 0; |
| 297 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 298 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 299 | // Use placement-new to initialize header |
| 300 | new(memory) tHeader(inUseList, 1); |
| 301 | inUseList = memory; |
| 302 | |
| 303 | unsigned char* ret = reinterpret_cast<unsigned char *>(inUseList) + headerSkip; |
| 304 | currentPageOffset = (headerSkip + allocationSize + alignmentMask) & ~alignmentMask; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 305 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 306 | return initializeAllocation(inUseList, ret, numBytes); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
| 310 | // |
| 311 | // Check all allocations in a list for damage by calling check on each. |
| 312 | // |
| 313 | void TAllocation::checkAllocList() const |
| 314 | { |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 315 | for (const TAllocation* alloc = this; alloc != 0; alloc = alloc->prevAlloc) |
| 316 | alloc->check(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 317 | } |