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