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 24 matching lines...) Expand all Loading... | |
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) { |
43 fNumObjects--; | 43 fNumObjects--; |
44 Rec* rec = &fRecs[fNumObjects]; | 44 Rec* rec = &fRecs[fNumObjects]; |
45 rec->fKillProc(rec->fObj); | 45 if (rec->fObj) { |
46 rec->fKillProc(rec->fObj); | |
47 } | |
46 // Safe to do if fObj is in fStorage, since fHeapStorage will | 48 // Safe to do if fObj is in fStorage, since fHeapStorage will |
47 // point to NULL. | 49 // point to NULL. |
48 sk_free(rec->fHeapStorage); | 50 sk_free(rec->fHeapStorage); |
49 } | 51 } |
50 } | 52 } |
51 | 53 |
52 /* | 54 /* |
53 * Create a new object of type T. Its lifetime will be handled by this | 55 * Create a new object of type T. Its lifetime will be handled by this |
54 * SkSmallAllocator. | 56 * SkSmallAllocator. |
55 * Each version behaves the same but takes a different number of | 57 * Each version behaves the same but takes a different number of |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 rec->fHeapStorage = NULL; | 126 rec->fHeapStorage = NULL; |
125 SkASSERT(SkIsAlign4(fStorageUsed)); | 127 SkASSERT(SkIsAlign4(fStorageUsed)); |
126 rec->fObj = static_cast<void*>(fStorage + (fStorageUsed / 4)); | 128 rec->fObj = static_cast<void*>(fStorage + (fStorageUsed / 4)); |
127 fStorageUsed += storageRequired; | 129 fStorageUsed += storageRequired; |
128 } | 130 } |
129 rec->fKillProc = destroyT<T>; | 131 rec->fKillProc = destroyT<T>; |
130 fNumObjects++; | 132 fNumObjects++; |
131 return rec->fObj; | 133 return rec->fObj; |
132 } | 134 } |
133 | 135 |
136 /* | |
137 * Free the memory reserved for an object without calling its destructor. | |
138 * Assumes the passed-in pointer has previously been returned by either | |
139 * createT or reserveT. | |
140 */ | |
141 void free(void* obj) { | |
142 // Find the corresponding Rec. | |
scroggo
2014/03/24 21:24:46
Do we think it's necessary to allow freeing anythi
Dominik Grewe
2014/03/26 17:22:22
Sounds good to me. I've implemented it. If we also
scroggo
2014/03/26 23:13:09
That seems reasonable. It might be nice to know ho
Dominik Grewe
2014/03/27 14:27:20
During the tests (excluding the ones using gradien
| |
143 Rec* rec = NULL; | |
144 for (uint32_t i = 0; i < fNumObjects; i++) { | |
145 if (fRecs[i].fObj == obj) { | |
146 rec = &fRecs[i]; | |
147 break; | |
148 } | |
149 } | |
150 SkASSERT(rec); | |
151 // The rest of the cleanup will be done in the destructor. | |
152 rec->fObj = NULL; | |
153 } | |
154 | |
134 private: | 155 private: |
135 struct Rec { | 156 struct Rec { |
136 void* fObj; | 157 void* fObj; |
137 void* fHeapStorage; | 158 void* fHeapStorage; |
138 void (*fKillProc)(void*); | 159 void (*fKillProc)(void*); |
139 }; | 160 }; |
140 | 161 |
141 // Number of bytes used so far. | 162 // Number of bytes used so far. |
142 size_t fStorageUsed; | 163 size_t fStorageUsed; |
143 // Pad the storage size to be 4-byte aligned. | 164 // Pad the storage size to be 4-byte aligned. |
144 uint32_t fStorage[SkAlign4(kTotalBytes) >> 2]; | 165 uint32_t fStorage[SkAlign4(kTotalBytes) >> 2]; |
145 uint32_t fNumObjects; | 166 uint32_t fNumObjects; |
146 Rec fRecs[kMaxObjects]; | 167 Rec fRecs[kMaxObjects]; |
147 }; | 168 }; |
148 | 169 |
149 #endif // SkSmallAllocator_DEFINED | 170 #endif // SkSmallAllocator_DEFINED |
OLD | NEW |