blob: 32a361297223546117d91988e6a2c42b610b95b8 [file] [log] [blame]
bsalomon@google.com4da34e32012-06-19 15:40:27 +00001/*
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 Derbyb549cc32017-03-27 13:35:15 -04009#include "SkMalloc.h"
Brian Salomona5002c32017-03-28 16:51:02 -040010#ifdef SK_DEBUG
11#include "SkAtomics.h"
12#endif
Herb Derbyd7b34a52017-03-20 11:19:23 -040013
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000014#ifdef SK_DEBUG
bsalomon@google.com4da34e32012-06-19 15:40:27 +000015 #define VALIDATE this->validate()
16#else
17 #define VALIDATE
18#endif
19
dskibae4cd0062016-11-29 06:50:35 -080020constexpr size_t GrMemoryPool::kSmallestMinAllocSize;
21
bsalomon@google.com4da34e32012-06-19 15:40:27 +000022GrMemoryPool::GrMemoryPool(size_t preallocSize, size_t minAllocSize) {
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000023 SkDEBUGCODE(fAllocationCnt = 0);
joshualittb7133be2015-04-08 09:08:31 -070024 SkDEBUGCODE(fAllocBlockCnt = 0);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000025
dskibae4cd0062016-11-29 06:50:35 -080026 minAllocSize = SkTMax<size_t>(GrSizeAlignUp(minAllocSize, kAlignment), kSmallestMinAllocSize);
27 preallocSize = SkTMax<size_t>(GrSizeAlignUp(preallocSize, kAlignment), minAllocSize);
28
29 fMinAllocSize = minAllocSize;
joshualitt22c6f5c2016-01-05 08:07:18 -080030 fSize = 0;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000031
dskibae4cd0062016-11-29 06:50:35 -080032 fHead = CreateBlock(preallocSize);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000033 fTail = fHead;
halcanary96fcdcc2015-08-27 07:41:13 -070034 fHead->fNext = nullptr;
35 fHead->fPrev = nullptr;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000036 VALIDATE;
37};
38
39GrMemoryPool::~GrMemoryPool() {
40 VALIDATE;
Brian Salomona5002c32017-03-28 16:51:02 -040041#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.orgf6de4752013-08-17 00:02:59 +000054 SkASSERT(0 == fAllocationCnt);
55 SkASSERT(fHead == fTail);
56 SkASSERT(0 == fHead->fLiveCount);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000057 DeleteBlock(fHead);
58};
59
60void* GrMemoryPool::allocate(size_t size) {
61 VALIDATE;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000062 size += kPerAllocPad;
robertphillips19c62502016-01-06 07:04:46 -080063 size = GrSizeAlignUp(size, kAlignment);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000064 if (fTail->fFreeSize < size) {
dskibae4cd0062016-11-29 06:50:35 -080065 size_t blockSize = size + kHeaderSize;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000066 blockSize = SkTMax<size_t>(blockSize, fMinAllocSize);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000067 BlockHeader* block = CreateBlock(blockSize);
68
69 block->fPrev = fTail;
halcanary96fcdcc2015-08-27 07:41:13 -070070 block->fNext = nullptr;
71 SkASSERT(nullptr == fTail->fNext);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000072 fTail->fNext = block;
73 fTail = block;
joshualittb7133be2015-04-08 09:08:31 -070074 fSize += block->fSize;
75 SkDEBUGCODE(++fAllocBlockCnt);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000076 }
robertphillipsb7f4b8e2016-01-07 10:12:16 -080077 SkASSERT(kAssignedMarker == fTail->fBlockSentinal);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000078 SkASSERT(fTail->fFreeSize >= size);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000079 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.
robertphillips19c62502016-01-06 07:04:46 -080082 AllocHeader* allocData = reinterpret_cast<AllocHeader*>(ptr);
83 SkDEBUGCODE(allocData->fSentinal = kAssignedMarker);
Brian Salomona5002c32017-03-28 16:51:02 -040084 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));
robertphillips19c62502016-01-06 07:04:46 -080087 allocData->fHeader = fTail;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000088 ptr += kPerAllocPad;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000089 fTail->fPrevPtr = fTail->fCurrPtr;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000090 fTail->fCurrPtr += size;
91 fTail->fFreeSize -= size;
92 fTail->fLiveCount += 1;
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000093 SkDEBUGCODE(++fAllocationCnt);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000094 VALIDATE;
95 return reinterpret_cast<void*>(ptr);
96}
97
98void GrMemoryPool::release(void* p) {
99 VALIDATE;
100 intptr_t ptr = reinterpret_cast<intptr_t>(p) - kPerAllocPad;
robertphillips19c62502016-01-06 07:04:46 -0800101 AllocHeader* allocData = reinterpret_cast<AllocHeader*>(ptr);
102 SkASSERT(kAssignedMarker == allocData->fSentinal);
103 SkDEBUGCODE(allocData->fSentinal = kFreedMarker);
Brian Salomona5002c32017-03-28 16:51:02 -0400104 SkDEBUGCODE(fAllocatedIDs.remove(allocData->fID));
robertphillips19c62502016-01-06 07:04:46 -0800105 BlockHeader* block = allocData->fHeader;
robertphillipsb7f4b8e2016-01-07 10:12:16 -0800106 SkASSERT(kAssignedMarker == block->fBlockSentinal);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000107 if (1 == block->fLiveCount) {
108 // the head block is special, it is reset rather than deleted
109 if (fHead == block) {
joshualittb7133be2015-04-08 09:08:31 -0700110 fHead->fCurrPtr = reinterpret_cast<intptr_t>(fHead) + kHeaderSize;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000111 fHead->fLiveCount = 0;
dskibae4cd0062016-11-29 06:50:35 -0800112 fHead->fFreeSize = fHead->fSize - kHeaderSize;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000113 } else {
114 BlockHeader* prev = block->fPrev;
115 BlockHeader* next = block->fNext;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000116 SkASSERT(prev);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000117 prev->fNext = next;
118 if (next) {
119 next->fPrev = prev;
120 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000121 SkASSERT(fTail == block);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000122 fTail = prev;
123 }
joshualittb7133be2015-04-08 09:08:31 -0700124 fSize -= block->fSize;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000125 DeleteBlock(block);
joshualittb7133be2015-04-08 09:08:31 -0700126 SkDEBUGCODE(fAllocBlockCnt--);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000127 }
128 } else {
129 --block->fLiveCount;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000130 // 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.com4da34e32012-06-19 15:40:27 +0000135 }
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000136 SkDEBUGCODE(--fAllocationCnt);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000137 VALIDATE;
138}
139
dskibae4cd0062016-11-29 06:50:35 -0800140GrMemoryPool::BlockHeader* GrMemoryPool::CreateBlock(size_t blockSize) {
141 blockSize = SkTMax<size_t>(blockSize, kHeaderSize);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000142 BlockHeader* block =
dskibae4cd0062016-11-29 06:50:35 -0800143 reinterpret_cast<BlockHeader*>(sk_malloc_throw(blockSize));
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000144 // we assume malloc gives us aligned memory
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000145 SkASSERT(!(reinterpret_cast<intptr_t>(block) % kAlignment));
robertphillipsb7f4b8e2016-01-07 10:12:16 -0800146 SkDEBUGCODE(block->fBlockSentinal = kAssignedMarker);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000147 block->fLiveCount = 0;
dskibae4cd0062016-11-29 06:50:35 -0800148 block->fFreeSize = blockSize - kHeaderSize;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000149 block->fCurrPtr = reinterpret_cast<intptr_t>(block) + kHeaderSize;
halcanary96fcdcc2015-08-27 07:41:13 -0700150 block->fPrevPtr = 0; // gcc warns on assigning nullptr to an intptr_t.
dskibae4cd0062016-11-29 06:50:35 -0800151 block->fSize = blockSize;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000152 return block;
153}
154
155void GrMemoryPool::DeleteBlock(BlockHeader* block) {
robertphillipsb7f4b8e2016-01-07 10:12:16 -0800156 SkASSERT(kAssignedMarker == block->fBlockSentinal);
157 SkDEBUGCODE(block->fBlockSentinal = kFreedMarker); // FWIW
reed@google.com939ca7c2013-09-26 19:56:51 +0000158 sk_free(block);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000159}
160
161void GrMemoryPool::validate() {
humper@google.com0e515772013-01-07 19:54:40 +0000162#ifdef SK_DEBUG
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000163 BlockHeader* block = fHead;
halcanary96fcdcc2015-08-27 07:41:13 -0700164 BlockHeader* prev = nullptr;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000165 SkASSERT(block);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000166 int allocCount = 0;
167 do {
robertphillipsb7f4b8e2016-01-07 10:12:16 -0800168 SkASSERT(kAssignedMarker == block->fBlockSentinal);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000169 allocCount += block->fLiveCount;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000170 SkASSERT(prev == block->fPrev);
bsalomon49f085d2014-09-05 13:34:00 -0700171 if (prev) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000172 SkASSERT(prev->fNext == block);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000173 }
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.com4da34e32012-06-19 15:40:27 +0000178 intptr_t userStart = b + kHeaderSize;
179
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000180 SkASSERT(!(b % kAlignment));
181 SkASSERT(!(totalSize % kAlignment));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000182 SkASSERT(!(block->fCurrPtr % kAlignment));
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000183 if (fHead != block) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000184 SkASSERT(block->fLiveCount);
dskibae4cd0062016-11-29 06:50:35 -0800185 SkASSERT(totalSize >= fMinAllocSize);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000186 } else {
dskibae4cd0062016-11-29 06:50:35 -0800187 SkASSERT(totalSize == block->fSize);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000188 }
189 if (!block->fLiveCount) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000190 SkASSERT(ptrOffset == kHeaderSize);
191 SkASSERT(userStart == block->fCurrPtr);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000192 } else {
robertphillips19c62502016-01-06 07:04:46 -0800193 AllocHeader* allocData = reinterpret_cast<AllocHeader*>(userStart);
halcanary9d524f22016-03-29 09:03:52 -0700194 SkASSERT(allocData->fSentinal == kAssignedMarker ||
robertphillips19c62502016-01-06 07:04:46 -0800195 allocData->fSentinal == kFreedMarker);
196 SkASSERT(block == allocData->fHeader);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000197 }
robertphillips19c62502016-01-06 07:04:46 -0800198
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000199 prev = block;
200 } while ((block = block->fNext));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000201 SkASSERT(allocCount == fAllocationCnt);
Brian Salomona5002c32017-03-28 16:51:02 -0400202 SkASSERT(fAllocationCnt == fAllocatedIDs.count());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000203 SkASSERT(prev == fTail);
joshualitt22c6f5c2016-01-05 08:07:18 -0800204 SkASSERT(fAllocBlockCnt != 0 || fSize == 0);
humper@google.com0e515772013-01-07 19:54:40 +0000205#endif
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000206}