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" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 12 | |
| 13 | #include "SkRefCnt.h" |
| 14 | |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 15 | #ifdef SK_DEBUG |
| 16 | #include "SkTHash.h" |
| 17 | #endif |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * Allocates memory in blocks and parcels out space in the blocks for allocation |
| 21 | * requests. It is optimized for allocate / release speed over memory |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 22 | * 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] | 23 | * and delete overrides. All allocations are expected to be released before the |
| 24 | * pool's destructor is called. Allocations will be 8-byte aligned. |
| 25 | */ |
| 26 | class GrMemoryPool { |
| 27 | public: |
| 28 | /** |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 29 | * Prealloc size is the amount of space to allocate at pool creation |
| 30 | * time and keep around until pool destruction. The min alloc size is |
| 31 | * the smallest allowed size of additional allocations. Both sizes are |
| 32 | * adjusted to ensure that: |
| 33 | * 1. they are are 8-byte aligned |
| 34 | * 2. minAllocSize >= kSmallestMinAllocSize |
| 35 | * 3. preallocSize >= minAllocSize |
| 36 | * |
| 37 | * Both sizes is what the pool will end up allocating from the system, and |
| 38 | * portions of the allocated memory is used for internal bookkeeping. |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 39 | */ |
| 40 | GrMemoryPool(size_t preallocSize, size_t minAllocSize); |
| 41 | |
| 42 | ~GrMemoryPool(); |
| 43 | |
| 44 | /** |
| 45 | * Allocates memory. The memory must be freed with release(). |
| 46 | */ |
| 47 | void* allocate(size_t size); |
| 48 | |
| 49 | /** |
| 50 | * p must have been returned by allocate() |
| 51 | */ |
| 52 | void release(void* p); |
| 53 | |
| 54 | /** |
| 55 | * Returns true if there are no unreleased allocations. |
| 56 | */ |
| 57 | bool isEmpty() const { return fTail == fHead && !fHead->fLiveCount; } |
| 58 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 59 | /** |
joshualitt | 22c6f5c | 2016-01-05 08:07:18 -0800 | [diff] [blame] | 60 | * Returns the total allocated size of the GrMemoryPool minus any preallocated amount |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 61 | */ |
| 62 | size_t size() const { return fSize; } |
| 63 | |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 64 | /** |
| 65 | * Returns the preallocated size of the GrMemoryPool |
| 66 | */ |
| 67 | size_t preallocSize() const { return fHead->fSize; } |
| 68 | |
| 69 | /** |
| 70 | * Minimum value of minAllocSize constructor argument. |
| 71 | */ |
| 72 | constexpr static size_t kSmallestMinAllocSize = 1 << 10; |
| 73 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 74 | private: |
| 75 | struct BlockHeader; |
| 76 | |
commit-bot@chromium.org | 8743b8f | 2013-08-01 14:23:33 +0000 | [diff] [blame] | 77 | static BlockHeader* CreateBlock(size_t size); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 78 | |
commit-bot@chromium.org | 8743b8f | 2013-08-01 14:23:33 +0000 | [diff] [blame] | 79 | static void DeleteBlock(BlockHeader* block); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 80 | |
| 81 | void validate(); |
| 82 | |
| 83 | struct BlockHeader { |
robertphillips | b7f4b8e | 2016-01-07 10:12:16 -0800 | [diff] [blame] | 84 | #ifdef SK_DEBUG |
| 85 | uint32_t fBlockSentinal; ///< known value to check for bad back pointers to blocks |
| 86 | #endif |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 87 | BlockHeader* fNext; ///< doubly-linked list of blocks. |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 88 | BlockHeader* fPrev; |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 89 | int fLiveCount; ///< number of outstanding allocations in the |
| 90 | ///< block. |
| 91 | intptr_t fCurrPtr; ///< ptr to the start of blocks free space. |
| 92 | intptr_t fPrevPtr; ///< ptr to the last allocation made |
| 93 | size_t fFreeSize; ///< amount of free space left in the block. |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 94 | size_t fSize; ///< total allocated size of the block |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 97 | static const uint32_t kAssignedMarker = 0xCDCDCDCD; |
| 98 | static const uint32_t kFreedMarker = 0xEFEFEFEF; |
| 99 | |
| 100 | struct AllocHeader { |
| 101 | #ifdef SK_DEBUG |
| 102 | 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] | 103 | 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] | 104 | #endif |
| 105 | BlockHeader* fHeader; ///< pointer back to the block header in which an alloc resides |
| 106 | }; |
| 107 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 108 | size_t fSize; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 109 | size_t fMinAllocSize; |
| 110 | BlockHeader* fHead; |
| 111 | BlockHeader* fTail; |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 112 | #ifdef SK_DEBUG |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 113 | int fAllocationCnt; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 114 | int fAllocBlockCnt; |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 115 | SkTHashSet<int32_t> fAllocatedIDs; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 116 | #endif |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 117 | |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 118 | public: |
| 119 | constexpr static size_t kBlockHeaderSize = sizeof(BlockHeader); |
| 120 | |
| 121 | constexpr static size_t kAllocHeaderSize = sizeof(AllocHeader); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 124 | class GrOp; |
| 125 | |
| 126 | // DDL TODO: for the DLL use case this could probably be the non-intrinsic-based style of |
| 127 | // ref counting |
| 128 | class GrOpMemoryPool : public SkRefCnt { |
| 129 | public: |
| 130 | GrOpMemoryPool(size_t preallocSize, size_t minAllocSize) |
| 131 | : fMemoryPool(preallocSize, minAllocSize) { |
| 132 | } |
| 133 | |
| 134 | template <typename Op, typename... OpArgs> |
| 135 | std::unique_ptr<Op> allocate(OpArgs&&... opArgs) { |
| 136 | char* mem = (char*) fMemoryPool.allocate(sizeof(Op)); |
| 137 | return std::unique_ptr<Op>(new (mem) Op(std::forward<OpArgs>(opArgs)...)); |
| 138 | } |
| 139 | |
| 140 | void* allocate(size_t size) { |
| 141 | return fMemoryPool.allocate(size); |
| 142 | } |
| 143 | |
| 144 | void release(std::unique_ptr<GrOp> op); |
| 145 | |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 146 | bool isEmpty() const { return fMemoryPool.isEmpty(); } |
| 147 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 148 | private: |
| 149 | GrMemoryPool fMemoryPool; |
| 150 | }; |
| 151 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 152 | #endif |