2 // Copyright (c) 2002-2012 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.
7 #include "compiler/PoolAlloc.h"
14 #include "compiler/InitializeGlobals.h"
15 #include "compiler/osinclude.h"
17 OS_TLSIndex PoolIndex = OS_INVALID_TLS_INDEX;
19 bool InitializePoolIndex()
21 assert(PoolIndex == OS_INVALID_TLS_INDEX);
23 PoolIndex = OS_AllocTLSIndex();
24 return PoolIndex != OS_INVALID_TLS_INDEX;
29 assert(PoolIndex != OS_INVALID_TLS_INDEX);
31 OS_FreeTLSIndex(PoolIndex);
32 PoolIndex = OS_INVALID_TLS_INDEX;
35 TPoolAllocator* GetGlobalPoolAllocator()
37 assert(PoolIndex != OS_INVALID_TLS_INDEX);
38 return static_cast<TPoolAllocator*>(OS_GetTLSValue(PoolIndex));
41 void SetGlobalPoolAllocator(TPoolAllocator* poolAllocator)
43 assert(PoolIndex != OS_INVALID_TLS_INDEX);
44 OS_SetTLSValue(PoolIndex, poolAllocator);
48 // Implement the functionality of the TPoolAllocator class, which
49 // is documented in PoolAlloc.h.
51 TPoolAllocator::TPoolAllocator(int growthIncrement, int allocationAlignment) :
52 pageSize(growthIncrement),
53 alignment(allocationAlignment),
60 // Don't allow page sizes we know are smaller than all common
63 if (pageSize < 4*1024)
67 // A large currentPageOffset indicates a new page needs to
68 // be obtained to allocate memory.
70 currentPageOffset = pageSize;
73 // Adjust alignment to be at least pointer aligned and
76 size_t minAlign = sizeof(void*);
77 alignment &= ~(minAlign - 1);
78 if (alignment < minAlign)
84 alignmentMask = a - 1;
89 headerSkip = minAlign;
90 if (headerSkip < sizeof(tHeader)) {
91 headerSkip = (sizeof(tHeader) + alignmentMask) & ~alignmentMask;
95 TPoolAllocator::~TPoolAllocator()
98 tHeader* next = inUseList->nextPage;
99 inUseList->~tHeader();
100 delete [] reinterpret_cast<char*>(inUseList);
104 // We should not check the guard blocks
105 // here, because we did it already when the block was
106 // placed into the free list.
109 tHeader* next = freeList->nextPage;
110 delete [] reinterpret_cast<char*>(freeList);
115 // Support MSVC++ 6.0
116 const unsigned char TAllocation::guardBlockBeginVal = 0xfb;
117 const unsigned char TAllocation::guardBlockEndVal = 0xfe;
118 const unsigned char TAllocation::userDataFill = 0xcd;
121 const size_t TAllocation::guardBlockSize = 16;
123 const size_t TAllocation::guardBlockSize = 0;
127 // Check a single guard block for damage
129 void TAllocation::checkGuardBlock(unsigned char* blockMem, unsigned char val, const char* locText) const
132 for (size_t x = 0; x < guardBlockSize; x++) {
133 if (blockMem[x] != val) {
136 // We don't print the assert message. It's here just to be helpful.
137 #if defined(_MSC_VER)
138 _snprintf(assertMsg, sizeof(assertMsg), "PoolAlloc: Damage %s %Iu byte allocation at 0x%p\n",
139 locText, size, data());
141 snprintf(assertMsg, sizeof(assertMsg), "PoolAlloc: Damage %s %zu byte allocation at 0x%p\n",
142 locText, size, data());
144 assert(0 && "PoolAlloc: Damage in guard block");
151 void TPoolAllocator::push()
153 tAllocState state = { currentPageOffset, inUseList };
155 stack.push_back(state);
158 // Indicate there is no current page to allocate from.
160 currentPageOffset = pageSize;
164 // Do a mass-deallocation of all the individual allocations
165 // that have occurred since the last push(), or since the
166 // last pop(), or since the object's creation.
168 // The deallocated pages are saved for future allocations.
170 void TPoolAllocator::pop()
172 if (stack.size() < 1)
175 tHeader* page = stack.back().page;
176 currentPageOffset = stack.back().offset;
178 while (inUseList != page) {
179 // invoke destructor to free allocation list
180 inUseList->~tHeader();
182 tHeader* nextInUse = inUseList->nextPage;
183 if (inUseList->pageCount > 1)
184 delete [] reinterpret_cast<char*>(inUseList);
186 inUseList->nextPage = freeList;
187 freeList = inUseList;
189 inUseList = nextInUse;
196 // Do a mass-deallocation of all the individual allocations
197 // that have occurred.
199 void TPoolAllocator::popAll()
201 while (stack.size() > 0)
205 void* TPoolAllocator::allocate(size_t numBytes)
208 // Just keep some interesting statistics.
211 totalBytes += numBytes;
213 // If we are using guard blocks, all allocations are bracketed by
214 // them: [guardblock][allocation][guardblock]. numBytes is how
215 // much memory the caller asked for. allocationSize is the total
216 // size including guard blocks. In release build,
217 // guardBlockSize=0 and this all gets optimized away.
218 size_t allocationSize = TAllocation::allocationSize(numBytes);
219 // Detect integer overflow.
220 if (allocationSize < numBytes)
224 // Do the allocation, most likely case first, for efficiency.
225 // This step could be moved to be inline sometime.
227 if (allocationSize <= pageSize - currentPageOffset) {
229 // Safe to allocate from currentPageOffset.
231 unsigned char* memory = reinterpret_cast<unsigned char *>(inUseList) + currentPageOffset;
232 currentPageOffset += allocationSize;
233 currentPageOffset = (currentPageOffset + alignmentMask) & ~alignmentMask;
235 return initializeAllocation(inUseList, memory, numBytes);
238 if (allocationSize > pageSize - headerSkip) {
240 // Do a multi-page allocation. Don't mix these with the others.
241 // The OS is efficient and allocating and free-ing multiple pages.
243 size_t numBytesToAlloc = allocationSize + headerSkip;
244 // Detect integer overflow.
245 if (numBytesToAlloc < allocationSize)
248 tHeader* memory = reinterpret_cast<tHeader*>(::new char[numBytesToAlloc]);
252 // Use placement-new to initialize header
253 new(memory) tHeader(inUseList, (numBytesToAlloc + pageSize - 1) / pageSize);
256 currentPageOffset = pageSize; // make next allocation come from a new page
258 // No guard blocks for multi-page allocations (yet)
259 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(memory) + headerSkip);
263 // Need a simple page to allocate from.
268 freeList = freeList->nextPage;
270 memory = reinterpret_cast<tHeader*>(::new char[pageSize]);
275 // Use placement-new to initialize header
276 new(memory) tHeader(inUseList, 1);
279 unsigned char* ret = reinterpret_cast<unsigned char *>(inUseList) + headerSkip;
280 currentPageOffset = (headerSkip + allocationSize + alignmentMask) & ~alignmentMask;
282 return initializeAllocation(inUseList, ret, numBytes);
287 // Check all allocations in a list for damage by calling check on each.
289 void TAllocation::checkAllocList() const
291 for (const TAllocation* alloc = this; alloc != 0; alloc = alloc->prevAlloc)