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