| 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 11 matching lines...) Expand all Loading... |
| 22 * | 22 * |
| 23 * @param itemSize the size of each item to allocate | 23 * @param itemSize the size of each item to allocate |
| 24 * @param itemsPerBlock the number of items to allocate at once | 24 * @param itemsPerBlock the number of items to allocate at once |
| 25 * @param initialBlock optional memory to use for the first block. | 25 * @param initialBlock optional memory to use for the first block. |
| 26 * Must be at least itemSize*itemsPerBlock sized. | 26 * Must be at least itemSize*itemsPerBlock sized. |
| 27 * Caller is responsible for freeing this memory. | 27 * Caller is responsible for freeing this memory. |
| 28 */ | 28 */ |
| 29 GrAllocator(size_t itemSize, int itemsPerBlock, void* initialBlock) | 29 GrAllocator(size_t itemSize, int itemsPerBlock, void* initialBlock) |
| 30 : fItemSize(itemSize) | 30 : fItemSize(itemSize) |
| 31 , fItemsPerBlock(itemsPerBlock) | 31 , fItemsPerBlock(itemsPerBlock) |
| 32 , fOwnFirstBlock(NULL == initialBlock) | 32 , fOwnFirstBlock(nullptr == initialBlock) |
| 33 , fCount(0) | 33 , fCount(0) |
| 34 , fInsertionIndexInBlock(0) { | 34 , fInsertionIndexInBlock(0) { |
| 35 SkASSERT(itemsPerBlock > 0); | 35 SkASSERT(itemsPerBlock > 0); |
| 36 fBlockSize = fItemSize * fItemsPerBlock; | 36 fBlockSize = fItemSize * fItemsPerBlock; |
| 37 if (fOwnFirstBlock) { | 37 if (fOwnFirstBlock) { |
| 38 // This force us to allocate a new block on push_back(). | 38 // This force us to allocate a new block on push_back(). |
| 39 fInsertionIndexInBlock = fItemsPerBlock; | 39 fInsertionIndexInBlock = fItemsPerBlock; |
| 40 } else { | 40 } else { |
| 41 fBlocks.push_back() = initialBlock; | 41 fBlocks.push_back() = initialBlock; |
| 42 fInsertionIndexInBlock = 0; | 42 fInsertionIndexInBlock = 0; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 */ | 185 */ |
| 186 const void* operator[] (int i) const { | 186 const void* operator[] (int i) const { |
| 187 SkASSERT(i >= 0 && i < fCount); | 187 SkASSERT(i >= 0 && i < fCount); |
| 188 return (const char*)fBlocks[i / fItemsPerBlock] + | 188 return (const char*)fBlocks[i / fItemsPerBlock] + |
| 189 fItemSize * (i % fItemsPerBlock); | 189 fItemSize * (i % fItemsPerBlock); |
| 190 } | 190 } |
| 191 | 191 |
| 192 protected: | 192 protected: |
| 193 /** | 193 /** |
| 194 * Set first block of memory to write into. Must be called before any other
methods. | 194 * Set first block of memory to write into. Must be called before any other
methods. |
| 195 * This requires that you have passed NULL in the constructor. | 195 * This requires that you have passed nullptr in the constructor. |
| 196 * | 196 * |
| 197 * @param initialBlock optional memory to use for the first block. | 197 * @param initialBlock optional memory to use for the first block. |
| 198 * Must be at least itemSize*itemsPerBlock sized. | 198 * Must be at least itemSize*itemsPerBlock sized. |
| 199 * Caller is responsible for freeing this memory. | 199 * Caller is responsible for freeing this memory. |
| 200 */ | 200 */ |
| 201 void setInitialBlock(void* initialBlock) { | 201 void setInitialBlock(void* initialBlock) { |
| 202 SkASSERT(0 == fCount); | 202 SkASSERT(0 == fCount); |
| 203 SkASSERT(0 == fBlocks.count()); | 203 SkASSERT(0 == fBlocks.count()); |
| 204 SkASSERT(fItemsPerBlock == fInsertionIndexInBlock); | 204 SkASSERT(fItemsPerBlock == fInsertionIndexInBlock); |
| 205 fOwnFirstBlock = false; | 205 fOwnFirstBlock = false; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 230 template <typename T> class GrTAllocator : SkNoncopyable { | 230 template <typename T> class GrTAllocator : SkNoncopyable { |
| 231 public: | 231 public: |
| 232 virtual ~GrTAllocator() { this->reset(); }; | 232 virtual ~GrTAllocator() { this->reset(); }; |
| 233 | 233 |
| 234 /** | 234 /** |
| 235 * Create an allocator | 235 * Create an allocator |
| 236 * | 236 * |
| 237 * @param itemsPerBlock the number of items to allocate at once | 237 * @param itemsPerBlock the number of items to allocate at once |
| 238 */ | 238 */ |
| 239 explicit GrTAllocator(int itemsPerBlock) | 239 explicit GrTAllocator(int itemsPerBlock) |
| 240 : fAllocator(sizeof(T), itemsPerBlock, NULL) {} | 240 : fAllocator(sizeof(T), itemsPerBlock, nullptr) {} |
| 241 | 241 |
| 242 /** | 242 /** |
| 243 * Adds an item and returns it. | 243 * Adds an item and returns it. |
| 244 * | 244 * |
| 245 * @return the added item. | 245 * @return the added item. |
| 246 */ | 246 */ |
| 247 T& push_back() { | 247 T& push_back() { |
| 248 void* item = fAllocator.push_back(); | 248 void* item = fAllocator.push_back(); |
| 249 SkASSERT(item); | 249 SkASSERT(item); |
| 250 new (item) T; | 250 new (item) T; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 // to match the op new silences warnings about missing op delete when a construc
tor throws an | 389 // to match the op new silences warnings about missing op delete when a construc
tor throws an |
| 390 // exception. | 390 // exception. |
| 391 template <typename T> void operator delete(void*, GrTAllocator<T>*) { | 391 template <typename T> void operator delete(void*, GrTAllocator<T>*) { |
| 392 SK_CRASH(); | 392 SK_CRASH(); |
| 393 } | 393 } |
| 394 | 394 |
| 395 #define GrNEW_APPEND_TO_ALLOCATOR(allocator_ptr, type_name, args) \ | 395 #define GrNEW_APPEND_TO_ALLOCATOR(allocator_ptr, type_name, args) \ |
| 396 new (allocator_ptr) type_name args | 396 new (allocator_ptr) type_name args |
| 397 | 397 |
| 398 #endif | 398 #endif |
| OLD | NEW |