blob: 1c49c86f385abffc78951a21a82ef1e8506d251a [file] [log] [blame]
cdalton6819df32014-10-15 13:43:48 -07001/*
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
14template<typename TBase, typename TAlign> class GrTRecorder;
15template<typename TItem> struct GrTRecorderAllocWrapper;
16
17/**
18 * Records a list of items with a common base type, optional associated data, and
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), sizeOfData)
28 *
29 * Upon reset or delete, the items are destructed in the same order they were received,
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 start 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 allocations.
43 * This should be the largest known alignment requirement for all objects
44 * that may be stored in the list.
45 */
46template<typename TBase, typename TAlign> class GrTRecorder : SkNoncopyable {
47public:
48 class Iter;
cdalton72badbd2015-04-16 10:42:49 -070049 class ReverseIter;
cdalton6819df32014-10-15 13:43:48 -070050
51 /**
52 * Create a recorder.
53 *
54 * @param initialSizeInBytes The amount of memory reserved by the recorder initially,
55 and after calls to reset().
56 */
57 GrTRecorder(int initialSizeInBytes)
bsalomon77d77f42014-11-21 14:38:06 -080058 : fHeadBlock(MemBlock::Alloc(LengthOf(initialSizeInBytes), NULL)),
cdalton6819df32014-10-15 13:43:48 -070059 fTailBlock(fHeadBlock),
60 fLastItem(NULL) {}
61
62 ~GrTRecorder() {
63 this->reset();
64 MemBlock::Free(fHeadBlock);
65 }
66
67 bool empty() { return !fLastItem; }
68
69 TBase& back() {
70 SkASSERT(!this->empty());
71 return *fLastItem;
72 }
73
74 /**
bsalomon77d77f42014-11-21 14:38:06 -080075 * Removes and destroys the last block added to the recorder. It may not be called when the
76 * recorder is empty.
77 */
78 void pop_back();
79
80 /**
cdalton6819df32014-10-15 13:43:48 -070081 * Destruct all items in the list and reset to empty.
82 */
83 void reset();
84
85 /**
86 * Retrieve the extra data associated with an item that was allocated using
87 * GrNEW_APPEND_WITH_DATA_TO_RECORDER().
88 *
89 * @param item The item whose data to retrieve. The pointer must be of the same type
90 * that was allocated initally; it can't be a pointer to a base class.
91 *
92 * @return The item's associated data.
93 */
94 template<typename TItem> static const void* GetDataForItem(const TItem* item) {
95 const TAlign* ptr = reinterpret_cast<const TAlign*>(item);
96 return &ptr[length_of<TItem>::kValue];
97 }
98 template<typename TItem> static void* GetDataForItem(TItem* item) {
99 TAlign* ptr = reinterpret_cast<TAlign*>(item);
100 return &ptr[length_of<TItem>::kValue];
101 }
102
103private:
104 template<typename TItem> struct length_of {
105 enum { kValue = (sizeof(TItem) + sizeof(TAlign) - 1) / sizeof(TAlign) };
106 };
107 static int LengthOf(int bytes) { return (bytes + sizeof(TAlign) - 1) / sizeof(TAlign); }
108
109 struct Header {
bsalomon77d77f42014-11-21 14:38:06 -0800110 int fTotalLength; // The length of an entry including header, item, and data in TAligns.
111 int fPrevLength; // Same but for the previous entry. Used for iterating backwards.
cdalton6819df32014-10-15 13:43:48 -0700112 };
113 template<typename TItem> TItem* alloc_back(int dataLength);
114
115 struct MemBlock : SkNoncopyable {
bsalomon77d77f42014-11-21 14:38:06 -0800116 /** Allocates a new block and appends it to prev if not NULL. The length param is in units
117 of TAlign. */
118 static MemBlock* Alloc(int length, MemBlock* prev) {
cdalton6819df32014-10-15 13:43:48 -0700119 MemBlock* block = reinterpret_cast<MemBlock*>(
120 sk_malloc_throw(sizeof(TAlign) * (length_of<MemBlock>::kValue + length)));
121 block->fLength = length;
122 block->fBack = 0;
123 block->fNext = NULL;
bsalomon77d77f42014-11-21 14:38:06 -0800124 block->fPrev = prev;
125 if (prev) {
126 SkASSERT(NULL == prev->fNext);
127 prev->fNext = block;
128 }
cdalton6819df32014-10-15 13:43:48 -0700129 return block;
130 }
131
bsalomon77d77f42014-11-21 14:38:06 -0800132 // Frees from this block forward. Also adjusts prev block's next ptr.
cdalton6819df32014-10-15 13:43:48 -0700133 static void Free(MemBlock* block) {
bsalomon77d77f42014-11-21 14:38:06 -0800134 if (block && block->fPrev) {
135 SkASSERT(block->fPrev->fNext == block);
136 block->fPrev->fNext = NULL;
cdalton6819df32014-10-15 13:43:48 -0700137 }
bsalomon77d77f42014-11-21 14:38:06 -0800138 while (block) {
139 MemBlock* next = block->fNext;
140 sk_free(block);
141 block = next;
142 }
cdalton6819df32014-10-15 13:43:48 -0700143 }
144
145 TAlign& operator [](int i) {
146 return reinterpret_cast<TAlign*>(this)[length_of<MemBlock>::kValue + i];
147 }
148
bsalomon77d77f42014-11-21 14:38:06 -0800149 int fLength; // Length in units of TAlign of the block.
150 int fBack; // Offset, in TAligns, to unused portion of the memory block.
cdalton6819df32014-10-15 13:43:48 -0700151 MemBlock* fNext;
bsalomon77d77f42014-11-21 14:38:06 -0800152 MemBlock* fPrev;
cdalton6819df32014-10-15 13:43:48 -0700153 };
154 MemBlock* const fHeadBlock;
155 MemBlock* fTailBlock;
156
157 TBase* fLastItem;
158
159 template<typename TItem> friend struct GrTRecorderAllocWrapper;
160
161 template <typename UBase, typename UAlign, typename UItem>
162 friend void* operator new(size_t, GrTRecorder<UBase, UAlign>&,
163 const GrTRecorderAllocWrapper<UItem>&);
164
165 friend class Iter;
cdalton72badbd2015-04-16 10:42:49 -0700166 friend class ReverseIter;
cdalton6819df32014-10-15 13:43:48 -0700167};
168
169////////////////////////////////////////////////////////////////////////////////
170
171template<typename TBase, typename TAlign>
bsalomon77d77f42014-11-21 14:38:06 -0800172void GrTRecorder<TBase, TAlign>::pop_back() {
173 SkASSERT(fLastItem);
174 Header* header = reinterpret_cast<Header*>(
175 reinterpret_cast<TAlign*>(fLastItem) - length_of<Header>::kValue);
176 fTailBlock->fBack -= header->fTotalLength;
177 fLastItem->~TBase();
178
179 int lastItemLength = header->fPrevLength;
180
181 if (!header->fPrevLength) {
182 // We popped the first entry in the recorder.
183 SkASSERT(0 == fTailBlock->fBack);
184 fLastItem = NULL;
185 return;
186 }
cdalton72badbd2015-04-16 10:42:49 -0700187 while (!fTailBlock->fBack) {
bsalomon77d77f42014-11-21 14:38:06 -0800188 // We popped the last entry in a block that isn't the head block. Move back a block but
189 // don't free it since we'll probably grow into it shortly.
190 fTailBlock = fTailBlock->fPrev;
191 SkASSERT(fTailBlock);
192 }
193 fLastItem = reinterpret_cast<TBase*>(
194 &(*fTailBlock)[fTailBlock->fBack - lastItemLength + length_of<Header>::kValue]);
195}
196
197template<typename TBase, typename TAlign>
cdalton6819df32014-10-15 13:43:48 -0700198template<typename TItem>
199TItem* GrTRecorder<TBase, TAlign>::alloc_back(int dataLength) {
bsalomon77d77f42014-11-21 14:38:06 -0800200 // Find the header of the previous entry and get its length. We need to store that in the new
201 // header for backwards iteration (pop_back()).
202 int prevLength = 0;
203 if (fLastItem) {
204 Header* lastHeader = reinterpret_cast<Header*>(
205 reinterpret_cast<TAlign*>(fLastItem) - length_of<Header>::kValue);
206 prevLength = lastHeader->fTotalLength;
207 }
208
cdalton6819df32014-10-15 13:43:48 -0700209 const int totalLength = length_of<Header>::kValue + length_of<TItem>::kValue + dataLength;
210
bsalomon77d77f42014-11-21 14:38:06 -0800211 // Check if there is room in the current block and if not walk to next (allocating if
212 // necessary). Note that pop_back() and reset() can leave the recorder in a state where it
213 // has preallocated blocks hanging off the tail that are currently unused.
cdaltonc4650ee2014-11-07 12:51:18 -0800214 while (fTailBlock->fBack + totalLength > fTailBlock->fLength) {
215 if (!fTailBlock->fNext) {
bsalomon77d77f42014-11-21 14:38:06 -0800216 fTailBlock = MemBlock::Alloc(SkTMax(2 * fTailBlock->fLength, totalLength), fTailBlock);
217 } else {
218 fTailBlock = fTailBlock->fNext;
cdaltonc4650ee2014-11-07 12:51:18 -0800219 }
cdaltonc4650ee2014-11-07 12:51:18 -0800220 SkASSERT(0 == fTailBlock->fBack);
cdalton6819df32014-10-15 13:43:48 -0700221 }
222
223 Header* header = reinterpret_cast<Header*>(&(*fTailBlock)[fTailBlock->fBack]);
224 TItem* rawPtr = reinterpret_cast<TItem*>(
225 &(*fTailBlock)[fTailBlock->fBack + length_of<Header>::kValue]);
226
227 header->fTotalLength = totalLength;
bsalomon77d77f42014-11-21 14:38:06 -0800228 header->fPrevLength = prevLength;
cdalton6819df32014-10-15 13:43:48 -0700229 fLastItem = rawPtr;
230 fTailBlock->fBack += totalLength;
231
232 // FIXME: We currently require that the base and subclass share the same start address.
233 // This is not required by the C++ spec, and is likely to not be true in the case of
234 // multiple inheritance or a base class that doesn't have virtual methods (when the
235 // subclass does). It would be ideal to find a more robust solution that comes at no
236 // extra cost to performance or code generality.
237 SkDEBUGCODE(void* baseAddr = fLastItem;
238 void* subclassAddr = rawPtr);
239 SkASSERT(baseAddr == subclassAddr);
240
241 return rawPtr;
242}
243
cdalton72badbd2015-04-16 10:42:49 -0700244/**
245 * Iterates through a recorder from front to back. The initial state of the iterator is
246 * to not have the front item loaded yet; next() must be called first. Usage model:
247 *
248 * GrTRecorder<TBase, TAlign>::Iter iter(recorder);
249 * while (iter.next()) {
250 * iter->doSomething();
251 * }
252 */
cdalton6819df32014-10-15 13:43:48 -0700253template<typename TBase, typename TAlign>
254class GrTRecorder<TBase, TAlign>::Iter {
255public:
256 Iter(GrTRecorder& recorder) : fBlock(recorder.fHeadBlock), fPosition(0), fItem(NULL) {}
257
258 bool next() {
cdaltonc4650ee2014-11-07 12:51:18 -0800259 while (fPosition >= fBlock->fBack) {
cdalton6819df32014-10-15 13:43:48 -0700260 SkASSERT(fPosition == fBlock->fBack);
261 if (!fBlock->fNext) {
262 return false;
263 }
cdalton6819df32014-10-15 13:43:48 -0700264 fBlock = fBlock->fNext;
265 fPosition = 0;
266 }
267
268 Header* header = reinterpret_cast<Header*>(&(*fBlock)[fPosition]);
269 fItem = reinterpret_cast<TBase*>(&(*fBlock)[fPosition + length_of<Header>::kValue]);
270 fPosition += header->fTotalLength;
271 return true;
272 }
273
274 TBase* get() const {
275 SkASSERT(fItem);
276 return fItem;
277 }
278
279 TBase* operator->() const { return this->get(); }
280
281private:
282 MemBlock* fBlock;
283 int fPosition;
284 TBase* fItem;
285};
286
cdalton72badbd2015-04-16 10:42:49 -0700287/**
288 * Iterates through a recorder in reverse, from back to front. This version mirrors "Iter",
289 * so the initial state is to have recorder.back() loaded already. (Note that this will
290 * assert if the recorder is empty.) Usage model:
291 *
292 * GrTRecorder<TBase, TAlign>::ReverseIter reverseIter(recorder);
293 * do {
294 * reverseIter->doSomething();
295 * } while (reverseIter.previous());
296 */
297template<typename TBase, typename TAlign>
298class GrTRecorder<TBase, TAlign>::ReverseIter {
299public:
300 ReverseIter(GrTRecorder& recorder)
301 : fBlock(recorder.fTailBlock),
302 fItem(&recorder.back()) {
303 Header* lastHeader = reinterpret_cast<Header*>(
304 reinterpret_cast<TAlign*>(fItem) - length_of<Header>::kValue);
305 fPosition = fBlock->fBack - lastHeader->fTotalLength;
306 }
307
308 bool previous() {
309 Header* header = reinterpret_cast<Header*>(&(*fBlock)[fPosition]);
310
311 while (0 == fPosition) {
312 if (!fBlock->fPrev) {
313 // We've reached the front of the recorder.
314 return false;
315 }
316 fBlock = fBlock->fPrev;
317 fPosition = fBlock->fBack;
318 }
319
320 fPosition -= header->fPrevLength;
321 SkASSERT(fPosition >= 0);
322
323 fItem = reinterpret_cast<TBase*>(&(*fBlock)[fPosition + length_of<Header>::kValue]);
324 return true;
325 }
326
327 TBase* get() const { return fItem; }
328 TBase* operator->() const { return this->get(); }
329
330private:
331 MemBlock* fBlock;
332 int fPosition;
333 TBase* fItem;
334};
335
cdalton6819df32014-10-15 13:43:48 -0700336template<typename TBase, typename TAlign>
337void GrTRecorder<TBase, TAlign>::reset() {
338 Iter iter(*this);
339 while (iter.next()) {
340 iter->~TBase();
341 }
cdaltonc4650ee2014-11-07 12:51:18 -0800342
343 // Assume the next time this recorder fills up it will use approximately the same
344 // amount of space as last time. Leave enough space for up to ~50% growth; free
345 // everything else.
346 if (fTailBlock->fBack <= fTailBlock->fLength / 2) {
347 MemBlock::Free(fTailBlock->fNext);
cdaltonc4650ee2014-11-07 12:51:18 -0800348 } else if (fTailBlock->fNext) {
349 MemBlock::Free(fTailBlock->fNext->fNext);
350 fTailBlock->fNext->fNext = NULL;
351 }
352
353 for (MemBlock* block = fHeadBlock; block; block = block->fNext) {
354 block->fBack = 0;
355 }
356
cdalton6819df32014-10-15 13:43:48 -0700357 fTailBlock = fHeadBlock;
358 fLastItem = NULL;
359}
360
361////////////////////////////////////////////////////////////////////////////////
362
363template<typename TItem> struct GrTRecorderAllocWrapper {
364 GrTRecorderAllocWrapper() : fDataLength(0) {}
365
366 template <typename TBase, typename TAlign>
367 GrTRecorderAllocWrapper(const GrTRecorder<TBase, TAlign>&, int sizeOfData)
368 : fDataLength(GrTRecorder<TBase, TAlign>::LengthOf(sizeOfData)) {}
369
370 const int fDataLength;
371};
372
373template <typename TBase, typename TAlign, typename TItem>
374void* operator new(size_t size, GrTRecorder<TBase, TAlign>& recorder,
375 const GrTRecorderAllocWrapper<TItem>& wrapper) {
376 SkASSERT(size == sizeof(TItem));
377 return recorder.template alloc_back<TItem>(wrapper.fDataLength);
378}
379
380template <typename TBase, typename TAlign, typename TItem>
381void operator delete(void*, GrTRecorder<TBase, TAlign>&, const GrTRecorderAllocWrapper<TItem>&) {
382 // We only provide an operator delete to work around compiler warnings that can come
383 // up for an unmatched operator new when compiling with exceptions.
384 SK_CRASH();
385}
386
387#define GrNEW_APPEND_TO_RECORDER(recorder, type_name, args) \
388 (new (recorder, GrTRecorderAllocWrapper<type_name>()) type_name args)
389
390#define GrNEW_APPEND_WITH_DATA_TO_RECORDER(recorder, type_name, args, size_of_data) \
391 (new (recorder, GrTRecorderAllocWrapper<type_name>(recorder, size_of_data)) type_name args)
392
393#endif