| 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 "src/gpu/GrBlockAllocator.h" |
| 12 | |
| 13 | #ifdef SK_DEBUG |
| 14 | #include "include/private/SkTHash.h" |
| 15 | #endif |
| 16 | |
| 17 | /** |
| 18 | * Allocates memory in blocks and parcels out space in the blocks for allocation requests. It is |
| 19 | * optimized for allocate / release speed over memory efficiency. The interface is designed to be |
| 20 | * used to implement operator new and delete overrides. All allocations are expected to be released |
| 21 | * before the pool's destructor is called. Allocations will be aligned to sizeof(std::max_align_t). |
| 22 | * |
| 23 | * All allocated objects must be released back to the memory pool before it can be destroyed. |
| 24 | */ |
| 25 | class GrMemoryPool { |
| 26 | public: |
| 27 | #ifdef SK_FORCE_8_BYTE_ALIGNMENT |
| 28 | // This is an issue for WASM builds using emscripten, which had |
| 29 | // std::max_align_t = 16, but was returning pointers only aligned to 8 |
| 30 | // bytes. https://github.com/emscripten-core/emscripten/issues/10072 |
| 31 | // Since Skia does not use "long double" (16 bytes), we should be ok to |
| 32 | // force it back to 8 bytes until emscripten is fixed. |
| 33 | static constexpr size_t kAlignment = 8; |
| 34 | #else |
| 35 | // Guaranteed alignment of pointer returned by allocate(). |
| 36 | static constexpr size_t kAlignment = alignof(std::max_align_t); |
| 37 | #endif |
| 38 | |
| 39 | // Smallest block size allocated on the heap (not the smallest reservation via allocate()). |
| 40 | static constexpr size_t kMinAllocationSize = 1 << 10; |
| 41 | |
| 42 | /** |
| 43 | * Prealloc size is the amount of space to allocate at pool creation |
| 44 | * time and keep around until pool destruction. The min alloc size is |
| 45 | * the smallest allowed size of additional allocations. Both sizes are |
| 46 | * adjusted to ensure that they are at least as large as kMinAllocationSize |
| 47 | * and less than GrBlockAllocator::kMaxAllocationSize. |
| 48 | * |
| 49 | * Both sizes are what the pool will end up allocating from the system, and |
| 50 | * portions of the allocated memory is used for internal bookkeeping. |
| 51 | */ |
| 52 | static std::unique_ptr<GrMemoryPool> Make(size_t preallocSize, size_t minAllocSize); |
| 53 | |
| 54 | ~GrMemoryPool(); |
| 55 | void operator delete(void* p) { ::operator delete(p); } |
| 56 | |
| 57 | /** |
| 58 | * Allocates memory. The memory must be freed with release() before the GrMemoryPool is deleted. |
| 59 | */ |
| 60 | void* allocate(size_t size); |
| 61 | /** |
| 62 | * p must have been returned by allocate(). |
| 63 | */ |
| 64 | void release(void* p); |
| 65 | |
| 66 | /** |
| 67 | * Returns true if there are no unreleased allocations. |
| 68 | */ |
| 69 | bool isEmpty() const { |
| 70 | // If size is the same as preallocSize, there aren't any heap blocks, so currentBlock() |
| 71 | // is the inline head block. |
| 72 | return 0 == this->size() && 0 == fAllocator.currentBlock()->metadata(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the total allocated size of the GrMemoryPool minus any preallocated amount |
| 77 | */ |
| 78 | size_t size() const { return fAllocator.totalSize() - fAllocator.preallocSize(); } |
| 79 | |
| 80 | /** |
| 81 | * Returns the preallocated size of the GrMemoryPool |
| 82 | */ |
| 83 | size_t preallocSize() const { |
| 84 | // Account for the debug-only fields in this count, the offset is 0 for release builds |
| 85 | return offsetof(GrMemoryPool, fAllocator) + fAllocator.preallocSize(); |
| 86 | } |
| 87 | |
| 88 | #ifdef SK_DEBUG |
| 89 | void validate() const; |
| 90 | #endif |
| 91 | |
| 92 | private: |
| 93 | // Per-allocation overhead so that GrMemoryPool can always identify the block owning each and |
| 94 | // release all occupied bytes, including any resulting from alignment padding. |
| 95 | struct { |
| 96 | #ifdef SK_DEBUG |
| 97 | int ; // known value to check for memory stomping (e.g., (CD)*) |
| 98 | int ; // ID that can be used to track down leaks by clients. |
| 99 | #endif |
| 100 | int ; |
| 101 | int ; |
| 102 | }; |
| 103 | |
| 104 | GrMemoryPool(size_t preallocSize, size_t minAllocSize); |
| 105 | |
| 106 | #ifdef SK_DEBUG |
| 107 | SkTHashSet<int> fAllocatedIDs; |
| 108 | int fAllocationCount; |
| 109 | #endif |
| 110 | |
| 111 | GrBlockAllocator fAllocator; // Must be the last field, in order to use extra allocated space |
| 112 | |
| 113 | friend class GrOpMemoryPool; |
| 114 | }; |
| 115 | |
| 116 | class GrOp; |
| 117 | |
| 118 | class GrOpMemoryPool { |
| 119 | public: |
| 120 | static std::unique_ptr<GrOpMemoryPool> Make(size_t preallocSize, size_t minAllocSize); |
| 121 | void operator delete(void* p) { ::operator delete(p); } |
| 122 | |
| 123 | template <typename Op, typename... OpArgs> |
| 124 | std::unique_ptr<Op> allocate(OpArgs&&... opArgs) { |
| 125 | auto mem = this->allocate(sizeof(Op)); |
| 126 | return std::unique_ptr<Op>(new (mem) Op(std::forward<OpArgs>(opArgs)...)); |
| 127 | } |
| 128 | |
| 129 | void* allocate(size_t size) { return fPool.allocate(size); } |
| 130 | |
| 131 | void release(std::unique_ptr<GrOp> op); |
| 132 | |
| 133 | bool isEmpty() const { return fPool.isEmpty(); } |
| 134 | |
| 135 | private: |
| 136 | GrOpMemoryPool(size_t preallocSize, size_t minAllocSize) |
| 137 | : fPool(preallocSize - offsetof(GrOpMemoryPool, fPool), minAllocSize) {} |
| 138 | |
| 139 | GrMemoryPool fPool; // Must be the last field |
| 140 | }; |
| 141 | |
| 142 | #endif |
| 143 | |