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

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

Issue 2488523003: Make SkSmallAllocator obey the RAII invariants and be expandable (Closed)
Patch Set: Desturctor name change 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
« no previous file with comments | « src/core/SkRasterPipelineBlitter.cpp ('k') | tests/LayerDrawLooperTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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), DefaultDestructor<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) {
mtklein_C 2016/11/09 20:47:57 Can't we infer T? template <typename Initer> auto
77 SkASSERT(storageRequired >= sizeof(T)); 54 SkASSERT(size >= sizeof(T));
78 if (kMaxObjects == fNumObjects) { 55
79 return nullptr; 56 void* storage = this->reserve(size, DefaultDestructor<T>);
57 T* candidate = initer(storage);
58 if (!candidate) {
59 // Initializing didn't workout so free the memory.
60 this->freeLast();
80 } 61 }
62
63 return candidate;
64 }
65
66 /*
67 * Free the last object allocated and call its destructor. This can be calle d multiple times
68 * removing objects from the pool in reverse order.
69 */
70 void deleteLast() {
71 SkASSERT(fRecs.count() > 0);
72 Rec& rec = fRecs.back();
73 rec.fDestructor(rec.fObj);
74 this->freeLast();
75 }
76
77 private:
78 using Destructor = void(*)(void*);
79 struct Rec {
80 size_t fStorageSize; // 0 if allocated on heap
81 char* fObj;
82 Destructor fDestructor;
83 };
84
85 // Used to call the destructor for allocated objects.
86 template<typename T>
87 static void DefaultDestructor(void* ptr) {
88 static_cast<T*>(ptr)->~T();
89 }
90
91 // Reserve storageRequired from fStorage if possible otherwise allocate on t he heap.
92 void* reserve(size_t storageRequired, Destructor destructor) {
81 const size_t storageRemaining = sizeof(fStorage) - fStorageUsed; 93 const size_t storageRemaining = sizeof(fStorage) - fStorageUsed;
82 Rec* rec = &fRecs[fNumObjects]; 94 Rec& rec = fRecs.push_back();
83 if (storageRequired > storageRemaining) { 95 if (storageRequired > storageRemaining) {
84 // Allocate on the heap. Ideally we want to avoid this situation. 96 // Allocate on the heap. Ideally we want to avoid this situation.
85 97
86 // With the gm composeshader_bitmap2, storage required is 4476 98 // With the gm composeshader_bitmap2, storage required is 4476
87 // and storage remaining is 3392. Increasing the base storage 99 // and storage remaining is 3392. Increasing the base storage
88 // causes google 3 tests to fail. 100 // causes google 3 tests to fail.
89 101
90 rec->fStorageSize = 0; 102 rec.fStorageSize = 0;
91 rec->fHeapStorage = sk_malloc_throw(storageRequired); 103 rec.fObj = new char [storageRequired];
92 rec->fObj = static_cast<void*>(rec->fHeapStorage);
93 } else { 104 } else {
94 // There is space in fStorage. 105 // There is space in fStorage.
95 rec->fStorageSize = storageRequired; 106 rec.fStorageSize = storageRequired;
96 rec->fHeapStorage = nullptr; 107 rec.fObj = &fStorage[fStorageUsed];
97 rec->fObj = static_cast<void*>(fStorage + fStorageUsed);
98 fStorageUsed += storageRequired; 108 fStorageUsed += storageRequired;
99 } 109 }
100 rec->fKillProc = DestroyT<T>; 110 rec.fDestructor = destructor;
101 fNumObjects++; 111 return rec.fObj;
102 return rec->fObj;
103 } 112 }
104 113
105 /*
106 * Free the memory reserved last without calling the destructor.
107 * Can be used in a nested way, i.e. after reserving A and B, calling
108 * freeLast once will free B and calling it again will free A.
109 */
110 void freeLast() { 114 void freeLast() {
111 SkASSERT(fNumObjects > 0); 115 Rec& rec = fRecs.back();
112 Rec* rec = &fRecs[fNumObjects - 1]; 116 if (0 == rec.fStorageSize) {
113 sk_free(rec->fHeapStorage); 117 delete [] rec.fObj;
114 fStorageUsed -= rec->fStorageSize; 118 }
115 119 fStorageUsed -= rec.fStorageSize;
116 fNumObjects--; 120 fRecs.pop_back();
117 } 121 }
118 122
119 private: 123 size_t fStorageUsed {0}; // Number of bytes used so far.
120 struct Rec { 124 SkSTArray<kExpectedObjects, Rec, true> fRecs;
121 size_t fStorageSize; // 0 if allocated on heap 125 char fStorage[kTotalBytes];
122 void* fObj;
123 void* fHeapStorage;
124 void (*fKillProc)(void*);
125 };
126
127 // Used to call the destructor for allocated objects.
128 template<typename T>
129 static void DestroyT(void* ptr) {
130 static_cast<T*>(ptr)->~T();
131 }
132
133 alignas(16) char fStorage[kTotalBytes];
134 size_t fStorageUsed; // Number of bytes used so far.
135 uint32_t fNumObjects;
136 Rec fRecs[kMaxObjects];
137 }; 126 };
138 127
139 #endif // SkSmallAllocator_DEFINED 128 #endif // SkSmallAllocator_DEFINED
OLDNEW
« no previous file with comments | « src/core/SkRasterPipelineBlitter.cpp ('k') | tests/LayerDrawLooperTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698