OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 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 GrAllocator_DEFINED | 8 #ifndef GrAllocator_DEFINED |
9 #define GrAllocator_DEFINED | 9 #define GrAllocator_DEFINED |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 fItemSize(itemSize), | 32 fItemSize(itemSize), |
33 fItemsPerBlock(itemsPerBlock), | 33 fItemsPerBlock(itemsPerBlock), |
34 fOwnFirstBlock(NULL == initialBlock), | 34 fOwnFirstBlock(NULL == initialBlock), |
35 fCount(0) { | 35 fCount(0) { |
36 SkASSERT(itemsPerBlock > 0); | 36 SkASSERT(itemsPerBlock > 0); |
37 fBlockSize = fItemSize * fItemsPerBlock; | 37 fBlockSize = fItemSize * fItemsPerBlock; |
38 fBlocks.push_back() = initialBlock; | 38 fBlocks.push_back() = initialBlock; |
39 SkDEBUGCODE(if (!fOwnFirstBlock) {*((char*)initialBlock+fBlockSize-1)='a
';} ); | 39 SkDEBUGCODE(if (!fOwnFirstBlock) {*((char*)initialBlock+fBlockSize-1)='a
';} ); |
40 } | 40 } |
41 | 41 |
| 42 /* |
| 43 * Set first block of memory to write into. Must be called before any other
methods. |
| 44 * This requires that you have passed NULL in the constructor. |
| 45 * |
| 46 * @param initialBlock optional memory to use for the first block. |
| 47 * Must be at least itemSize*itemsPerBlock sized. |
| 48 * Caller is responsible for freeing this memory. |
| 49 */ |
| 50 void setInitialBlock(void* initialBlock) { |
| 51 SkASSERT(0 == fCount); |
| 52 SkASSERT(1 == fBlocks.count()); |
| 53 SkASSERT(NULL == fBlocks.back()); |
| 54 fOwnFirstBlock = false; |
| 55 fBlocks.back() = initialBlock; |
| 56 } |
| 57 |
42 /** | 58 /** |
43 * Adds an item and returns pointer to it. | 59 * Adds an item and returns pointer to it. |
44 * | 60 * |
45 * @return pointer to the added item. | 61 * @return pointer to the added item. |
46 */ | 62 */ |
47 void* push_back() { | 63 void* push_back() { |
48 int indexInBlock = fCount % fItemsPerBlock; | 64 int indexInBlock = fCount % fItemsPerBlock; |
49 // we always have at least one block | 65 // we always have at least one block |
50 if (0 == indexInBlock) { | 66 if (0 == indexInBlock) { |
51 if (0 != fCount) { | 67 if (0 != fCount) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 154 |
139 template <typename T> | 155 template <typename T> |
140 class GrTAllocator : public SkNoncopyable { | 156 class GrTAllocator : public SkNoncopyable { |
141 public: | 157 public: |
142 virtual ~GrTAllocator() { this->reset(); }; | 158 virtual ~GrTAllocator() { this->reset(); }; |
143 | 159 |
144 /** | 160 /** |
145 * Create an allocator | 161 * Create an allocator |
146 * | 162 * |
147 * @param itemsPerBlock the number of items to allocate at once | 163 * @param itemsPerBlock the number of items to allocate at once |
148 * @param initialBlock optional memory to use for the first block. | |
149 * Must be at least size(T)*itemsPerBlock sized. | |
150 * Caller is responsible for freeing this memory. | |
151 */ | 164 */ |
152 explicit GrTAllocator(int itemsPerBlock) | 165 explicit GrTAllocator(int itemsPerBlock) |
153 : fAllocator(sizeof(T), itemsPerBlock, NULL) {} | 166 : fAllocator(sizeof(T), itemsPerBlock, NULL) {} |
154 | 167 |
155 /** | 168 /** |
156 * Adds an item and returns it. | 169 * Adds an item and returns it. |
157 * | 170 * |
158 * @return the added item. | 171 * @return the added item. |
159 */ | 172 */ |
160 T& push_back() { | 173 T& push_back() { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 } | 229 } |
217 | 230 |
218 /** | 231 /** |
219 * access item by index. | 232 * access item by index. |
220 */ | 233 */ |
221 const T& operator[] (int i) const { | 234 const T& operator[] (int i) const { |
222 return *(const T*)(fAllocator[i]); | 235 return *(const T*)(fAllocator[i]); |
223 } | 236 } |
224 | 237 |
225 protected: | 238 protected: |
226 GrTAllocator(int itemsPerBlock, void* initialBlock) | 239 /* |
227 : fAllocator(sizeof(T), itemsPerBlock, initialBlock) { | 240 * Set first block of memory to write into. Must be called before any other
methods. |
| 241 * |
| 242 * @param initialBlock optional memory to use for the first block. |
| 243 * Must be at least size(T)*itemsPerBlock sized. |
| 244 * Caller is responsible for freeing this memory. |
| 245 */ |
| 246 void setInitialBlock(void* initialBlock) { |
| 247 fAllocator.setInitialBlock(initialBlock); |
228 } | 248 } |
229 | 249 |
230 private: | 250 private: |
231 GrAllocator fAllocator; | 251 GrAllocator fAllocator; |
232 typedef SkNoncopyable INHERITED; | 252 typedef SkNoncopyable INHERITED; |
233 }; | 253 }; |
234 | 254 |
235 template <int N, typename T> class GrSTAllocator : public GrTAllocator<T> { | 255 template <int N, typename T> class GrSTAllocator : public GrTAllocator<T> { |
236 private: | 256 private: |
237 typedef GrTAllocator<T> INHERITED; | 257 typedef GrTAllocator<T> INHERITED; |
238 | 258 |
239 public: | 259 public: |
240 GrSTAllocator() : INHERITED(N, fStorage.get()) { | 260 GrSTAllocator() : INHERITED(N) { |
| 261 this->setInitialBlock(fStorage.get()); |
241 } | 262 } |
242 | 263 |
243 private: | 264 private: |
244 SkAlignedSTStorage<N, T> fStorage; | 265 SkAlignedSTStorage<N, T> fStorage; |
245 }; | 266 }; |
246 | 267 |
247 #endif | 268 #endif |
OLD | NEW |