| 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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 size_t fBlockSize; | 217 size_t fBlockSize; |
| 218 size_t fItemSize; | 218 size_t fItemSize; |
| 219 int fItemsPerBlock; | 219 int fItemsPerBlock; |
| 220 bool fOwnFirstBlock; | 220 bool fOwnFirstBlock; |
| 221 int fCount; | 221 int fCount; |
| 222 int fInsertionIndexInBlock; | 222 int fInsertionIndexInBlock; |
| 223 | 223 |
| 224 typedef SkNoncopyable INHERITED; | 224 typedef SkNoncopyable INHERITED; |
| 225 }; | 225 }; |
| 226 | 226 |
| 227 template <typename T> | 227 template <typename T> class GrTAllocator; |
| 228 class GrTAllocator : SkNoncopyable { | 228 template <typename T> void* operator new(size_t, GrTAllocator<T>*); |
| 229 |
| 230 template <typename T> class GrTAllocator : SkNoncopyable { |
| 229 public: | 231 public: |
| 230 virtual ~GrTAllocator() { this->reset(); }; | 232 virtual ~GrTAllocator() { this->reset(); }; |
| 231 | 233 |
| 232 /** | 234 /** |
| 233 * Create an allocator | 235 * Create an allocator |
| 234 * | 236 * |
| 235 * @param itemsPerBlock the number of items to allocate at once | 237 * @param itemsPerBlock the number of items to allocate at once |
| 236 */ | 238 */ |
| 237 explicit GrTAllocator(int itemsPerBlock) | 239 explicit GrTAllocator(int itemsPerBlock) |
| 238 : fAllocator(sizeof(T), itemsPerBlock, NULL) {} | 240 : fAllocator(sizeof(T), itemsPerBlock, NULL) {} |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 * | 355 * |
| 354 * @param initialBlock optional memory to use for the first block. | 356 * @param initialBlock optional memory to use for the first block. |
| 355 * Must be at least size(T)*itemsPerBlock sized. | 357 * Must be at least size(T)*itemsPerBlock sized. |
| 356 * Caller is responsible for freeing this memory. | 358 * Caller is responsible for freeing this memory. |
| 357 */ | 359 */ |
| 358 void setInitialBlock(void* initialBlock) { | 360 void setInitialBlock(void* initialBlock) { |
| 359 fAllocator.setInitialBlock(initialBlock); | 361 fAllocator.setInitialBlock(initialBlock); |
| 360 } | 362 } |
| 361 | 363 |
| 362 private: | 364 private: |
| 365 friend void* operator new<T>(size_t, GrTAllocator*); |
| 366 |
| 363 GrAllocator fAllocator; | 367 GrAllocator fAllocator; |
| 364 typedef SkNoncopyable INHERITED; | 368 typedef SkNoncopyable INHERITED; |
| 365 }; | 369 }; |
| 366 | 370 |
| 367 template <int N, typename T> class GrSTAllocator : public GrTAllocator<T> { | 371 template <int N, typename T> class GrSTAllocator : public GrTAllocator<T> { |
| 368 private: | 372 private: |
| 369 typedef GrTAllocator<T> INHERITED; | 373 typedef GrTAllocator<T> INHERITED; |
| 370 | 374 |
| 371 public: | 375 public: |
| 372 GrSTAllocator() : INHERITED(N) { | 376 GrSTAllocator() : INHERITED(N) { |
| 373 this->setInitialBlock(fStorage.get()); | 377 this->setInitialBlock(fStorage.get()); |
| 374 } | 378 } |
| 375 | 379 |
| 376 private: | 380 private: |
| 377 SkAlignedSTStorage<N, T> fStorage; | 381 SkAlignedSTStorage<N, T> fStorage; |
| 378 }; | 382 }; |
| 379 | 383 |
| 384 template <typename T> void* operator new(size_t size, GrTAllocator<T>* allocator
) { |
| 385 return allocator->fAllocator.push_back(); |
| 386 } |
| 387 |
| 388 // Skia doesn't use C++ exceptions but it may be compiled with them enabled. Hav
ing an op delete |
| 389 // to match the op new silences warnings about missing op delete when a construc
tor throws an |
| 390 // exception. |
| 391 template <typename T> void operator delete(void*, GrTAllocator<T>*) { |
| 392 SK_CRASH(); |
| 393 } |
| 394 |
| 395 #define GrNEW_APPEND_TO_ALLOCATOR(allocator_ptr, type_name, args) \ |
| 396 new (allocator_ptr) type_name args |
| 397 |
| 380 #endif | 398 #endif |
| OLD | NEW |