blob: ad63bc4cd66aa89784f6c6e410742255240130fe [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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 Lang0a73dd82014-11-19 16:18:08 -05007#ifndef COMPILER_TRANSLATOR_POOLALLOC_H_
8#define COMPILER_TRANSLATOR_POOLALLOC_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009
10#ifdef _DEBUG
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +000011#define GUARD_BLOCKS // define to enable guard block sanity checking
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012#endif
13
14//
15// This header defines an allocator that can be used to efficiently
Jamie Madilld7b1ab52016-12-12 14:42:19 -050016// allocate a large number of small requests for heap memory, with the
17// intention that they are not individually deallocated, but rather
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018// 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.orgcff1aff2010-05-14 19:24:22 +000035#include <string.h>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036#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.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050041//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042
Jamie Madilld7b1ab52016-12-12 14:42:19 -050043class TAllocation
44{
45 public:
46 TAllocation(size_t size, unsigned char *mem, TAllocation *prev = 0)
47 : size(size), mem(mem), prevAlloc(prev)
48 {
49// Allocations are bracketed:
50// [allocationHeader][initialGuardBlock][userData][finalGuardBlock]
51// This would be cleaner with if (guardBlockSize)..., but that
52// makes the compiler print warnings about 0 length memsets,
53// even with the if() protecting them.
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +000054#ifdef GUARD_BLOCKS
55 memset(preGuard(), guardBlockBeginVal, guardBlockSize);
Jamie Madilld7b1ab52016-12-12 14:42:19 -050056 memset(data(), userDataFill, size);
57 memset(postGuard(), guardBlockEndVal, guardBlockSize);
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +000058#endif
59 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000060
Jamie Madilld7b1ab52016-12-12 14:42:19 -050061 void check() const
62 {
63 checkGuardBlock(preGuard(), guardBlockBeginVal, "before");
64 checkGuardBlock(postGuard(), guardBlockEndVal, "after");
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +000065 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +000067 void checkAllocList() const;
68
69 // Return total size needed to accomodate user buffer of 'size',
70 // plus our tracking data.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050071 inline static size_t allocationSize(size_t size)
72 {
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +000073 return size + 2 * guardBlockSize + headerSize();
74 }
75
76 // Offset from surrounding buffer to get to user data buffer.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050077 inline static unsigned char *offsetAllocation(unsigned char *m)
78 {
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +000079 return m + guardBlockSize + headerSize();
80 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081
Jamie Madilld7b1ab52016-12-12 14:42:19 -050082 private:
83 void checkGuardBlock(unsigned char *blockMem, unsigned char val, const char *locText) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +000085 // Find offsets to pre and post guard blocks, and user data buffer
Jamie Madilld7b1ab52016-12-12 14:42:19 -050086 unsigned char *preGuard() const { return mem + headerSize(); }
87 unsigned char *data() const { return preGuard() + guardBlockSize; }
88 unsigned char *postGuard() const { return data() + size; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089
Jamie Madilld7b1ab52016-12-12 14:42:19 -050090 size_t size; // size of the user data area
91 unsigned char *mem; // beginning of our allocation (pts to header)
92 TAllocation *prevAlloc; // prior allocation in the chain
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +000094 // Support MSVC++ 6.0
95 const static unsigned char guardBlockBeginVal;
96 const static unsigned char guardBlockEndVal;
97 const static unsigned char userDataFill;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +000099 const static size_t guardBlockSize;
100#ifdef GUARD_BLOCKS
101 inline static size_t headerSize() { return sizeof(TAllocation); }
102#else
103 inline static size_t headerSize() { return 0; }
104#endif
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105};
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000106
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107//
108// There are several stacks. One is to track the pushing and popping
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500109// of the user, and not yet implemented. The others are simply a
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110// repositories of free pages or used pages.
111//
112// Page stacks are linked together with a simple header at the beginning
113// of each allocation obtained from the underlying OS. Multi-page allocations
114// are returned to the OS. Individual page allocations are kept for future
115// re-use.
116//
117// The "page size" used is not, nor must it match, the underlying OS
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500118// page size. But, having it be about that size or equal to a set of
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119// pages is likely most optimal.
120//
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500121class TPoolAllocator
122{
123 public:
124 TPoolAllocator(int growthIncrement = 8 * 1024, int allocationAlignment = 16);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000126 //
127 // Don't call the destructor just to free up the memory, call pop()
128 //
129 ~TPoolAllocator();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000131 //
132 // Call push() to establish a new place to pop memory too. Does not
133 // have to be called to get things started.
134 //
135 void push();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000137 //
138 // Call pop() to free all memory allocated since the last call to push(),
139 // or if no last call to push, frees all memory since first allocation.
140 //
141 void pop();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000143 //
144 // Call popAll() to free all memory allocated.
145 //
146 void popAll();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000148 //
149 // Call allocate() to actually acquire memory. Returns 0 if no memory
150 // available, otherwise a properly aligned pointer to 'numBytes' of memory.
151 //
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500152 void *allocate(size_t numBytes);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000154 //
155 // There is no deallocate. The point of this class is that
156 // deallocation can be skipped by the user of it, as the model
157 // of use is to simultaneously deallocate everything at once
158 // by calling pop(), and to not have to solve memory leak problems.
159 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160
Jamie Madill438dbcf2016-06-17 14:20:05 -0400161 // Catch unwanted allocations.
162 // TODO(jmadill): Remove this when we remove the global allocator.
163 void lock();
164 void unlock();
165
Corentin Wallez28b65282016-06-16 07:24:50 -0700166 private:
167 size_t alignment; // all returned allocations will be aligned at
168 // this granularity, which will be a power of 2
169 size_t alignmentMask;
170
171#if !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC)
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000172 friend struct tHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500174 struct tHeader
175 {
176 tHeader(tHeader *nextPage, size_t pageCount)
177 : nextPage(nextPage),
178 pageCount(pageCount)
179#ifdef GUARD_BLOCKS
180 ,
181 lastAllocation(0)
182#endif
183 {
184 }
185
186 ~tHeader()
187 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188#ifdef GUARD_BLOCKS
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000189 if (lastAllocation)
190 lastAllocation->checkAllocList();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191#endif
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000192 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500194 tHeader *nextPage;
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000195 size_t pageCount;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196#ifdef GUARD_BLOCKS
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500197 TAllocation *lastAllocation;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000198#endif
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000199 };
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500201 struct tAllocState
202 {
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000203 size_t offset;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500204 tHeader *page;
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000205 };
206 typedef std::vector<tAllocState> tAllocStack;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000208 // Track allocations if and only if we're using guard blocks
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500209 void *initializeAllocation(tHeader *block, unsigned char *memory, size_t numBytes)
210 {
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000211#ifdef GUARD_BLOCKS
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500212 new (memory) TAllocation(numBytes, memory, block->lastAllocation);
213 block->lastAllocation = reinterpret_cast<TAllocation *>(memory);
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000214#endif
215 // This is optimized entirely away if GUARD_BLOCKS is not defined.
216 return TAllocation::offsetAllocation(memory);
217 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500219 size_t pageSize; // granularity of allocation from the OS
220 size_t headerSkip; // amount of memory to skip to make room for the
221 // header (basically, size of header, rounded
222 // up to make it aligned
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000223 size_t currentPageOffset; // next offset in top of inUseList to allocate from
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500224 tHeader *freeList; // list of popped memory
225 tHeader *inUseList; // list of all memory currently being used
226 tAllocStack mStack; // stack of where to allocate from, to partition pool
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500228 int numCalls; // just an interesting statistic
229 size_t totalBytes; // just an interesting statistic
Corentin Wallez28b65282016-06-16 07:24:50 -0700230
231#else // !defined(ANGLE_TRANSLATOR_DISABLE_POOL_ALLOC)
232 std::vector<std::vector<void *>> mStack;
233#endif
234
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500235 TPoolAllocator &operator=(const TPoolAllocator &); // dont allow assignment operator
236 TPoolAllocator(const TPoolAllocator &); // dont allow default copy constructor
Jamie Madill438dbcf2016-06-17 14:20:05 -0400237 bool mLocked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238};
239
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240//
241// There could potentially be many pools with pops happening at
242// different times. But a simple use is to have a global pop
243// with everyone using the same global allocator.
244//
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500245extern TPoolAllocator *GetGlobalPoolAllocator();
246extern void SetGlobalPoolAllocator(TPoolAllocator *poolAllocator);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248//
249// This STL compatible allocator is intended to be used as the allocator
250// parameter to templatized STL containers, like vector and map.
251//
252// It will use the pools for allocation, and not
253// do any deallocation, but will still do destruction.
254//
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500255template <class T>
256class pool_allocator
257{
258 public:
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000259 typedef size_t size_type;
260 typedef ptrdiff_t difference_type;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500261 typedef T *pointer;
262 typedef const T *const_pointer;
263 typedef T &reference;
264 typedef const T &const_reference;
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000265 typedef T value_type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500267 template <class Other>
268 struct rebind
269 {
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000270 typedef pool_allocator<Other> other;
271 };
272 pointer address(reference x) const { return &x; }
273 const_pointer address(const_reference x) const { return &x; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500275 pool_allocator() {}
Dmitry Skiba01971112015-07-10 14:54:00 -0400276
277 template <class Other>
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500278 pool_allocator(const pool_allocator<Other> &p)
279 {
280 }
281
282 template <class Other>
283 pool_allocator<T> &operator=(const pool_allocator<Other> &p)
284 {
285 return *this;
286 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287
alokp@chromium.orgb416e702010-08-09 22:32:56 +0000288#if defined(__SUNPRO_CC) && !defined(_RWSTD_ALLOCATOR)
289 // libCStd on some platforms have a different allocate/deallocate interface.
290 // Caller pre-bakes sizeof(T) into 'n' which is the number of bytes to be
291 // allocated, not the number of elements.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500292 void *allocate(size_type n) { return getAllocator().allocate(n); }
293 void *allocate(size_type n, const void *) { return getAllocator().allocate(n); }
294 void deallocate(void *, size_type) {}
alokp@chromium.orgb416e702010-08-09 22:32:56 +0000295#else
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500296 pointer allocate(size_type n)
297 {
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000298 return reinterpret_cast<pointer>(getAllocator().allocate(n * sizeof(T)));
299 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500300 pointer allocate(size_type n, const void *)
301 {
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000302 return reinterpret_cast<pointer>(getAllocator().allocate(n * sizeof(T)));
303 }
alokp@chromium.orgb416e702010-08-09 22:32:56 +0000304 void deallocate(pointer, size_type) {}
305#endif // _RWSTD_ALLOCATOR
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500307 void construct(pointer p, const T &val) { new ((void *)p) T(val); }
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000308 void destroy(pointer p) { p->T::~T(); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500310 bool operator==(const pool_allocator &rhs) const { return true; }
311 bool operator!=(const pool_allocator &rhs) const { return false; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312
alokp@chromium.orgb1e8c6f2010-05-12 18:35:53 +0000313 size_type max_size() const { return static_cast<size_type>(-1) / sizeof(T); }
314 size_type max_size(int size) const { return static_cast<size_type>(-1) / size; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500316 TPoolAllocator &getAllocator() const { return *GetGlobalPoolAllocator(); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317};
318
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500319#endif // COMPILER_TRANSLATOR_POOLALLOC_H_