| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google, Inc | 2 * Copyright 2014 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 SkSmallAllocator_DEFINED | 8 #ifndef SkSmallAllocator_DEFINED |
| 9 #define SkSmallAllocator_DEFINED | 9 #define SkSmallAllocator_DEFINED |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 * allocations. kMaxObjects is a hard limit on the number of objects that can | 22 * allocations. kMaxObjects is a hard limit on the number of objects that can |
| 23 * be allocated using this class. After that, attempts to create more objects | 23 * be allocated using this class. After that, attempts to create more objects |
| 24 * with this class will assert and return NULL. | 24 * with this class will assert and return NULL. |
| 25 * kTotalBytes is the total number of bytes provided for storage for all | 25 * kTotalBytes is the total number of bytes provided for storage for all |
| 26 * objects created by this allocator. If an object to be created is larger | 26 * objects created by this allocator. If an object to be created is larger |
| 27 * than the storage (minus storage already used), it will be allocated on the | 27 * than the storage (minus storage already used), it will be allocated on the |
| 28 * heap. This class's destructor will handle calling the destructor for each | 28 * heap. This class's destructor will handle calling the destructor for each |
| 29 * object it allocated and freeing its memory. | 29 * object it allocated and freeing its memory. |
| 30 */ | 30 */ |
| 31 template<uint32_t kMaxObjects, size_t kTotalBytes> | 31 template<uint32_t kMaxObjects, size_t kTotalBytes> |
| 32 class SkSmallAllocator : public SkNoncopyable { | 32 class SkSmallAllocator : SkNoncopyable { |
| 33 public: | 33 public: |
| 34 SkSmallAllocator() | 34 SkSmallAllocator() |
| 35 : fStorageUsed(0) | 35 : fStorageUsed(0) |
| 36 , fNumObjects(0) | 36 , fNumObjects(0) |
| 37 {} | 37 {} |
| 38 | 38 |
| 39 ~SkSmallAllocator() { | 39 ~SkSmallAllocator() { |
| 40 // Destruct in reverse order, in case an earlier object points to a | 40 // Destruct in reverse order, in case an earlier object points to a |
| 41 // later object. | 41 // later object. |
| 42 while (fNumObjects > 0) { | 42 while (fNumObjects > 0) { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 140 |
| 141 // Number of bytes used so far. | 141 // Number of bytes used so far. |
| 142 size_t fStorageUsed; | 142 size_t fStorageUsed; |
| 143 // Pad the storage size to be 4-byte aligned. | 143 // Pad the storage size to be 4-byte aligned. |
| 144 uint32_t fStorage[SkAlign4(kTotalBytes) >> 2]; | 144 uint32_t fStorage[SkAlign4(kTotalBytes) >> 2]; |
| 145 uint32_t fNumObjects; | 145 uint32_t fNumObjects; |
| 146 Rec fRecs[kMaxObjects]; | 146 Rec fRecs[kMaxObjects]; |
| 147 }; | 147 }; |
| 148 | 148 |
| 149 #endif // SkSmallAllocator_DEFINED | 149 #endif // SkSmallAllocator_DEFINED |
| OLD | NEW |