| 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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 template<typename T, typename A1, typename A2, typename A3> | 89 template<typename T, typename A1, typename A2, typename A3> |
| 90 T* createT(const A1& a1, const A2& a2, const A3& a3) { | 90 T* createT(const A1& a1, const A2& a2, const A3& a3) { |
| 91 void* buf = this->reserveT<T>(); | 91 void* buf = this->reserveT<T>(); |
| 92 if (NULL == buf) { | 92 if (NULL == buf) { |
| 93 return NULL; | 93 return NULL; |
| 94 } | 94 } |
| 95 SkNEW_PLACEMENT_ARGS(buf, T, (a1, a2, a3)); | 95 SkNEW_PLACEMENT_ARGS(buf, T, (a1, a2, a3)); |
| 96 return static_cast<T*>(buf); | 96 return static_cast<T*>(buf); |
| 97 } | 97 } |
| 98 | 98 |
| 99 private: | |
| 100 /* | 99 /* |
| 101 * Helper function to provide space for one T. The space will be in | 100 * Reserve a specified amount of space (must be enough space for one T). |
| 102 * fStorage if there is room, or on the heap otherwise. Either way, this | 101 * The space will be in fStorage if there is room, or on the heap otherwise
. |
| 103 * class will call ~T() in its destructor and free the heap allocation if | 102 * Either way, this class will call ~T() in its destructor and free the hea
p |
| 104 * necessary. | 103 * allocation if necessary. |
| 104 * Unlike createT(), this method will not call the constructor of T. |
| 105 */ | 105 */ |
| 106 template<typename T> void* reserveT() { | 106 template<typename T> void* reserveT(size_t storageRequired = sizeof(T)) { |
| 107 SkASSERT(fNumObjects < kMaxObjects); | 107 SkASSERT(fNumObjects < kMaxObjects); |
| 108 SkASSERT(storageRequired >= sizeof(T)); |
| 108 if (kMaxObjects == fNumObjects) { | 109 if (kMaxObjects == fNumObjects) { |
| 109 return NULL; | 110 return NULL; |
| 110 } | 111 } |
| 111 const size_t storageRemaining = SkAlign4(kTotalBytes) - fStorageUsed; | 112 const size_t storageRemaining = SkAlign4(kTotalBytes) - fStorageUsed; |
| 112 const size_t storageRequired = SkAlign4(sizeof(T)); | 113 storageRequired = SkAlign4(storageRequired); |
| 113 Rec* rec = &fRecs[fNumObjects]; | 114 Rec* rec = &fRecs[fNumObjects]; |
| 114 if (storageRequired > storageRemaining) { | 115 if (storageRequired > storageRemaining) { |
| 115 // Allocate on the heap. Ideally we want to avoid this situation, | 116 // Allocate on the heap. Ideally we want to avoid this situation, |
| 116 // but we're not sure we can catch all callers, so handle it but | 117 // but we're not sure we can catch all callers, so handle it but |
| 117 // assert false in debug mode. | 118 // assert false in debug mode. |
| 118 SkASSERT(false); | 119 SkASSERT(false); |
| 119 rec->fHeapStorage = sk_malloc_throw(storageRequired); | 120 rec->fHeapStorage = sk_malloc_throw(storageRequired); |
| 120 rec->fObj = static_cast<void*>(rec->fHeapStorage); | 121 rec->fObj = static_cast<void*>(rec->fHeapStorage); |
| 121 } else { | 122 } else { |
| 122 // There is space in fStorage. | 123 // There is space in fStorage. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 139 | 140 |
| 140 // Number of bytes used so far. | 141 // Number of bytes used so far. |
| 141 size_t fStorageUsed; | 142 size_t fStorageUsed; |
| 142 // Pad the storage size to be 4-byte aligned. | 143 // Pad the storage size to be 4-byte aligned. |
| 143 uint32_t fStorage[SkAlign4(kTotalBytes) >> 2]; | 144 uint32_t fStorage[SkAlign4(kTotalBytes) >> 2]; |
| 144 uint32_t fNumObjects; | 145 uint32_t fNumObjects; |
| 145 Rec fRecs[kMaxObjects]; | 146 Rec fRecs[kMaxObjects]; |
| 146 }; | 147 }; |
| 147 | 148 |
| 148 #endif // SkSmallAllocator_DEFINED | 149 #endif // SkSmallAllocator_DEFINED |
| OLD | NEW |