OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
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 "SkChunkAlloc.h" | 8 #include "SkChunkAlloc.h" |
9 | 9 |
10 // Don't malloc any chunks smaller than this | 10 // Don't malloc any chunks smaller than this |
(...skipping 12 matching lines...) Expand all Loading... |
23 char* fFreePtr; | 23 char* fFreePtr; |
24 // data[] follows | 24 // data[] follows |
25 | 25 |
26 size_t blockSize() { | 26 size_t blockSize() { |
27 char* start = this->startOfData(); | 27 char* start = this->startOfData(); |
28 size_t bytes = fFreePtr - start; | 28 size_t bytes = fFreePtr - start; |
29 return fFreeSize + bytes; | 29 return fFreeSize + bytes; |
30 } | 30 } |
31 | 31 |
32 void reset() { | 32 void reset() { |
33 fNext = NULL; | 33 fNext = nullptr; |
34 fFreeSize = this->blockSize(); | 34 fFreeSize = this->blockSize(); |
35 fFreePtr = this->startOfData(); | 35 fFreePtr = this->startOfData(); |
36 } | 36 } |
37 | 37 |
38 char* startOfData() { | 38 char* startOfData() { |
39 return reinterpret_cast<char*>(this + 1); | 39 return reinterpret_cast<char*>(this + 1); |
40 } | 40 } |
41 | 41 |
42 static void FreeChain(Block* block) { | 42 static void FreeChain(Block* block) { |
43 while (block) { | 43 while (block) { |
44 Block* next = block->fNext; | 44 Block* next = block->fNext; |
45 sk_free(block); | 45 sk_free(block); |
46 block = next; | 46 block = next; |
47 } | 47 } |
48 }; | 48 }; |
49 | 49 |
50 bool contains(const void* addr) const { | 50 bool contains(const void* addr) const { |
51 const char* ptr = reinterpret_cast<const char*>(addr); | 51 const char* ptr = reinterpret_cast<const char*>(addr); |
52 return ptr >= (const char*)(this + 1) && ptr < fFreePtr; | 52 return ptr >= (const char*)(this + 1) && ptr < fFreePtr; |
53 } | 53 } |
54 }; | 54 }; |
55 | 55 |
56 /////////////////////////////////////////////////////////////////////////////// | 56 /////////////////////////////////////////////////////////////////////////////// |
57 | 57 |
58 SkChunkAlloc::SkChunkAlloc(size_t minSize) { | 58 SkChunkAlloc::SkChunkAlloc(size_t minSize) { |
59 if (minSize < MIN_CHUNKALLOC_BLOCK_SIZE) { | 59 if (minSize < MIN_CHUNKALLOC_BLOCK_SIZE) { |
60 minSize = MIN_CHUNKALLOC_BLOCK_SIZE; | 60 minSize = MIN_CHUNKALLOC_BLOCK_SIZE; |
61 } | 61 } |
62 | 62 |
63 fBlock = NULL; | 63 fBlock = nullptr; |
64 fMinSize = minSize; | 64 fMinSize = minSize; |
65 fChunkSize = fMinSize; | 65 fChunkSize = fMinSize; |
66 fTotalCapacity = 0; | 66 fTotalCapacity = 0; |
67 fTotalUsed = 0; | 67 fTotalUsed = 0; |
68 SkDEBUGCODE(fTotalLost = 0;) | 68 SkDEBUGCODE(fTotalLost = 0;) |
69 SkDEBUGCODE(fBlockCount = 0;) | 69 SkDEBUGCODE(fBlockCount = 0;) |
70 } | 70 } |
71 | 71 |
72 SkChunkAlloc::~SkChunkAlloc() { | 72 SkChunkAlloc::~SkChunkAlloc() { |
73 this->reset(); | 73 this->reset(); |
74 } | 74 } |
75 | 75 |
76 void SkChunkAlloc::reset() { | 76 void SkChunkAlloc::reset() { |
77 Block::FreeChain(fBlock); | 77 Block::FreeChain(fBlock); |
78 fBlock = NULL; | 78 fBlock = nullptr; |
79 fChunkSize = fMinSize; // reset to our initial minSize | 79 fChunkSize = fMinSize; // reset to our initial minSize |
80 fTotalCapacity = 0; | 80 fTotalCapacity = 0; |
81 fTotalUsed = 0; | 81 fTotalUsed = 0; |
82 SkDEBUGCODE(fTotalLost = 0;) | 82 SkDEBUGCODE(fTotalLost = 0;) |
83 SkDEBUGCODE(fBlockCount = 0;) | 83 SkDEBUGCODE(fBlockCount = 0;) |
84 } | 84 } |
85 | 85 |
86 void SkChunkAlloc::rewind() { | 86 void SkChunkAlloc::rewind() { |
87 SkDEBUGCODE(this->validate();) | 87 SkDEBUGCODE(this->validate();) |
88 | 88 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 } | 135 } |
136 return block; | 136 return block; |
137 } | 137 } |
138 | 138 |
139 SkChunkAlloc::Block* SkChunkAlloc::addBlockIfNecessary(size_t bytes, AllocFailTy
pe ftype) { | 139 SkChunkAlloc::Block* SkChunkAlloc::addBlockIfNecessary(size_t bytes, AllocFailTy
pe ftype) { |
140 SkASSERT(SkIsAlign4(bytes)); | 140 SkASSERT(SkIsAlign4(bytes)); |
141 | 141 |
142 if (!fBlock || bytes > fBlock->fFreeSize) { | 142 if (!fBlock || bytes > fBlock->fFreeSize) { |
143 Block* block = this->newBlock(bytes, ftype); | 143 Block* block = this->newBlock(bytes, ftype); |
144 if (!block) { | 144 if (!block) { |
145 return NULL; | 145 return nullptr; |
146 } | 146 } |
147 #ifdef SK_DEBUG | 147 #ifdef SK_DEBUG |
148 if (fBlock) { | 148 if (fBlock) { |
149 fTotalLost += fBlock->fFreeSize; | 149 fTotalLost += fBlock->fFreeSize; |
150 } | 150 } |
151 #endif | 151 #endif |
152 block->fNext = fBlock; | 152 block->fNext = fBlock; |
153 fBlock = block; | 153 fBlock = block; |
154 } | 154 } |
155 | 155 |
156 SkASSERT(fBlock && bytes <= fBlock->fFreeSize); | 156 SkASSERT(fBlock && bytes <= fBlock->fFreeSize); |
157 return fBlock; | 157 return fBlock; |
158 } | 158 } |
159 | 159 |
160 void* SkChunkAlloc::alloc(size_t bytes, AllocFailType ftype) { | 160 void* SkChunkAlloc::alloc(size_t bytes, AllocFailType ftype) { |
161 SkDEBUGCODE(this->validate();) | 161 SkDEBUGCODE(this->validate();) |
162 | 162 |
163 bytes = SkAlign4(bytes); | 163 bytes = SkAlign4(bytes); |
164 | 164 |
165 Block* block = this->addBlockIfNecessary(bytes, ftype); | 165 Block* block = this->addBlockIfNecessary(bytes, ftype); |
166 if (!block) { | 166 if (!block) { |
167 return NULL; | 167 return nullptr; |
168 } | 168 } |
169 | 169 |
170 char* ptr = block->fFreePtr; | 170 char* ptr = block->fFreePtr; |
171 | 171 |
172 fTotalUsed += bytes; | 172 fTotalUsed += bytes; |
173 block->fFreeSize -= bytes; | 173 block->fFreeSize -= bytes; |
174 block->fFreePtr = ptr + bytes; | 174 block->fFreePtr = ptr + bytes; |
175 SkDEBUGCODE(this->validate();) | 175 SkDEBUGCODE(this->validate();) |
176 return ptr; | 176 return ptr; |
177 } | 177 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 } | 226 } |
227 | 227 |
228 SkASSERT(fBlockCount == numBlocks); | 228 SkASSERT(fBlockCount == numBlocks); |
229 SkASSERT(fTotalCapacity == totCapacity); | 229 SkASSERT(fTotalCapacity == totCapacity); |
230 SkASSERT(fTotalUsed == totUsed); | 230 SkASSERT(fTotalUsed == totUsed); |
231 SkASSERT(fTotalLost == totLost); | 231 SkASSERT(fTotalLost == totLost); |
232 SkASSERT(totCapacity == totUsed + totLost + totAvailable); | 232 SkASSERT(totCapacity == totUsed + totLost + totAvailable); |
233 } | 233 } |
234 #endif | 234 #endif |
235 | 235 |
OLD | NEW |