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