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