| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 // and storage remaining is 3392. Increasing the base storage | 86 // and storage remaining is 3392. Increasing the base storage |
| 87 // causes google 3 tests to fail. | 87 // causes google 3 tests to fail. |
| 88 | 88 |
| 89 rec->fStorageSize = 0; | 89 rec->fStorageSize = 0; |
| 90 rec->fHeapStorage = sk_malloc_throw(storageRequired); | 90 rec->fHeapStorage = sk_malloc_throw(storageRequired); |
| 91 rec->fObj = static_cast<void*>(rec->fHeapStorage); | 91 rec->fObj = static_cast<void*>(rec->fHeapStorage); |
| 92 } else { | 92 } else { |
| 93 // There is space in fStorage. | 93 // There is space in fStorage. |
| 94 rec->fStorageSize = storageRequired; | 94 rec->fStorageSize = storageRequired; |
| 95 rec->fHeapStorage = nullptr; | 95 rec->fHeapStorage = nullptr; |
| 96 rec->fObj = static_cast<void*>(fStorage.fBytes + fStorageUsed); | 96 rec->fObj = static_cast<void*>(fStorage + fStorageUsed); |
| 97 fStorageUsed += storageRequired; | 97 fStorageUsed += storageRequired; |
| 98 } | 98 } |
| 99 rec->fKillProc = DestroyT<T>; | 99 rec->fKillProc = DestroyT<T>; |
| 100 fNumObjects++; | 100 fNumObjects++; |
| 101 return rec->fObj; | 101 return rec->fObj; |
| 102 } | 102 } |
| 103 | 103 |
| 104 /* | 104 /* |
| 105 * Free the memory reserved last without calling the destructor. | 105 * Free the memory reserved last without calling the destructor. |
| 106 * Can be used in a nested way, i.e. after reserving A and B, calling | 106 * Can be used in a nested way, i.e. after reserving A and B, calling |
| (...skipping 15 matching lines...) Expand all Loading... |
| 122 void* fHeapStorage; | 122 void* fHeapStorage; |
| 123 void (*fKillProc)(void*); | 123 void (*fKillProc)(void*); |
| 124 }; | 124 }; |
| 125 | 125 |
| 126 // Used to call the destructor for allocated objects. | 126 // Used to call the destructor for allocated objects. |
| 127 template<typename T> | 127 template<typename T> |
| 128 static void DestroyT(void* ptr) { | 128 static void DestroyT(void* ptr) { |
| 129 static_cast<T*>(ptr)->~T(); | 129 static_cast<T*>(ptr)->~T(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 struct SK_STRUCT_ALIGN(16) Storage { | 132 // This could be implemented using std::aligned_storage, but MSVC does not i
mplement this. Use the |
| 133 // we add kMaxObjects * 15 to account for the worst-case slop, where eac
h allocation wasted | 133 // work around from: |
| 134 // 15 bytes (due to forcing each to be 16-byte aligned) | 134 // https://connect.microsoft.com/VisualStudio/feedback/details/1559873/std-a
ligned-storage-cannot-align-type-with-16-byte |
| 135 char fBytes[kTotalBytes + kMaxObjects * 15]; | 135 alignas(16) char fStorage[kTotalBytes]; |
| 136 }; | |
| 137 | |
| 138 Storage fStorage; | |
| 139 // Number of bytes used so far. | 136 // Number of bytes used so far. |
| 140 size_t fStorageUsed; | 137 size_t fStorageUsed; |
| 141 uint32_t fNumObjects; | 138 uint32_t fNumObjects; |
| 142 Rec fRecs[kMaxObjects]; | 139 Rec fRecs[kMaxObjects]; |
| 143 }; | 140 }; |
| 144 | 141 |
| 145 #endif // SkSmallAllocator_DEFINED | 142 #endif // SkSmallAllocator_DEFINED |
| OLD | NEW |