bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrMemoryPool_DEFINED |
| 9 | #define GrMemoryPool_DEFINED |
| 10 | |
| 11 | #include "GrTypes.h" |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 12 | #ifdef SK_DEBUG |
| 13 | #include "SkTHash.h" |
| 14 | #endif |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * Allocates memory in blocks and parcels out space in the blocks for allocation |
| 18 | * requests. It is optimized for allocate / release speed over memory |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 19 | * efficiency. The interface is designed to be used to implement operator new |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 20 | * and delete overrides. All allocations are expected to be released before the |
| 21 | * pool's destructor is called. Allocations will be 8-byte aligned. |
| 22 | */ |
| 23 | class GrMemoryPool { |
| 24 | public: |
| 25 | /** |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 26 | * Prealloc size is the amount of space to allocate at pool creation |
| 27 | * time and keep around until pool destruction. The min alloc size is |
| 28 | * the smallest allowed size of additional allocations. Both sizes are |
| 29 | * adjusted to ensure that: |
| 30 | * 1. they are are 8-byte aligned |
| 31 | * 2. minAllocSize >= kSmallestMinAllocSize |
| 32 | * 3. preallocSize >= minAllocSize |
| 33 | * |
| 34 | * Both sizes is what the pool will end up allocating from the system, and |
| 35 | * portions of the allocated memory is used for internal bookkeeping. |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 36 | */ |
| 37 | GrMemoryPool(size_t preallocSize, size_t minAllocSize); |
| 38 | |
| 39 | ~GrMemoryPool(); |
| 40 | |
| 41 | /** |
| 42 | * Allocates memory. The memory must be freed with release(). |
| 43 | */ |
| 44 | void* allocate(size_t size); |
| 45 | |
| 46 | /** |
| 47 | * p must have been returned by allocate() |
| 48 | */ |
| 49 | void release(void* p); |
| 50 | |
| 51 | /** |
| 52 | * Returns true if there are no unreleased allocations. |
| 53 | */ |
| 54 | bool isEmpty() const { return fTail == fHead && !fHead->fLiveCount; } |
| 55 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 56 | /** |
joshualitt | 22c6f5c | 2016-01-05 08:07:18 -0800 | [diff] [blame] | 57 | * Returns the total allocated size of the GrMemoryPool minus any preallocated amount |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 58 | */ |
| 59 | size_t size() const { return fSize; } |
| 60 | |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Returns the preallocated size of the GrMemoryPool |
| 63 | */ |
| 64 | size_t preallocSize() const { return fHead->fSize; } |
| 65 | |
| 66 | /** |
| 67 | * Minimum value of minAllocSize constructor argument. |
| 68 | */ |
| 69 | constexpr static size_t kSmallestMinAllocSize = 1 << 10; |
| 70 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 71 | private: |
| 72 | struct BlockHeader; |
| 73 | |
commit-bot@chromium.org | 8743b8f | 2013-08-01 14:23:33 +0000 | [diff] [blame] | 74 | static BlockHeader* CreateBlock(size_t size); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 75 | |
commit-bot@chromium.org | 8743b8f | 2013-08-01 14:23:33 +0000 | [diff] [blame] | 76 | static void DeleteBlock(BlockHeader* block); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 77 | |
| 78 | void validate(); |
| 79 | |
| 80 | struct BlockHeader { |
robertphillips | b7f4b8e | 2016-01-07 10:12:16 -0800 | [diff] [blame] | 81 | #ifdef SK_DEBUG |
| 82 | uint32_t fBlockSentinal; ///< known value to check for bad back pointers to blocks |
| 83 | #endif |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 84 | BlockHeader* fNext; ///< doubly-linked list of blocks. |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 85 | BlockHeader* fPrev; |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 86 | int fLiveCount; ///< number of outstanding allocations in the |
| 87 | ///< block. |
| 88 | intptr_t fCurrPtr; ///< ptr to the start of blocks free space. |
| 89 | intptr_t fPrevPtr; ///< ptr to the last allocation made |
| 90 | size_t fFreeSize; ///< amount of free space left in the block. |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 91 | size_t fSize; ///< total allocated size of the block |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 94 | static const uint32_t kAssignedMarker = 0xCDCDCDCD; |
| 95 | static const uint32_t kFreedMarker = 0xEFEFEFEF; |
| 96 | |
| 97 | struct AllocHeader { |
| 98 | #ifdef SK_DEBUG |
| 99 | uint32_t fSentinal; ///< known value to check for memory stomping (e.g., (CD)*) |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 100 | int32_t fID; ///< ID that can be used to track down leaks by clients. |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 101 | #endif |
| 102 | BlockHeader* fHeader; ///< pointer back to the block header in which an alloc resides |
| 103 | }; |
| 104 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 105 | size_t fSize; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 106 | size_t fMinAllocSize; |
| 107 | BlockHeader* fHead; |
| 108 | BlockHeader* fTail; |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 109 | #ifdef SK_DEBUG |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 110 | int fAllocationCnt; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 111 | int fAllocBlockCnt; |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 112 | SkTHashSet<int32_t> fAllocatedIDs; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 113 | #endif |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 114 | |
| 115 | protected: |
| 116 | enum { |
| 117 | // We assume this alignment is good enough for everybody. |
| 118 | kAlignment = 8, |
| 119 | kHeaderSize = GR_CT_ALIGN_UP(sizeof(BlockHeader), kAlignment), |
| 120 | kPerAllocPad = GR_CT_ALIGN_UP(sizeof(AllocHeader), kAlignment), |
| 121 | }; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 124 | /** |
| 125 | * Variant of GrMemoryPool that can only allocate objects of a single type. It is |
| 126 | * not as flexible as GrMemoryPool, but it has more convenient allocate() method, |
| 127 | * and more importantly, it guarantees number of objects that are preallocated at |
| 128 | * construction or when adding a new memory block. I.e. |
| 129 | * |
| 130 | * GrMemoryPool pool(3 * sizeof(T), 1000 * sizeof(T)); |
| 131 | * pool.allocate(sizeof(T)); |
| 132 | * pool.allocate(sizeof(T)); |
| 133 | * pool.allocate(sizeof(T)); |
| 134 | * |
| 135 | * will preallocate 3 * sizeof(T) bytes and use some of those bytes for internal |
| 136 | * structures. Because of that, last allocate() call will end up allocating a new |
| 137 | * block of 1000 * sizeof(T) bytes. In contrast, |
| 138 | * |
| 139 | * GrObjectMemoryPool<T> pool(3, 1000); |
| 140 | * pool.allocate(); |
| 141 | * pool.allocate(); |
| 142 | * pool.allocate(); |
| 143 | * |
| 144 | * guarantees to preallocate enough memory for 3 objects of sizeof(T), so last |
| 145 | * allocate() will use preallocated memory and won't cause allocation of a new block. |
| 146 | * |
| 147 | * Same thing is true for the second (minAlloc) ctor argument: this class guarantees |
| 148 | * that a newly added block will have enough space for 1000 objects of sizeof(T), while |
| 149 | * GrMemoryPool does not. |
| 150 | */ |
| 151 | template <class T> |
| 152 | class GrObjectMemoryPool: public GrMemoryPool { |
| 153 | public: |
| 154 | /** |
| 155 | * Preallocates memory for preallocCount objects, and sets new block size to be |
| 156 | * enough to hold minAllocCount objects. |
| 157 | */ |
| 158 | GrObjectMemoryPool(size_t preallocCount, size_t minAllocCount) |
| 159 | : GrMemoryPool(CountToSize(preallocCount), |
| 160 | CountToSize(SkTMax(minAllocCount, kSmallestMinAllocCount))) { |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Allocates memory for an object, but doesn't construct or otherwise initialize it. |
| 165 | * The memory must be freed with release(). |
| 166 | */ |
| 167 | T* allocate() { return static_cast<T*>(GrMemoryPool::allocate(sizeof(T))); } |
| 168 | |
| 169 | private: |
| 170 | constexpr static size_t kTotalObjectSize = |
| 171 | kPerAllocPad + GR_CT_ALIGN_UP(sizeof(T), kAlignment); |
| 172 | |
| 173 | constexpr static size_t CountToSize(size_t count) { |
| 174 | return kHeaderSize + count * kTotalObjectSize; |
| 175 | } |
| 176 | |
| 177 | public: |
| 178 | /** |
| 179 | * Minimum value of minAllocCount constructor argument. |
| 180 | */ |
| 181 | constexpr static size_t kSmallestMinAllocCount = |
| 182 | (GrMemoryPool::kSmallestMinAllocSize - kHeaderSize + kTotalObjectSize - 1) / |
| 183 | kTotalObjectSize; |
| 184 | }; |
| 185 | |
| 186 | template <class T> |
| 187 | constexpr size_t GrObjectMemoryPool<T>::kSmallestMinAllocCount; |
| 188 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 189 | #endif |