Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: src/gpu/GrAllocator.h

Issue 538183002: Add pop_back() to GrAllocator and add unit test. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Address comments and put local struct type in anonymous namespace Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gyp/tests.gypi ('k') | tests/GrAllocatorTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 /**
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
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
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
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
OLDNEW
« no previous file with comments | « gyp/tests.gypi ('k') | tests/GrAllocatorTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698