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 | #include "GrMemoryPool.h" |
Herb Derby | b549cc3 | 2017-03-27 13:35:15 -0400 | [diff] [blame] | 9 | #include "SkMalloc.h" |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 10 | #ifdef SK_DEBUG |
| 11 | #include "SkAtomics.h" |
| 12 | #endif |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 13 | #include "ops/GrOp.h" |
Herb Derby | d7b34a5 | 2017-03-20 11:19:23 -0400 | [diff] [blame] | 14 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 15 | #ifdef SK_DEBUG |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 16 | #define VALIDATE this->validate() |
| 17 | #else |
| 18 | #define VALIDATE |
| 19 | #endif |
| 20 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 21 | void GrOpMemoryPool::release(std::unique_ptr<GrOp> op) { |
| 22 | GrOp* tmp = op.release(); |
| 23 | SkASSERT(tmp); |
| 24 | tmp->~GrOp(); |
| 25 | fMemoryPool.release(tmp); |
| 26 | } |
| 27 | |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 28 | constexpr size_t GrMemoryPool::kSmallestMinAllocSize; |
| 29 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 30 | GrMemoryPool::GrMemoryPool(size_t preallocSize, size_t minAllocSize) { |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 31 | SkDEBUGCODE(fAllocationCnt = 0); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 32 | SkDEBUGCODE(fAllocBlockCnt = 0); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 33 | |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 34 | minAllocSize = SkTMax<size_t>(GrSizeAlignUp(minAllocSize, kAlignment), kSmallestMinAllocSize); |
| 35 | preallocSize = SkTMax<size_t>(GrSizeAlignUp(preallocSize, kAlignment), minAllocSize); |
| 36 | |
| 37 | fMinAllocSize = minAllocSize; |
joshualitt | 22c6f5c | 2016-01-05 08:07:18 -0800 | [diff] [blame] | 38 | fSize = 0; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 39 | |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 40 | fHead = CreateBlock(preallocSize); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 41 | fTail = fHead; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | fHead->fNext = nullptr; |
| 43 | fHead->fPrev = nullptr; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 44 | VALIDATE; |
| 45 | }; |
| 46 | |
| 47 | GrMemoryPool::~GrMemoryPool() { |
| 48 | VALIDATE; |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 49 | #ifdef SK_DEBUG |
| 50 | int i = 0; |
| 51 | int n = fAllocatedIDs.count(); |
| 52 | fAllocatedIDs.foreach([&i, n] (int32_t id) { |
| 53 | if (++i == 1) { |
| 54 | SkDebugf("Leaked IDs (in no particular order): %d", id); |
| 55 | } else if (i < 11) { |
| 56 | SkDebugf(", %d%s", id, (n == i ? "\n" : "")); |
| 57 | } else if (i == 11) { |
| 58 | SkDebugf(", ...\n"); |
| 59 | } |
| 60 | }); |
| 61 | #endif |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 62 | SkASSERT(0 == fAllocationCnt); |
| 63 | SkASSERT(fHead == fTail); |
| 64 | SkASSERT(0 == fHead->fLiveCount); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 65 | DeleteBlock(fHead); |
| 66 | }; |
| 67 | |
| 68 | void* GrMemoryPool::allocate(size_t size) { |
| 69 | VALIDATE; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 70 | size += kPerAllocPad; |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 71 | size = GrSizeAlignUp(size, kAlignment); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 72 | if (fTail->fFreeSize < size) { |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 73 | size_t blockSize = size + kHeaderSize; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 74 | blockSize = SkTMax<size_t>(blockSize, fMinAllocSize); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 75 | BlockHeader* block = CreateBlock(blockSize); |
| 76 | |
| 77 | block->fPrev = fTail; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | block->fNext = nullptr; |
| 79 | SkASSERT(nullptr == fTail->fNext); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 80 | fTail->fNext = block; |
| 81 | fTail = block; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 82 | fSize += block->fSize; |
| 83 | SkDEBUGCODE(++fAllocBlockCnt); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 84 | } |
robertphillips | b7f4b8e | 2016-01-07 10:12:16 -0800 | [diff] [blame] | 85 | SkASSERT(kAssignedMarker == fTail->fBlockSentinal); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 86 | SkASSERT(fTail->fFreeSize >= size); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 87 | intptr_t ptr = fTail->fCurrPtr; |
| 88 | // We stash a pointer to the block header, just before the allocated space, |
| 89 | // so that we can decrement the live count on delete in constant time. |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 90 | AllocHeader* allocData = reinterpret_cast<AllocHeader*>(ptr); |
| 91 | SkDEBUGCODE(allocData->fSentinal = kAssignedMarker); |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 92 | SkDEBUGCODE(allocData->fID = []{static int32_t gID; return sk_atomic_inc(&gID) + 1;}()); |
| 93 | // You can set a breakpoint here when a leaked ID is allocated to see the stack frame. |
| 94 | SkDEBUGCODE(fAllocatedIDs.add(allocData->fID)); |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 95 | allocData->fHeader = fTail; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 96 | ptr += kPerAllocPad; |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 97 | fTail->fPrevPtr = fTail->fCurrPtr; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 98 | fTail->fCurrPtr += size; |
| 99 | fTail->fFreeSize -= size; |
| 100 | fTail->fLiveCount += 1; |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 101 | SkDEBUGCODE(++fAllocationCnt); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 102 | VALIDATE; |
| 103 | return reinterpret_cast<void*>(ptr); |
| 104 | } |
| 105 | |
| 106 | void GrMemoryPool::release(void* p) { |
| 107 | VALIDATE; |
| 108 | intptr_t ptr = reinterpret_cast<intptr_t>(p) - kPerAllocPad; |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 109 | AllocHeader* allocData = reinterpret_cast<AllocHeader*>(ptr); |
| 110 | SkASSERT(kAssignedMarker == allocData->fSentinal); |
| 111 | SkDEBUGCODE(allocData->fSentinal = kFreedMarker); |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 112 | SkDEBUGCODE(fAllocatedIDs.remove(allocData->fID)); |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 113 | BlockHeader* block = allocData->fHeader; |
robertphillips | b7f4b8e | 2016-01-07 10:12:16 -0800 | [diff] [blame] | 114 | SkASSERT(kAssignedMarker == block->fBlockSentinal); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 115 | if (1 == block->fLiveCount) { |
| 116 | // the head block is special, it is reset rather than deleted |
| 117 | if (fHead == block) { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 118 | fHead->fCurrPtr = reinterpret_cast<intptr_t>(fHead) + kHeaderSize; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 119 | fHead->fLiveCount = 0; |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 120 | fHead->fFreeSize = fHead->fSize - kHeaderSize; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 121 | } else { |
| 122 | BlockHeader* prev = block->fPrev; |
| 123 | BlockHeader* next = block->fNext; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 124 | SkASSERT(prev); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 125 | prev->fNext = next; |
| 126 | if (next) { |
| 127 | next->fPrev = prev; |
| 128 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 129 | SkASSERT(fTail == block); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 130 | fTail = prev; |
| 131 | } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 132 | fSize -= block->fSize; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 133 | DeleteBlock(block); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 134 | SkDEBUGCODE(fAllocBlockCnt--); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 135 | } |
| 136 | } else { |
| 137 | --block->fLiveCount; |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 138 | // Trivial reclaim: if we're releasing the most recent allocation, reuse it |
| 139 | if (block->fPrevPtr == ptr) { |
| 140 | block->fFreeSize += (block->fCurrPtr - block->fPrevPtr); |
| 141 | block->fCurrPtr = block->fPrevPtr; |
| 142 | } |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 143 | } |
commit-bot@chromium.org | 1acc3d7 | 2013-09-06 23:13:05 +0000 | [diff] [blame] | 144 | SkDEBUGCODE(--fAllocationCnt); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 145 | VALIDATE; |
| 146 | } |
| 147 | |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 148 | GrMemoryPool::BlockHeader* GrMemoryPool::CreateBlock(size_t blockSize) { |
| 149 | blockSize = SkTMax<size_t>(blockSize, kHeaderSize); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 150 | BlockHeader* block = |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 151 | reinterpret_cast<BlockHeader*>(sk_malloc_throw(blockSize)); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 152 | // we assume malloc gives us aligned memory |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 153 | SkASSERT(!(reinterpret_cast<intptr_t>(block) % kAlignment)); |
robertphillips | b7f4b8e | 2016-01-07 10:12:16 -0800 | [diff] [blame] | 154 | SkDEBUGCODE(block->fBlockSentinal = kAssignedMarker); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 155 | block->fLiveCount = 0; |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 156 | block->fFreeSize = blockSize - kHeaderSize; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 157 | block->fCurrPtr = reinterpret_cast<intptr_t>(block) + kHeaderSize; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 158 | block->fPrevPtr = 0; // gcc warns on assigning nullptr to an intptr_t. |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 159 | block->fSize = blockSize; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 160 | return block; |
| 161 | } |
| 162 | |
| 163 | void GrMemoryPool::DeleteBlock(BlockHeader* block) { |
robertphillips | b7f4b8e | 2016-01-07 10:12:16 -0800 | [diff] [blame] | 164 | SkASSERT(kAssignedMarker == block->fBlockSentinal); |
| 165 | SkDEBUGCODE(block->fBlockSentinal = kFreedMarker); // FWIW |
reed@google.com | 939ca7c | 2013-09-26 19:56:51 +0000 | [diff] [blame] | 166 | sk_free(block); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void GrMemoryPool::validate() { |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 170 | #ifdef SK_DEBUG |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 171 | BlockHeader* block = fHead; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 172 | BlockHeader* prev = nullptr; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 173 | SkASSERT(block); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 174 | int allocCount = 0; |
| 175 | do { |
robertphillips | b7f4b8e | 2016-01-07 10:12:16 -0800 | [diff] [blame] | 176 | SkASSERT(kAssignedMarker == block->fBlockSentinal); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 177 | allocCount += block->fLiveCount; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 178 | SkASSERT(prev == block->fPrev); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 179 | if (prev) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 180 | SkASSERT(prev->fNext == block); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | intptr_t b = reinterpret_cast<intptr_t>(block); |
| 184 | size_t ptrOffset = block->fCurrPtr - b; |
| 185 | size_t totalSize = ptrOffset + block->fFreeSize; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 186 | intptr_t userStart = b + kHeaderSize; |
| 187 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 188 | SkASSERT(!(b % kAlignment)); |
| 189 | SkASSERT(!(totalSize % kAlignment)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 190 | SkASSERT(!(block->fCurrPtr % kAlignment)); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 191 | if (fHead != block) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 192 | SkASSERT(block->fLiveCount); |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 193 | SkASSERT(totalSize >= fMinAllocSize); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 194 | } else { |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 195 | SkASSERT(totalSize == block->fSize); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 196 | } |
| 197 | if (!block->fLiveCount) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 198 | SkASSERT(ptrOffset == kHeaderSize); |
| 199 | SkASSERT(userStart == block->fCurrPtr); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 200 | } else { |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 201 | AllocHeader* allocData = reinterpret_cast<AllocHeader*>(userStart); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 202 | SkASSERT(allocData->fSentinal == kAssignedMarker || |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 203 | allocData->fSentinal == kFreedMarker); |
| 204 | SkASSERT(block == allocData->fHeader); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 205 | } |
robertphillips | 19c6250 | 2016-01-06 07:04:46 -0800 | [diff] [blame] | 206 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 207 | prev = block; |
| 208 | } while ((block = block->fNext)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 209 | SkASSERT(allocCount == fAllocationCnt); |
Brian Salomon | a5002c3 | 2017-03-28 16:51:02 -0400 | [diff] [blame] | 210 | SkASSERT(fAllocationCnt == fAllocatedIDs.count()); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 211 | SkASSERT(prev == fTail); |
joshualitt | 22c6f5c | 2016-01-05 08:07:18 -0800 | [diff] [blame] | 212 | SkASSERT(fAllocBlockCnt != 0 || fSize == 0); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 213 | #endif |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 214 | } |