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