Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: src/core/SkSmallAllocator.h

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

Powered by Google App Engine
This is Rietveld 408576698