| 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 |
| 11 #include "SkTArray.h" | 11 #include "SkTDArray.h" |
| 12 #include "SkTypes.h" | 12 #include "SkTypes.h" |
| 13 | 13 |
| 14 #include <new> |
| 14 #include <utility> | 15 #include <utility> |
| 15 | 16 |
| 16 /* | 17 /* |
| 17 * Template class for allocating small objects without additional heap memory | 18 * Template class for allocating small objects without additional heap memory |
| 18 * allocations. | 19 * allocations. kMaxObjects is a hard limit on the number of objects that can |
| 20 * be allocated using this class. After that, attempts to create more objects |
| 21 * with this class will assert and return nullptr. |
| 19 * | 22 * |
| 20 * kTotalBytes is the total number of bytes provided for storage for all | 23 * kTotalBytes is the total number of bytes provided for storage for all |
| 21 * objects created by this allocator. If an object to be created is larger | 24 * objects created by this allocator. If an object to be created is larger |
| 22 * than the storage (minus storage already used), it will be allocated on the | 25 * than the storage (minus storage already used), it will be allocated on the |
| 23 * heap. This class's destructor will handle calling the destructor for each | 26 * heap. This class's destructor will handle calling the destructor for each |
| 24 * object it allocated and freeing its memory. | 27 * object it allocated and freeing its memory. |
| 28 * |
| 29 * Current the class always aligns each allocation to 16-bytes to be safe, but
future |
| 30 * may reduce this to only the alignment that is required per alloc. |
| 25 */ | 31 */ |
| 26 template<uint32_t kExpectedObjects, size_t kTotalBytes> | 32 template<uint32_t kMaxObjects, size_t kTotalBytes> |
| 27 class SkSmallAllocator : SkNoncopyable { | 33 class SkSmallAllocator : SkNoncopyable { |
| 28 public: | 34 public: |
| 35 SkSmallAllocator() |
| 36 : fStorageUsed(0) |
| 37 , fNumObjects(0) |
| 38 {} |
| 39 |
| 29 ~SkSmallAllocator() { | 40 ~SkSmallAllocator() { |
| 30 // Destruct in reverse order, in case an earlier object points to a | 41 // Destruct in reverse order, in case an earlier object points to a |
| 31 // later object. | 42 // later object. |
| 32 while (fRecs.count() > 0) { | 43 while (fNumObjects > 0) { |
| 33 this->deleteLast(); | 44 fNumObjects--; |
| 45 Rec* rec = &fRecs[fNumObjects]; |
| 46 rec->fKillProc(rec->fObj); |
| 47 // Safe to do if fObj is in fStorage, since fHeapStorage will |
| 48 // point to nullptr. |
| 49 sk_free(rec->fHeapStorage); |
| 34 } | 50 } |
| 35 } | 51 } |
| 36 | 52 |
| 37 /* | 53 /* |
| 38 * Create a new object of type T. Its lifetime will be handled by this | 54 * Create a new object of type T. Its lifetime will be handled by this |
| 39 * SkSmallAllocator. | 55 * SkSmallAllocator. |
| 56 * Note: If kMaxObjects have been created by this SkSmallAllocator, nullptr |
| 57 * will be returned. |
| 40 */ | 58 */ |
| 41 template<typename T, typename... Args> | 59 template<typename T, typename... Args> |
| 42 T* createT(Args&&... args) { | 60 T* createT(Args&&... args) { |
| 43 void* buf = this->reserve(sizeof(T), DefaultDestructor<T>); | 61 void* buf = this->reserveT<T>(); |
| 62 if (nullptr == buf) { |
| 63 return nullptr; |
| 64 } |
| 44 return new (buf) T(std::forward<Args>(args)...); | 65 return new (buf) T(std::forward<Args>(args)...); |
| 45 } | 66 } |
| 46 | 67 |
| 47 /* | 68 /* |
| 48 * Create a new object of size using initer to initialize the memory. The in
iter function has | 69 * Reserve a specified amount of space (must be enough space for one T). |
| 49 * the signature T* initer(void* storage). If initer is unable to initialize
the memory it | 70 * The space will be in fStorage if there is room, or on the heap otherwise
. |
| 50 * should return nullptr where SkSmallAllocator will free the memory. | 71 * Either way, this class will call ~T() in its destructor and free the hea
p |
| 72 * allocation if necessary. |
| 73 * Unlike createT(), this method will not call the constructor of T. |
| 51 */ | 74 */ |
| 52 template <typename T, typename Initer> | 75 template<typename T> void* reserveT(size_t storageRequired = sizeof(T)) { |
| 53 T* createWithIniterT(size_t size, Initer initer) { | 76 SkASSERT(fNumObjects < kMaxObjects); |
| 54 SkASSERT(size >= sizeof(T)); | 77 SkASSERT(storageRequired >= sizeof(T)); |
| 55 | 78 if (kMaxObjects == fNumObjects) { |
| 56 void* storage = this->reserve(size, DefaultDestructor<T>); | 79 return nullptr; |
| 57 T* candidate = initer(storage); | |
| 58 if (!candidate) { | |
| 59 // Initializing didn't workout so free the memory. | |
| 60 this->freeLast(); | |
| 61 } | 80 } |
| 62 | |
| 63 return candidate; | |
| 64 } | |
| 65 | |
| 66 /* | |
| 67 * Free the last object allocated and call its destructor. This can be calle
d multiple times | |
| 68 * removing objects from the pool in reverse order. | |
| 69 */ | |
| 70 void deleteLast() { | |
| 71 SkASSERT(fRecs.count() > 0); | |
| 72 Rec& rec = fRecs.back(); | |
| 73 rec.fDestructor(rec.fObj); | |
| 74 this->freeLast(); | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 using Destructor = void(*)(void*); | |
| 79 struct Rec { | |
| 80 size_t fStorageSize; // 0 if allocated on heap | |
| 81 char* fObj; | |
| 82 Destructor fDestructor; | |
| 83 }; | |
| 84 | |
| 85 // Used to call the destructor for allocated objects. | |
| 86 template<typename T> | |
| 87 static void DefaultDestructor(void* ptr) { | |
| 88 static_cast<T*>(ptr)->~T(); | |
| 89 } | |
| 90 | |
| 91 // Reserve storageRequired from fStorage if possible otherwise allocate on t
he heap. | |
| 92 void* reserve(size_t storageRequired, Destructor destructor) { | |
| 93 const size_t storageRemaining = sizeof(fStorage) - fStorageUsed; | 81 const size_t storageRemaining = sizeof(fStorage) - fStorageUsed; |
| 94 Rec& rec = fRecs.push_back(); | 82 Rec* rec = &fRecs[fNumObjects]; |
| 95 if (storageRequired > storageRemaining) { | 83 if (storageRequired > storageRemaining) { |
| 96 // Allocate on the heap. Ideally we want to avoid this situation. | 84 // Allocate on the heap. Ideally we want to avoid this situation. |
| 97 | 85 |
| 98 // With the gm composeshader_bitmap2, storage required is 4476 | 86 // With the gm composeshader_bitmap2, storage required is 4476 |
| 99 // and storage remaining is 3392. Increasing the base storage | 87 // and storage remaining is 3392. Increasing the base storage |
| 100 // causes google 3 tests to fail. | 88 // causes google 3 tests to fail. |
| 101 | 89 |
| 102 rec.fStorageSize = 0; | 90 rec->fStorageSize = 0; |
| 103 rec.fObj = new char [storageRequired]; | 91 rec->fHeapStorage = sk_malloc_throw(storageRequired); |
| 92 rec->fObj = static_cast<void*>(rec->fHeapStorage); |
| 104 } else { | 93 } else { |
| 105 // There is space in fStorage. | 94 // There is space in fStorage. |
| 106 rec.fStorageSize = storageRequired; | 95 rec->fStorageSize = storageRequired; |
| 107 rec.fObj = &fStorage[fStorageUsed]; | 96 rec->fHeapStorage = nullptr; |
| 97 rec->fObj = static_cast<void*>(fStorage + fStorageUsed); |
| 108 fStorageUsed += storageRequired; | 98 fStorageUsed += storageRequired; |
| 109 } | 99 } |
| 110 rec.fDestructor = destructor; | 100 rec->fKillProc = DestroyT<T>; |
| 111 return rec.fObj; | 101 fNumObjects++; |
| 102 return rec->fObj; |
| 112 } | 103 } |
| 113 | 104 |
| 105 /* |
| 106 * Free the memory reserved last without calling the destructor. |
| 107 * Can be used in a nested way, i.e. after reserving A and B, calling |
| 108 * freeLast once will free B and calling it again will free A. |
| 109 */ |
| 114 void freeLast() { | 110 void freeLast() { |
| 115 Rec& rec = fRecs.back(); | 111 SkASSERT(fNumObjects > 0); |
| 116 if (0 == rec.fStorageSize) { | 112 Rec* rec = &fRecs[fNumObjects - 1]; |
| 117 delete [] rec.fObj; | 113 sk_free(rec->fHeapStorage); |
| 118 } | 114 fStorageUsed -= rec->fStorageSize; |
| 119 fStorageUsed -= rec.fStorageSize; | 115 |
| 120 fRecs.pop_back(); | 116 fNumObjects--; |
| 121 } | 117 } |
| 122 | 118 |
| 123 size_t fStorageUsed {0}; // Number of bytes
used so far. | 119 private: |
| 124 SkSTArray<kExpectedObjects, Rec, true> fRecs; | 120 struct Rec { |
| 125 char fStorage[kTotalBytes]; | 121 size_t fStorageSize; // 0 if allocated on heap |
| 122 void* fObj; |
| 123 void* fHeapStorage; |
| 124 void (*fKillProc)(void*); |
| 125 }; |
| 126 |
| 127 // Used to call the destructor for allocated objects. |
| 128 template<typename T> |
| 129 static void DestroyT(void* ptr) { |
| 130 static_cast<T*>(ptr)->~T(); |
| 131 } |
| 132 |
| 133 alignas(16) char fStorage[kTotalBytes]; |
| 134 size_t fStorageUsed; // Number of bytes used so far. |
| 135 uint32_t fNumObjects; |
| 136 Rec fRecs[kMaxObjects]; |
| 126 }; | 137 }; |
| 127 | 138 |
| 128 #endif // SkSmallAllocator_DEFINED | 139 #endif // SkSmallAllocator_DEFINED |
| OLD | NEW |