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