OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrMemoryPool.h" | 8 #include "GrMemoryPool.h" |
9 | 9 |
10 #ifdef SK_DEBUG | 10 #ifdef SK_DEBUG |
11 #define VALIDATE this->validate() | 11 #define VALIDATE this->validate() |
12 #else | 12 #else |
13 #define VALIDATE | 13 #define VALIDATE |
14 #endif | 14 #endif |
15 | 15 |
16 GrMemoryPool::GrMemoryPool(size_t preallocSize, size_t minAllocSize) { | 16 GrMemoryPool::GrMemoryPool(size_t preallocSize, size_t minAllocSize) { |
17 SkDEBUGCODE(fAllocationCnt = 0); | 17 SkDEBUGCODE(fAllocationCnt = 0); |
18 SkDEBUGCODE(fAllocBlockCnt = 0); | 18 SkDEBUGCODE(fAllocBlockCnt = 0); |
19 | 19 |
20 minAllocSize = SkTMax<size_t>(minAllocSize, 1 << 10); | 20 minAllocSize = SkTMax<size_t>(minAllocSize, 1 << 10); |
21 fMinAllocSize = GrSizeAlignUp(minAllocSize + kPerAllocPad, kAlignment), | 21 fMinAllocSize = GrSizeAlignUp(minAllocSize + kPerAllocPad, kAlignment), |
22 fPreallocSize = GrSizeAlignUp(preallocSize + kPerAllocPad, kAlignment); | 22 fPreallocSize = GrSizeAlignUp(preallocSize + kPerAllocPad, kAlignment); |
23 fPreallocSize = SkTMax(fPreallocSize, fMinAllocSize); | 23 fPreallocSize = SkTMax(fPreallocSize, fMinAllocSize); |
24 fSize = fPreallocSize; | 24 fSize = fPreallocSize; |
25 | 25 |
26 fHead = CreateBlock(fPreallocSize); | 26 fHead = CreateBlock(fPreallocSize); |
27 fTail = fHead; | 27 fTail = fHead; |
28 fHead->fNext = NULL; | 28 fHead->fNext = nullptr; |
29 fHead->fPrev = NULL; | 29 fHead->fPrev = nullptr; |
30 VALIDATE; | 30 VALIDATE; |
31 }; | 31 }; |
32 | 32 |
33 GrMemoryPool::~GrMemoryPool() { | 33 GrMemoryPool::~GrMemoryPool() { |
34 VALIDATE; | 34 VALIDATE; |
35 SkASSERT(0 == fAllocationCnt); | 35 SkASSERT(0 == fAllocationCnt); |
36 SkASSERT(fHead == fTail); | 36 SkASSERT(fHead == fTail); |
37 SkASSERT(0 == fHead->fLiveCount); | 37 SkASSERT(0 == fHead->fLiveCount); |
38 DeleteBlock(fHead); | 38 DeleteBlock(fHead); |
39 }; | 39 }; |
40 | 40 |
41 void* GrMemoryPool::allocate(size_t size) { | 41 void* GrMemoryPool::allocate(size_t size) { |
42 VALIDATE; | 42 VALIDATE; |
43 size = GrSizeAlignUp(size, kAlignment); | 43 size = GrSizeAlignUp(size, kAlignment); |
44 size += kPerAllocPad; | 44 size += kPerAllocPad; |
45 if (fTail->fFreeSize < size) { | 45 if (fTail->fFreeSize < size) { |
46 size_t blockSize = size; | 46 size_t blockSize = size; |
47 blockSize = SkTMax<size_t>(blockSize, fMinAllocSize); | 47 blockSize = SkTMax<size_t>(blockSize, fMinAllocSize); |
48 BlockHeader* block = CreateBlock(blockSize); | 48 BlockHeader* block = CreateBlock(blockSize); |
49 | 49 |
50 block->fPrev = fTail; | 50 block->fPrev = fTail; |
51 block->fNext = NULL; | 51 block->fNext = nullptr; |
52 SkASSERT(NULL == fTail->fNext); | 52 SkASSERT(nullptr == fTail->fNext); |
53 fTail->fNext = block; | 53 fTail->fNext = block; |
54 fTail = block; | 54 fTail = block; |
55 fSize += block->fSize; | 55 fSize += block->fSize; |
56 SkDEBUGCODE(++fAllocBlockCnt); | 56 SkDEBUGCODE(++fAllocBlockCnt); |
57 } | 57 } |
58 SkASSERT(fTail->fFreeSize >= size); | 58 SkASSERT(fTail->fFreeSize >= size); |
59 intptr_t ptr = fTail->fCurrPtr; | 59 intptr_t ptr = fTail->fCurrPtr; |
60 // We stash a pointer to the block header, just before the allocated space, | 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. | 61 // so that we can decrement the live count on delete in constant time. |
62 *reinterpret_cast<BlockHeader**>(ptr) = fTail; | 62 *reinterpret_cast<BlockHeader**>(ptr) = fTail; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 110 |
111 GrMemoryPool::BlockHeader* GrMemoryPool::CreateBlock(size_t size) { | 111 GrMemoryPool::BlockHeader* GrMemoryPool::CreateBlock(size_t size) { |
112 size_t paddedSize = size + kHeaderSize; | 112 size_t paddedSize = size + kHeaderSize; |
113 BlockHeader* block = | 113 BlockHeader* block = |
114 reinterpret_cast<BlockHeader*>(sk_malloc_throw(paddedSize)); | 114 reinterpret_cast<BlockHeader*>(sk_malloc_throw(paddedSize)); |
115 // we assume malloc gives us aligned memory | 115 // we assume malloc gives us aligned memory |
116 SkASSERT(!(reinterpret_cast<intptr_t>(block) % kAlignment)); | 116 SkASSERT(!(reinterpret_cast<intptr_t>(block) % kAlignment)); |
117 block->fLiveCount = 0; | 117 block->fLiveCount = 0; |
118 block->fFreeSize = size; | 118 block->fFreeSize = size; |
119 block->fCurrPtr = reinterpret_cast<intptr_t>(block) + kHeaderSize; | 119 block->fCurrPtr = reinterpret_cast<intptr_t>(block) + kHeaderSize; |
120 block->fPrevPtr = 0; // gcc warns on assigning NULL to an intptr_t. | 120 block->fPrevPtr = 0; // gcc warns on assigning nullptr to an intptr_t. |
121 block->fSize = paddedSize; | 121 block->fSize = paddedSize; |
122 return block; | 122 return block; |
123 } | 123 } |
124 | 124 |
125 void GrMemoryPool::DeleteBlock(BlockHeader* block) { | 125 void GrMemoryPool::DeleteBlock(BlockHeader* block) { |
126 sk_free(block); | 126 sk_free(block); |
127 } | 127 } |
128 | 128 |
129 void GrMemoryPool::validate() { | 129 void GrMemoryPool::validate() { |
130 #ifdef SK_DEBUG | 130 #ifdef SK_DEBUG |
131 BlockHeader* block = fHead; | 131 BlockHeader* block = fHead; |
132 BlockHeader* prev = NULL; | 132 BlockHeader* prev = nullptr; |
133 SkASSERT(block); | 133 SkASSERT(block); |
134 int allocCount = 0; | 134 int allocCount = 0; |
135 do { | 135 do { |
136 allocCount += block->fLiveCount; | 136 allocCount += block->fLiveCount; |
137 SkASSERT(prev == block->fPrev); | 137 SkASSERT(prev == block->fPrev); |
138 if (prev) { | 138 if (prev) { |
139 SkASSERT(prev->fNext == block); | 139 SkASSERT(prev->fNext == block); |
140 } | 140 } |
141 | 141 |
142 intptr_t b = reinterpret_cast<intptr_t>(block); | 142 intptr_t b = reinterpret_cast<intptr_t>(block); |
(...skipping 18 matching lines...) Expand all Loading... |
161 } else { | 161 } else { |
162 SkASSERT(block == *reinterpret_cast<BlockHeader**>(userStart)); | 162 SkASSERT(block == *reinterpret_cast<BlockHeader**>(userStart)); |
163 } | 163 } |
164 prev = block; | 164 prev = block; |
165 } while ((block = block->fNext)); | 165 } while ((block = block->fNext)); |
166 SkASSERT(allocCount == fAllocationCnt); | 166 SkASSERT(allocCount == fAllocationCnt); |
167 SkASSERT(prev == fTail); | 167 SkASSERT(prev == fTail); |
168 SkASSERT(fAllocBlockCnt != 0 || fSize == fPreallocSize); | 168 SkASSERT(fAllocBlockCnt != 0 || fSize == fPreallocSize); |
169 #endif | 169 #endif |
170 } | 170 } |
OLD | NEW |