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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 return new (buf) T(args...); | 64 return new (buf) T(args...); |
65 } | 65 } |
66 | 66 |
67 /* | 67 /* |
68 * Reserve a specified amount of space (must be enough space for one T). | 68 * Reserve a specified amount of space (must be enough space for one T). |
69 * The space will be in fStorage if there is room, or on the heap otherwise
. | 69 * The space will be in fStorage if there is room, or on the heap otherwise
. |
70 * Either way, this class will call ~T() in its destructor and free the hea
p | 70 * Either way, this class will call ~T() in its destructor and free the hea
p |
71 * allocation if necessary. | 71 * allocation if necessary. |
72 * Unlike createT(), this method will not call the constructor of T. | 72 * Unlike createT(), this method will not call the constructor of T. |
73 */ | 73 */ |
74 template<typename T> void* reserveT(size_t storageRequested = sizeof(T)) { | 74 template<typename T> void* reserveT(size_t storageRequired = sizeof(T)) { |
75 SkASSERT(fNumObjects < kMaxObjects); | 75 SkASSERT(fNumObjects < kMaxObjects); |
76 SkASSERT(storageRequested >= sizeof(T)); | 76 SkASSERT(storageRequired >= sizeof(T)); |
77 if (kMaxObjects == fNumObjects) { | 77 if (kMaxObjects == fNumObjects) { |
78 return nullptr; | 78 return nullptr; |
79 } | 79 } |
80 const size_t storageRemaining = sizeof(fStorage) - fStorageUsed; | 80 const size_t storageRemaining = sizeof(fStorage) - fStorageUsed; |
81 const size_t storageRequired = SkAlign16(storageRequested); | |
82 Rec* rec = &fRecs[fNumObjects]; | 81 Rec* rec = &fRecs[fNumObjects]; |
83 if (storageRequired > storageRemaining) { | 82 if (storageRequired > storageRemaining) { |
84 // Allocate on the heap. Ideally we want to avoid this situation, | 83 // Allocate on the heap. Ideally we want to avoid this situation, |
85 // but we're not sure we can catch all callers, so handle it but | 84 // but we're not sure we can catch all callers, so handle it but |
86 // assert false in debug mode. | 85 // assert false in debug mode. |
87 SkASSERT(false); | 86 SkASSERT(false); |
88 rec->fStorageSize = 0; | 87 rec->fStorageSize = 0; |
89 rec->fHeapStorage = sk_malloc_throw(storageRequired); | 88 rec->fHeapStorage = sk_malloc_throw(storageRequired); |
90 rec->fObj = static_cast<void*>(rec->fHeapStorage); | 89 rec->fObj = static_cast<void*>(rec->fHeapStorage); |
91 } else { | 90 } else { |
92 // There is space in fStorage. | 91 // There is space in fStorage. |
93 rec->fStorageSize = storageRequired; | 92 rec->fStorageSize = storageRequired; |
94 rec->fHeapStorage = nullptr; | 93 rec->fHeapStorage = nullptr; |
95 SkASSERT(SkIsAlign16(fStorageUsed)); | |
96 rec->fObj = static_cast<void*>(fStorage.fBytes + fStorageUsed); | 94 rec->fObj = static_cast<void*>(fStorage.fBytes + fStorageUsed); |
97 fStorageUsed += storageRequired; | 95 fStorageUsed += storageRequired; |
98 } | 96 } |
99 rec->fKillProc = DestroyT<T>; | 97 rec->fKillProc = DestroyT<T>; |
100 fNumObjects++; | 98 fNumObjects++; |
101 return rec->fObj; | 99 return rec->fObj; |
102 } | 100 } |
103 | 101 |
104 /* | 102 /* |
105 * Free the memory reserved last without calling the destructor. | 103 * Free the memory reserved last without calling the destructor. |
(...skipping 30 matching lines...) Expand all Loading... |
136 }; | 134 }; |
137 | 135 |
138 Storage fStorage; | 136 Storage fStorage; |
139 // Number of bytes used so far. | 137 // Number of bytes used so far. |
140 size_t fStorageUsed; | 138 size_t fStorageUsed; |
141 uint32_t fNumObjects; | 139 uint32_t fNumObjects; |
142 Rec fRecs[kMaxObjects]; | 140 Rec fRecs[kMaxObjects]; |
143 }; | 141 }; |
144 | 142 |
145 #endif // SkSmallAllocator_DEFINED | 143 #endif // SkSmallAllocator_DEFINED |
OLD | NEW |