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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrMemoryPool.h" |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 9 | |
Mike Klein | 8aa0edf | 2020-10-16 11:04:18 -0500 | [diff] [blame] | 10 | #include "include/private/SkTPin.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/ops/GrOp.h" |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 12 | |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 13 | #ifdef SK_DEBUG |
| 14 | #include <atomic> |
| 15 | #endif |
Herb Derby | d7b34a5 | 2017-03-20 11:19:23 -0400 | [diff] [blame] | 16 | |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 17 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 18 | |
Brian Salomon | 6986c65 | 2019-12-12 10:58:47 -0500 | [diff] [blame] | 19 | std::unique_ptr<GrMemoryPool> GrMemoryPool::Make(size_t preallocSize, size_t minAllocSize) { |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 20 | static_assert(sizeof(GrMemoryPool) < GrMemoryPool::kMinAllocationSize); |
| 21 | |
| 22 | preallocSize = SkTPin(preallocSize, kMinAllocationSize, |
| 23 | (size_t) GrBlockAllocator::kMaxAllocationSize); |
| 24 | minAllocSize = SkTPin(minAllocSize, kMinAllocationSize, |
| 25 | (size_t) GrBlockAllocator::kMaxAllocationSize); |
| 26 | void* mem = operator new(preallocSize); |
| 27 | return std::unique_ptr<GrMemoryPool>(new (mem) GrMemoryPool(preallocSize, minAllocSize)); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 28 | } |
| 29 | |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 30 | GrMemoryPool::GrMemoryPool(size_t preallocSize, size_t minAllocSize) |
| 31 | : fAllocator(GrBlockAllocator::GrowthPolicy::kFixed, minAllocSize, |
| 32 | preallocSize - offsetof(GrMemoryPool, fAllocator) - sizeof(GrBlockAllocator)) { |
| 33 | SkDEBUGCODE(fAllocationCount = 0;) |
| 34 | } |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 35 | |
| 36 | GrMemoryPool::~GrMemoryPool() { |
John Stiles | 244ebf7 | 2020-10-28 11:08:06 -0400 | [diff] [blame^] | 37 | this->reportLeaks(); |
| 38 | SkASSERT(0 == fAllocationCount); |
| 39 | SkASSERT(this->isEmpty()); |
| 40 | } |
| 41 | |
| 42 | void GrMemoryPool::reportLeaks() const { |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 43 | #ifdef SK_DEBUG |
| 44 | int i = 0; |
| 45 | int n = fAllocatedIDs.count(); |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 46 | fAllocatedIDs.foreach([&i, n] (int id) { |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 47 | if (++i == 1) { |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 48 | SkDebugf("Leaked %d IDs (in no particular order): %d%s", n, id, (n == i) ? "\n" : ""); |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 49 | } else if (i < 11) { |
| 50 | SkDebugf(", %d%s", id, (n == i ? "\n" : "")); |
| 51 | } else if (i == 11) { |
| 52 | SkDebugf(", ...\n"); |
| 53 | } |
| 54 | }); |
| 55 | #endif |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 56 | } |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 57 | |
| 58 | void* GrMemoryPool::allocate(size_t size) { |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 59 | static_assert(alignof(Header) <= kAlignment); |
| 60 | SkDEBUGCODE(this->validate();) |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 61 | |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 62 | GrBlockAllocator::ByteRange alloc = fAllocator.allocate<kAlignment, sizeof(Header)>(size); |
| 63 | |
| 64 | // Initialize GrMemoryPool's custom header at the start of the allocation |
| 65 | Header* header = static_cast<Header*>(alloc.fBlock->ptr(alloc.fAlignedOffset - sizeof(Header))); |
| 66 | header->fStart = alloc.fStart; |
| 67 | header->fEnd = alloc.fEnd; |
| 68 | |
| 69 | // Update live count within the block |
| 70 | alloc.fBlock->setMetadata(alloc.fBlock->metadata() + 1); |
| 71 | |
| 72 | #ifdef SK_DEBUG |
| 73 | header->fSentinel = GrBlockAllocator::kAssignedMarker; |
| 74 | header->fID = []{ |
| 75 | static std::atomic<int> nextID{1}; |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 76 | return nextID++; |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 77 | }(); |
| 78 | |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 79 | // You can set a breakpoint here when a leaked ID is allocated to see the stack frame. |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 80 | fAllocatedIDs.add(header->fID); |
| 81 | fAllocationCount++; |
| 82 | #endif |
| 83 | |
| 84 | // User-facing pointer is after the header padding |
| 85 | return alloc.fBlock->ptr(alloc.fAlignedOffset); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void GrMemoryPool::release(void* p) { |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 89 | // NOTE: if we needed it, (p - block) would equal the original alignedOffset value returned by |
| 90 | // GrBlockAllocator::allocate() |
| 91 | Header* header = reinterpret_cast<Header*>(reinterpret_cast<intptr_t>(p) - sizeof(Header)); |
| 92 | SkASSERT(GrBlockAllocator::kAssignedMarker == header->fSentinel); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 93 | |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 94 | GrBlockAllocator::Block* block = fAllocator.owningBlock<kAlignment>(header, header->fStart); |
Brian Salomon | 6986c65 | 2019-12-12 10:58:47 -0500 | [diff] [blame] | 95 | |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 96 | #ifdef SK_DEBUG |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 97 | header->fSentinel = GrBlockAllocator::kFreedMarker; |
| 98 | fAllocatedIDs.remove(header->fID); |
| 99 | fAllocationCount--; |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 100 | #endif |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 101 | |
| 102 | int alive = block->metadata(); |
| 103 | if (alive == 1) { |
| 104 | // This was last allocation in the block, so remove it |
| 105 | fAllocator.releaseBlock(block); |
| 106 | } else { |
| 107 | // Update count and release storage of the allocation itself |
| 108 | block->setMetadata(alive - 1); |
| 109 | block->release(header->fStart, header->fEnd); |
| 110 | } |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 111 | } |
Brian Salomon | 6986c65 | 2019-12-12 10:58:47 -0500 | [diff] [blame] | 112 | |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 113 | #ifdef SK_DEBUG |
| 114 | void GrMemoryPool::validate() const { |
| 115 | fAllocator.validate(); |
Brian Salomon | 6986c65 | 2019-12-12 10:58:47 -0500 | [diff] [blame] | 116 | |
Michael Ludwig | cd01979 | 2020-03-17 10:14:48 -0400 | [diff] [blame] | 117 | int allocCount = 0; |
| 118 | for (const auto* b : fAllocator.blocks()) { |
| 119 | allocCount += b->metadata(); |
| 120 | } |
| 121 | SkASSERT(allocCount == fAllocationCount); |
| 122 | SkASSERT(fAllocationCount == fAllocatedIDs.count()); |
| 123 | SkASSERT(allocCount > 0 || this->isEmpty()); |
| 124 | } |
| 125 | #endif |