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