OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2014 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #ifndef GrTRecorder_DEFINED | |
9 #define GrTRecorder_DEFINED | |
10 | |
11 #include "SkTemplates.h" | |
12 #include "SkTypes.h" | |
13 | |
14 template<typename TBase, typename TAlign> class GrTRecorder; | |
15 template<typename TItem> struct GrTRecorderAllocWrapper; | |
16 | |
17 /** | |
18 * Records a list of items with a common base type, optional associated data, an d | |
19 * permanent memory addresses. | |
20 * | |
21 * This class preallocates its own chunks of memory for hosting objects, so new items can | |
22 * be created without excessive calls to malloc(). | |
23 * | |
24 * To create a new item and append it to the back of the list, use the following macros: | |
25 * | |
26 * GrNEW_APPEND_TO_RECORDER(recorder, SubclassName, (args)) | |
27 * GrNEW_APPEND_WITH_DATA_TO_RECORDER(recorder, SubclassName, (args), sizeOf Data) | |
28 * | |
29 * Upon reset or delete, the items are destructed in the same order they were re ceived, | |
30 * not reverse (stack) order. | |
31 * | |
32 * @param TBase Common base type of items in the list. If TBase is not a class with a | |
33 * virtual destructor, the client is responsible for invoking any necessary | |
34 * destructors. | |
35 * | |
36 * For now, any subclass used in the list must have the same star t address | |
37 * as TBase (or in other words, the types must be convertible via | |
38 * reinterpret_cast<>). Classes with multiple inheritance (or any subclass | |
39 * on an obscure compiler) may not be compatible. This is runtime asserted | |
40 * in debug builds. | |
41 * | |
42 * @param TAlign A type whose size is the desired memory alignment for object a llocations. | |
43 * This should be the largest known alignment requirement for all objects | |
44 * that may be stored in the list. | |
45 */ | |
46 template<typename TBase, typename TAlign> class GrTRecorder : SkNoncopyable { | |
47 public: | |
48 class Iter; | |
49 | |
50 /** | |
51 * Create a recorder. | |
52 * | |
53 * @param initialSizeInBytes The amount of memory reserved by the recorder initially, | |
54 and after calls to reset(). | |
55 */ | |
56 GrTRecorder(int initialSizeInBytes) | |
57 : fHeadBlock(MemBlock::Alloc(LengthOf(initialSizeInBytes))), | |
58 fTailBlock(fHeadBlock), | |
59 fLastItem(NULL) {} | |
60 | |
61 ~GrTRecorder() { | |
62 this->reset(); | |
63 MemBlock::Free(fHeadBlock); | |
64 } | |
65 | |
66 bool empty() { return !fLastItem; } | |
67 | |
68 TBase& back() { | |
69 SkASSERT(!this->empty()); | |
70 return *fLastItem; | |
71 } | |
72 | |
73 /** | |
74 * Destruct all items in the list and reset to empty. | |
75 */ | |
76 void reset(); | |
77 | |
78 /** | |
79 * Retrieve the extra data associated with an item that was allocated using | |
80 * GrNEW_APPEND_WITH_DATA_TO_RECORDER(). | |
81 * | |
82 * @param item The item whose data to retrieve. The pointer must be of the same type | |
83 * that was allocated initally; it can't be a pointer to a base class. | |
84 * | |
85 * @return The item's associated data. | |
86 */ | |
87 template<typename TItem> static const void* GetDataForItem(const TItem* item ) { | |
88 const TAlign* ptr = reinterpret_cast<const TAlign*>(item); | |
89 return &ptr[length_of<TItem>::kValue]; | |
90 } | |
91 template<typename TItem> static void* GetDataForItem(TItem* item) { | |
92 TAlign* ptr = reinterpret_cast<TAlign*>(item); | |
93 return &ptr[length_of<TItem>::kValue]; | |
94 } | |
95 | |
96 private: | |
97 template<typename TItem> struct length_of { | |
98 enum { kValue = (sizeof(TItem) + sizeof(TAlign) - 1) / sizeof(TAlign) }; | |
99 }; | |
100 static int LengthOf(int bytes) { return (bytes + sizeof(TAlign) - 1) / sizeo f(TAlign); } | |
101 | |
102 struct Header { | |
103 int fTotalLength; | |
104 }; | |
105 template<typename TItem> TItem* alloc_back(int dataLength); | |
106 | |
107 struct MemBlock { | |
108 static MemBlock* Alloc(int length) { | |
109 MemBlock* block = reinterpret_cast<MemBlock*>( | |
110 sk_malloc_throw(sizeof(TAlign) * (length_of<MemBlock>::kValue + length))); | |
111 block->fLength = length; | |
112 block->fBack = 0; | |
113 block->fNext = NULL; | |
114 return block; | |
115 } | |
116 static void Free(MemBlock* block) { | |
117 if (!block) { | |
118 return; | |
119 } | |
120 Free(block->fNext); | |
121 sk_free(block); | |
122 } | |
123 | |
124 int fLength; | |
125 int fBack; | |
126 MemBlock* fNext; | |
127 TAlign& operator [](int i) { | |
bsalomon
2014/10/15 18:38:06
can you group this with the other methods? otherwi
Chris Dalton
2014/10/15 20:10:13
Done.
| |
128 return reinterpret_cast<TAlign*>(this)[length_of<MemBlock>::kValue + i]; | |
129 } | |
130 }; | |
131 MemBlock* const fHeadBlock; | |
132 MemBlock* fTailBlock; | |
133 | |
134 TBase* fLastItem; | |
135 | |
136 template<typename TItem> friend struct GrTRecorderAllocWrapper; | |
137 | |
138 template <typename UBase, typename UAlign, typename UAlloc> | |
139 friend void* operator new(size_t, GrTRecorder<UBase, UAlign>&, | |
140 const GrTRecorderAllocWrapper<UAlloc>&); | |
141 | |
142 friend class Iter; | |
143 }; | |
144 | |
145 //////////////////////////////////////////////////////////////////////////////// | |
146 | |
147 template<typename TBase, typename TAlign> | |
148 template<typename TItem> | |
149 TItem* GrTRecorder<TBase, TAlign>::alloc_back(int dataLength) { | |
150 const int totalLength = length_of<Header>::kValue + length_of<TItem>::kValue + dataLength; | |
151 | |
152 if (fTailBlock->fBack + totalLength > fTailBlock->fLength) { | |
153 SkASSERT(!fTailBlock->fNext); | |
154 fTailBlock->fNext = MemBlock::Alloc(SkTMax(2 * fTailBlock->fLength, tota lLength)); | |
155 fTailBlock = fTailBlock->fNext; | |
156 } | |
157 | |
158 Header* header = reinterpret_cast<Header*>(&(*fTailBlock)[fTailBlock->fBack] ); | |
159 TItem* rawPtr = reinterpret_cast<TItem*>( | |
160 &(*fTailBlock)[fTailBlock->fBack + length_of<Header>::kV alue]); | |
161 | |
162 header->fTotalLength = totalLength; | |
163 fLastItem = rawPtr; | |
164 fTailBlock->fBack += totalLength; | |
165 | |
166 // FIXME: We currently require that the base and subclass share the same sta rt address. | |
167 // This is not required by the C++ spec, and is likely to not be true in the case of | |
168 // multiple inheritance or a base class that doesn't have virtual methods (w hen the | |
169 // subclass does). It would be ideal to find a more robust solution that com es at no | |
170 // extra cost to performance or code generality. | |
171 SkDEBUGCODE(void* baseAddr = fLastItem; | |
172 void* subclassAddr = rawPtr); | |
173 SkASSERT(baseAddr == subclassAddr); | |
174 | |
175 return rawPtr; | |
176 } | |
177 | |
178 template<typename TBase, typename TAlign> | |
179 class GrTRecorder<TBase, TAlign>::Iter { | |
180 public: | |
181 Iter(GrTRecorder& recorder) : fBlock(recorder.fHeadBlock), fPosition(0), fIt em(NULL) {} | |
182 | |
183 bool next() { | |
184 if (fPosition >= fBlock->fBack) { | |
185 SkASSERT(fPosition == fBlock->fBack); | |
186 if (!fBlock->fNext) { | |
187 return false; | |
188 } | |
189 SkASSERT(0 != fBlock->fNext->fBack); | |
190 fBlock = fBlock->fNext; | |
191 fPosition = 0; | |
192 } | |
193 | |
194 Header* header = reinterpret_cast<Header*>(&(*fBlock)[fPosition]); | |
195 fItem = reinterpret_cast<TBase*>(&(*fBlock)[fPosition + length_of<Header >::kValue]); | |
196 fPosition += header->fTotalLength; | |
197 return true; | |
198 } | |
199 | |
200 TBase* get() const { | |
201 SkASSERT(fItem); | |
202 return fItem; | |
203 } | |
204 | |
205 TBase* operator->() const { return this->get(); } | |
206 | |
207 private: | |
208 MemBlock* fBlock; | |
209 int fPosition; | |
210 TBase* fItem; | |
211 }; | |
212 | |
213 template<typename TBase, typename TAlign> | |
214 void GrTRecorder<TBase, TAlign>::reset() { | |
215 Iter iter(*this); | |
216 while (iter.next()) { | |
217 iter->~TBase(); | |
218 } | |
219 fHeadBlock->fBack = 0; | |
220 MemBlock::Free(fHeadBlock->fNext); | |
221 fHeadBlock->fNext = NULL; | |
222 fTailBlock = fHeadBlock; | |
223 fLastItem = NULL; | |
224 } | |
225 | |
226 //////////////////////////////////////////////////////////////////////////////// | |
227 | |
228 template<typename TItem> struct GrTRecorderAllocWrapper { | |
229 GrTRecorderAllocWrapper() : fDataLength(0) {} | |
230 | |
231 template <typename TBase, typename TAlign> | |
232 GrTRecorderAllocWrapper(const GrTRecorder<TBase, TAlign>&, int sizeOfData) | |
233 : fDataLength(GrTRecorder<TBase, TAlign>::LengthOf(sizeOfData)) {} | |
234 | |
235 const int fDataLength; | |
236 }; | |
237 | |
238 template <typename TBase, typename TAlign, typename TItem> | |
239 void* operator new(size_t size, GrTRecorder<TBase, TAlign>& recorder, | |
240 const GrTRecorderAllocWrapper<TItem>& wrapper) { | |
241 SkASSERT(size == sizeof(TItem)); | |
242 return recorder.template alloc_back<TItem>(wrapper.fDataLength); | |
243 } | |
244 | |
245 template <typename TBase, typename TAlign, typename TItem> | |
246 void operator delete(void*, GrTRecorder<TBase, TAlign>&, const GrTRecorderAllocW rapper<TItem>&) { | |
247 // We only provide an operator delete to work around compiler warnings that can come | |
248 // up for an unmatched operator new when compiling with exceptions. | |
249 SK_CRASH(); | |
250 } | |
251 | |
252 #define GrNEW_APPEND_TO_RECORDER(recorder, type_name, args) \ | |
253 (new (recorder, GrTRecorderAllocWrapper<type_name>()) type_name args) | |
254 | |
255 #define GrNEW_APPEND_WITH_DATA_TO_RECORDER(recorder, type_name, args, size_of_da ta) \ | |
256 (new (recorder, GrTRecorderAllocWrapper<type_name>(recorder, size_of_data)) type_name args) | |
257 | |
258 #endif | |
OLD | NEW |