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 | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 7 | #ifndef COMPILER_TRANSLATOR_POOLALLOC_H_ |
| 8 | #define COMPILER_TRANSLATOR_POOLALLOC_H_ |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 9 | |
| 10 | #ifdef _DEBUG |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 11 | #define GUARD_BLOCKS // define to enable guard block sanity checking |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
| 14 | // |
| 15 | // This header defines an allocator that can be used to efficiently |
| 16 | // allocate a large number of small requests for heap memory, with the |
| 17 | // intention that they are not individually deallocated, but rather |
| 18 | // collectively deallocated at one time. |
| 19 | // |
| 20 | // This simultaneously |
| 21 | // |
| 22 | // * Makes each individual allocation much more efficient; the |
| 23 | // typical allocation is trivial. |
| 24 | // * Completely avoids the cost of doing individual deallocation. |
| 25 | // * Saves the trouble of tracking down and plugging a large class of leaks. |
| 26 | // |
| 27 | // Individual classes can use this allocator by supplying their own |
| 28 | // new and delete methods. |
| 29 | // |
| 30 | // STL containers can use this allocator by using the pool_allocator |
| 31 | // class as the allocator (second) template argument. |
| 32 | // |
| 33 | |
| 34 | #include <stddef.h> |
alokp@chromium.org | cff1aff | 2010-05-14 19:24:22 +0000 | [diff] [blame] | 35 | #include <string.h> |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 36 | #include <vector> |
| 37 | |
| 38 | // If we are using guard blocks, we must track each indivual |
| 39 | // allocation. If we aren't using guard blocks, these |
| 40 | // never get instantiated, so won't have any impact. |
| 41 | // |
| 42 | |
| 43 | class TAllocation { |
| 44 | public: |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 45 | TAllocation(size_t size, unsigned char* mem, TAllocation* prev = 0) : |
| 46 | size(size), mem(mem), prevAlloc(prev) { |
| 47 | // Allocations are bracketed: |
| 48 | // [allocationHeader][initialGuardBlock][userData][finalGuardBlock] |
| 49 | // This would be cleaner with if (guardBlockSize)..., but that |
| 50 | // makes the compiler print warnings about 0 length memsets, |
| 51 | // even with the if() protecting them. |
| 52 | #ifdef GUARD_BLOCKS |
| 53 | memset(preGuard(), guardBlockBeginVal, guardBlockSize); |
| 54 | memset(data(), userDataFill, size); |
| 55 | memset(postGuard(), guardBlockEndVal, guardBlockSize); |
| 56 | #endif |
| 57 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 58 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 59 | void check() const { |
| 60 | checkGuardBlock(preGuard(), guardBlockBeginVal, "before"); |
| 61 | checkGuardBlock(postGuard(), guardBlockEndVal, "after"); |
| 62 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 63 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 64 | void checkAllocList() const; |
| 65 | |
| 66 | // Return total size needed to accomodate user buffer of 'size', |
| 67 | // plus our tracking data. |
| 68 | inline static size_t allocationSize(size_t size) { |
| 69 | return size + 2 * guardBlockSize + headerSize(); |
| 70 | } |
| 71 | |
| 72 | // Offset from surrounding buffer to get to user data buffer. |
| 73 | inline static unsigned char* offsetAllocation(unsigned char* m) { |
| 74 | return m + guardBlockSize + headerSize(); |
| 75 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 76 | |
| 77 | private: |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 78 | void checkGuardBlock(unsigned char* blockMem, unsigned char val, const char* locText) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 79 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 80 | // Find offsets to pre and post guard blocks, and user data buffer |
| 81 | unsigned char* preGuard() const { return mem + headerSize(); } |
| 82 | unsigned char* data() const { return preGuard() + guardBlockSize; } |
| 83 | unsigned char* postGuard() const { return data() + size; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 84 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 85 | size_t size; // size of the user data area |
| 86 | unsigned char* mem; // beginning of our allocation (pts to header) |
| 87 | TAllocation* prevAlloc; // prior allocation in the chain |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 88 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 89 | // Support MSVC++ 6.0 |
| 90 | const static unsigned char guardBlockBeginVal; |
| 91 | const static unsigned char guardBlockEndVal; |
| 92 | const static unsigned char userDataFill; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 93 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 94 | const static size_t guardBlockSize; |
| 95 | #ifdef GUARD_BLOCKS |
| 96 | inline static size_t headerSize() { return sizeof(TAllocation); } |
| 97 | #else |
| 98 | inline static size_t headerSize() { return 0; } |
| 99 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 100 | }; |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 101 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 102 | // |
| 103 | // There are several stacks. One is to track the pushing and popping |
| 104 | // of the user, and not yet implemented. The others are simply a |
| 105 | // repositories of free pages or used pages. |
| 106 | // |
| 107 | // Page stacks are linked together with a simple header at the beginning |
| 108 | // of each allocation obtained from the underlying OS. Multi-page allocations |
| 109 | // are returned to the OS. Individual page allocations are kept for future |
| 110 | // re-use. |
| 111 | // |
| 112 | // The "page size" used is not, nor must it match, the underlying OS |
| 113 | // page size. But, having it be about that size or equal to a set of |
| 114 | // pages is likely most optimal. |
| 115 | // |
| 116 | class TPoolAllocator { |
| 117 | public: |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 118 | TPoolAllocator(int growthIncrement = 8*1024, int allocationAlignment = 16); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 119 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 120 | // |
| 121 | // Don't call the destructor just to free up the memory, call pop() |
| 122 | // |
| 123 | ~TPoolAllocator(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 124 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 125 | // |
| 126 | // Call push() to establish a new place to pop memory too. Does not |
| 127 | // have to be called to get things started. |
| 128 | // |
| 129 | void push(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 130 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 131 | // |
| 132 | // Call pop() to free all memory allocated since the last call to push(), |
| 133 | // or if no last call to push, frees all memory since first allocation. |
| 134 | // |
| 135 | void pop(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 136 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 137 | // |
| 138 | // Call popAll() to free all memory allocated. |
| 139 | // |
| 140 | void popAll(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 141 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 142 | // |
| 143 | // Call allocate() to actually acquire memory. Returns 0 if no memory |
| 144 | // available, otherwise a properly aligned pointer to 'numBytes' of memory. |
| 145 | // |
| 146 | void* allocate(size_t numBytes); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 147 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 148 | // |
| 149 | // There is no deallocate. The point of this class is that |
| 150 | // deallocation can be skipped by the user of it, as the model |
| 151 | // of use is to simultaneously deallocate everything at once |
| 152 | // by calling pop(), and to not have to solve memory leak problems. |
| 153 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 154 | |
| 155 | protected: |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 156 | friend struct tHeader; |
| 157 | |
| 158 | struct tHeader { |
| 159 | tHeader(tHeader* nextPage, size_t pageCount) : |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 160 | nextPage(nextPage), |
alokp@chromium.org | b2dfd8e | 2010-08-09 22:28:19 +0000 | [diff] [blame] | 161 | pageCount(pageCount) |
| 162 | #ifdef GUARD_BLOCKS |
| 163 | , lastAllocation(0) |
| 164 | #endif |
| 165 | { } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 166 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 167 | ~tHeader() { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 168 | #ifdef GUARD_BLOCKS |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 169 | if (lastAllocation) |
| 170 | lastAllocation->checkAllocList(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 171 | #endif |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 172 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 173 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 174 | tHeader* nextPage; |
| 175 | size_t pageCount; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 176 | #ifdef GUARD_BLOCKS |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 177 | TAllocation* lastAllocation; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 178 | #endif |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 179 | }; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 180 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 181 | struct tAllocState { |
| 182 | size_t offset; |
| 183 | tHeader* page; |
| 184 | }; |
| 185 | typedef std::vector<tAllocState> tAllocStack; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 186 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 187 | // Track allocations if and only if we're using guard blocks |
| 188 | void* initializeAllocation(tHeader* block, unsigned char* memory, size_t numBytes) { |
| 189 | #ifdef GUARD_BLOCKS |
| 190 | new(memory) TAllocation(numBytes, memory, block->lastAllocation); |
| 191 | block->lastAllocation = reinterpret_cast<TAllocation*>(memory); |
| 192 | #endif |
| 193 | // This is optimized entirely away if GUARD_BLOCKS is not defined. |
| 194 | return TAllocation::offsetAllocation(memory); |
| 195 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 196 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 197 | size_t pageSize; // granularity of allocation from the OS |
| 198 | size_t alignment; // all returned allocations will be aligned at |
| 199 | // this granularity, which will be a power of 2 |
| 200 | size_t alignmentMask; |
| 201 | size_t headerSkip; // amount of memory to skip to make room for the |
| 202 | // header (basically, size of header, rounded |
| 203 | // up to make it aligned |
| 204 | size_t currentPageOffset; // next offset in top of inUseList to allocate from |
| 205 | tHeader* freeList; // list of popped memory |
| 206 | tHeader* inUseList; // list of all memory currently being used |
| 207 | tAllocStack stack; // stack of where to allocate from, to partition pool |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 208 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 209 | int numCalls; // just an interesting statistic |
| 210 | size_t totalBytes; // just an interesting statistic |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 211 | private: |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 212 | TPoolAllocator& operator=(const TPoolAllocator&); // dont allow assignment operator |
| 213 | TPoolAllocator(const TPoolAllocator&); // dont allow default copy constructor |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | |
| 217 | // |
| 218 | // There could potentially be many pools with pops happening at |
| 219 | // different times. But a simple use is to have a global pop |
| 220 | // with everyone using the same global allocator. |
| 221 | // |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 222 | extern TPoolAllocator* GetGlobalPoolAllocator(); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 223 | extern void SetGlobalPoolAllocator(TPoolAllocator* poolAllocator); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 224 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 225 | // |
| 226 | // This STL compatible allocator is intended to be used as the allocator |
| 227 | // parameter to templatized STL containers, like vector and map. |
| 228 | // |
| 229 | // It will use the pools for allocation, and not |
| 230 | // do any deallocation, but will still do destruction. |
| 231 | // |
| 232 | template<class T> |
| 233 | class pool_allocator { |
| 234 | public: |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 235 | typedef size_t size_type; |
| 236 | typedef ptrdiff_t difference_type; |
| 237 | typedef T* pointer; |
| 238 | typedef const T* const_pointer; |
| 239 | typedef T& reference; |
| 240 | typedef const T& const_reference; |
| 241 | typedef T value_type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 242 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 243 | template<class Other> |
| 244 | struct rebind { |
| 245 | typedef pool_allocator<Other> other; |
| 246 | }; |
| 247 | pointer address(reference x) const { return &x; } |
| 248 | const_pointer address(const_reference x) const { return &x; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 249 | |
Dmitry Skiba | 0197111 | 2015-07-10 14:54:00 -0400 | [diff] [blame] | 250 | pool_allocator() { } |
Jamie Madill | 53b7610 | 2015-07-10 18:52:58 +0000 | [diff] [blame] | 251 | |
| 252 | template<class Other> |
Dmitry Skiba | 0197111 | 2015-07-10 14:54:00 -0400 | [diff] [blame] | 253 | pool_allocator(const pool_allocator<Other>& p) { } |
| 254 | |
| 255 | template <class Other> |
| 256 | pool_allocator<T>& operator=(const pool_allocator<Other>& p) { return *this; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 257 | |
alokp@chromium.org | b416e70 | 2010-08-09 22:32:56 +0000 | [diff] [blame] | 258 | #if defined(__SUNPRO_CC) && !defined(_RWSTD_ALLOCATOR) |
| 259 | // libCStd on some platforms have a different allocate/deallocate interface. |
| 260 | // Caller pre-bakes sizeof(T) into 'n' which is the number of bytes to be |
| 261 | // allocated, not the number of elements. |
| 262 | void* allocate(size_type n) { |
| 263 | return getAllocator().allocate(n); |
| 264 | } |
| 265 | void* allocate(size_type n, const void*) { |
| 266 | return getAllocator().allocate(n); |
| 267 | } |
| 268 | void deallocate(void*, size_type) {} |
| 269 | #else |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 270 | pointer allocate(size_type n) { |
| 271 | return reinterpret_cast<pointer>(getAllocator().allocate(n * sizeof(T))); |
| 272 | } |
| 273 | pointer allocate(size_type n, const void*) { |
| 274 | return reinterpret_cast<pointer>(getAllocator().allocate(n * sizeof(T))); |
| 275 | } |
alokp@chromium.org | b416e70 | 2010-08-09 22:32:56 +0000 | [diff] [blame] | 276 | void deallocate(pointer, size_type) {} |
| 277 | #endif // _RWSTD_ALLOCATOR |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 278 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 279 | void construct(pointer p, const T& val) { new ((void *)p) T(val); } |
| 280 | void destroy(pointer p) { p->T::~T(); } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 281 | |
Dmitry Skiba | 0197111 | 2015-07-10 14:54:00 -0400 | [diff] [blame] | 282 | bool operator==(const pool_allocator& rhs) const { return true; } |
| 283 | bool operator!=(const pool_allocator& rhs) const { return false; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 284 | |
alokp@chromium.org | b1e8c6f | 2010-05-12 18:35:53 +0000 | [diff] [blame] | 285 | size_type max_size() const { return static_cast<size_type>(-1) / sizeof(T); } |
| 286 | size_type max_size(int size) const { return static_cast<size_type>(-1) / size; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 287 | |
Dmitry Skiba | 0197111 | 2015-07-10 14:54:00 -0400 | [diff] [blame] | 288 | TPoolAllocator& getAllocator() const { return *GetGlobalPoolAllocator(); } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 291 | #endif // COMPILER_TRANSLATOR_POOLALLOC_H_ |