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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 rec->fHeapStorage = NULL; | 124 rec->fHeapStorage = NULL; |
125 SkASSERT(SkIsAlign4(fStorageUsed)); | 125 SkASSERT(SkIsAlign4(fStorageUsed)); |
126 rec->fObj = static_cast<void*>(fStorage + (fStorageUsed / 4)); | 126 rec->fObj = static_cast<void*>(fStorage + (fStorageUsed / 4)); |
127 fStorageUsed += storageRequired; | 127 fStorageUsed += storageRequired; |
128 } | 128 } |
129 rec->fKillProc = destroyT<T>; | 129 rec->fKillProc = destroyT<T>; |
130 fNumObjects++; | 130 fNumObjects++; |
131 return rec->fObj; | 131 return rec->fObj; |
132 } | 132 } |
133 | 133 |
| 134 /* |
| 135 * Free the memory reserved last without calling the destructor. |
| 136 * Can be used in a nested way, i.e. after reserving A and B, calling |
| 137 * freeLast once will free B and calling it again will free A. |
| 138 * Note: the available memory will not be made available to future |
| 139 * allocations. |
| 140 */ |
| 141 void freeLast() { |
| 142 SkASSERT(fNumObjects > 0); |
| 143 Rec* rec = &fRecs[fNumObjects - 1]; |
| 144 sk_free(rec->fHeapStorage); |
| 145 |
| 146 fNumObjects--; |
| 147 } |
| 148 |
134 private: | 149 private: |
135 struct Rec { | 150 struct Rec { |
136 void* fObj; | 151 void* fObj; |
137 void* fHeapStorage; | 152 void* fHeapStorage; |
138 void (*fKillProc)(void*); | 153 void (*fKillProc)(void*); |
139 }; | 154 }; |
140 | 155 |
141 // Number of bytes used so far. | 156 // Number of bytes used so far. |
142 size_t fStorageUsed; | 157 size_t fStorageUsed; |
143 // Pad the storage size to be 4-byte aligned. | 158 // Pad the storage size to be 4-byte aligned. |
144 uint32_t fStorage[SkAlign4(kTotalBytes) >> 2]; | 159 uint32_t fStorage[SkAlign4(kTotalBytes) >> 2]; |
145 uint32_t fNumObjects; | 160 uint32_t fNumObjects; |
146 Rec fRecs[kMaxObjects]; | 161 Rec fRecs[kMaxObjects]; |
147 }; | 162 }; |
148 | 163 |
149 #endif // SkSmallAllocator_DEFINED | 164 #endif // SkSmallAllocator_DEFINED |
OLD | NEW |