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