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