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