OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 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 #ifndef GrAllocator_DEFINED | 8 #ifndef GrAllocator_DEFINED |
9 #define GrAllocator_DEFINED | 9 #define GrAllocator_DEFINED |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 if (fItemsPerBlock == fInsertionIndexInBlock) { | 53 if (fItemsPerBlock == fInsertionIndexInBlock) { |
54 fBlocks.push_back() = sk_malloc_throw(fBlockSize); | 54 fBlocks.push_back() = sk_malloc_throw(fBlockSize); |
55 fInsertionIndexInBlock = 0; | 55 fInsertionIndexInBlock = 0; |
56 } | 56 } |
57 void* ret = (char*)fBlocks.back() + fItemSize * fInsertionIndexInBlock; | 57 void* ret = (char*)fBlocks.back() + fItemSize * fInsertionIndexInBlock; |
58 ++fCount; | 58 ++fCount; |
59 ++fInsertionIndexInBlock; | 59 ++fInsertionIndexInBlock; |
60 return ret; | 60 return ret; |
61 } | 61 } |
62 | 62 |
63 /** | 63 /** |
robertphillips
2014/09/04 20:24:35
Add a unit test for this new capability ?
bsalomon
2014/09/05 20:02:54
Done (in separate CL)
| |
64 * Remove the last item, only call if count() != 0 | |
65 */ | |
66 void pop_back() { | |
67 SkASSERT(fCount); | |
68 SkASSERT(fInsertionIndexInBlock > 0); | |
69 --fInsertionIndexInBlock; | |
70 --fCount; | |
71 if (0 == fInsertionIndexInBlock) { | |
72 // Never delete the first block | |
73 if (fBlocks.count() > 1) { | |
74 sk_free(fBlocks.back()); | |
75 fBlocks.pop_back(); | |
76 fInsertionIndexInBlock = fItemsPerBlock; | |
77 } | |
78 } | |
79 } | |
80 | |
81 /** | |
64 * Removes all added items. | 82 * Removes all added items. |
65 */ | 83 */ |
66 void reset() { | 84 void reset() { |
67 int firstBlockToFree = fOwnFirstBlock ? 0 : 1; | 85 int firstBlockToFree = fOwnFirstBlock ? 0 : 1; |
68 for (int i = firstBlockToFree; i < fBlocks.count(); ++i) { | 86 for (int i = firstBlockToFree; i < fBlocks.count(); ++i) { |
69 sk_free(fBlocks[i]); | 87 sk_free(fBlocks[i]); |
70 } | 88 } |
71 if (fOwnFirstBlock) { | 89 if (fOwnFirstBlock) { |
72 fBlocks.reset(); | 90 fBlocks.reset(); |
73 // This force us to allocate a new block on push_back(). | 91 // This force us to allocate a new block on push_back(). |
(...skipping 28 matching lines...) Expand all Loading... | |
102 | 120 |
103 /** | 121 /** |
104 * Access last item, only call if count() != 0 | 122 * Access last item, only call if count() != 0 |
105 */ | 123 */ |
106 const void* back() const { | 124 const void* back() const { |
107 SkASSERT(fCount); | 125 SkASSERT(fCount); |
108 SkASSERT(fInsertionIndexInBlock > 0); | 126 SkASSERT(fInsertionIndexInBlock > 0); |
109 return (const char*)(fBlocks.back()) + (fInsertionIndexInBlock - 1) * fI temSize; | 127 return (const char*)(fBlocks.back()) + (fInsertionIndexInBlock - 1) * fI temSize; |
110 } | 128 } |
111 | 129 |
112 | |
113 /** | 130 /** |
114 * Iterates through the allocator. This is faster than using operator[] when walking linearly | 131 * Iterates through the allocator. This is faster than using operator[] when walking linearly |
115 * through the allocator. | 132 * through the allocator. |
116 */ | 133 */ |
117 class Iter { | 134 class Iter { |
118 public: | 135 public: |
119 /** | 136 /** |
120 * Initializes the iterator. next() must be called before get(). | 137 * Initializes the iterator. next() must be called before get(). |
121 */ | 138 */ |
122 Iter(const GrAllocator* allocator) | 139 Iter(const GrAllocator* allocator) |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 } | 250 } |
234 | 251 |
235 T& push_back(const T& t) { | 252 T& push_back(const T& t) { |
236 void* item = fAllocator.push_back(); | 253 void* item = fAllocator.push_back(); |
237 SkASSERT(NULL != item); | 254 SkASSERT(NULL != item); |
238 SkNEW_PLACEMENT_ARGS(item, T, (t)); | 255 SkNEW_PLACEMENT_ARGS(item, T, (t)); |
239 return *(T*)item; | 256 return *(T*)item; |
240 } | 257 } |
241 | 258 |
242 /** | 259 /** |
260 * Remove the last item, only call if count() != 0 | |
261 */ | |
262 void pop_back() { | |
263 this->back().~T(); | |
264 fAllocator.pop_back(); | |
265 } | |
266 | |
267 /** | |
243 * Removes all added items. | 268 * Removes all added items. |
244 */ | 269 */ |
245 void reset() { | 270 void reset() { |
246 int c = fAllocator.count(); | 271 int c = fAllocator.count(); |
247 for (int i = 0; i < c; ++i) { | 272 for (int i = 0; i < c; ++i) { |
248 ((T*)fAllocator[i])->~T(); | 273 ((T*)fAllocator[i])->~T(); |
249 } | 274 } |
250 fAllocator.reset(); | 275 fAllocator.reset(); |
251 } | 276 } |
252 | 277 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
346 public: | 371 public: |
347 GrSTAllocator() : INHERITED(N) { | 372 GrSTAllocator() : INHERITED(N) { |
348 this->setInitialBlock(fStorage.get()); | 373 this->setInitialBlock(fStorage.get()); |
349 } | 374 } |
350 | 375 |
351 private: | 376 private: |
352 SkAlignedSTStorage<N, T> fStorage; | 377 SkAlignedSTStorage<N, T> fStorage; |
353 }; | 378 }; |
354 | 379 |
355 #endif | 380 #endif |
OLD | NEW |