Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkTemplates_DEFINED | 10 #ifndef SkTemplates_DEFINED |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 | 221 |
| 222 private: | 222 private: |
| 223 T* fArray; | 223 T* fArray; |
| 224 SkDEBUGCODE(int fCount;) | 224 SkDEBUGCODE(int fCount;) |
| 225 }; | 225 }; |
| 226 | 226 |
| 227 /** Wraps SkAutoTArray, with room for up to N elements preallocated | 227 /** Wraps SkAutoTArray, with room for up to N elements preallocated |
| 228 */ | 228 */ |
| 229 template <size_t N, typename T> class SkAutoSTArray : SkNoncopyable { | 229 template <size_t N, typename T> class SkAutoSTArray : SkNoncopyable { |
| 230 public: | 230 public: |
| 231 /** Initialize with no objects */ | |
| 232 SkAutoSTArray() { | |
| 233 fArray = NULL; | |
| 234 fCount = 0; | |
| 235 } | |
| 236 | |
| 231 /** Allocate count number of T elements | 237 /** Allocate count number of T elements |
| 232 */ | 238 */ |
| 233 SkAutoSTArray(size_t count) { | 239 SkAutoSTArray(size_t count) { |
| 240 fArray = NULL; | |
| 241 fCount = 0; | |
| 242 this->reset(count); | |
| 243 } | |
| 244 | |
| 245 ~SkAutoSTArray() { | |
| 246 this->reset(0); | |
| 247 } | |
| 248 | |
| 249 /** Destroys previous objects in the array and default constructs count numb er of objects */ | |
| 250 void reset(size_t count) { | |
| 251 T* start = fArray; | |
| 252 T* iter = start + fCount; | |
| 253 while (iter > start) { | |
| 254 (--iter)->~T(); | |
| 255 } | |
| 256 | |
|
robertphillips
2013/06/13 14:52:42
could recycle the memory if "fCount > count && cou
bsalomon
2013/06/13 15:03:29
Done, but only for exact fit. Not sure how much ex
| |
| 257 if (fCount > N) { | |
| 258 sk_free(fArray); | |
| 259 } | |
| 260 | |
| 234 fCount = count; | 261 fCount = count; |
| 235 if (count > N) { | 262 if (count > N) { |
| 236 fArray = (T*) sk_malloc_throw(count * sizeof(T)); | 263 fArray = (T*) sk_malloc_throw(count * sizeof(T)); |
| 237 } else if (count > 0) { | 264 } else if (count > 0) { |
| 238 fArray = (T*) fStorage; | 265 fArray = (T*) fStorage; |
| 239 } else { | 266 } else { |
| 240 fArray = NULL; | 267 fArray = NULL; |
| 241 return; | 268 return; |
| 242 } | 269 } |
| 243 T* iter = fArray; | 270 iter = fArray; |
| 244 T* stop = fArray + count; | 271 T* stop = fArray + count; |
| 245 while (iter < stop) { | 272 while (iter < stop) { |
| 246 SkNEW_PLACEMENT(iter++, T); | 273 SkNEW_PLACEMENT(iter++, T); |
| 247 } | 274 } |
| 248 } | 275 } |
| 249 | 276 |
| 250 ~SkAutoSTArray() { | |
| 251 T* start = fArray; | |
| 252 T* iter = start + fCount; | |
| 253 while (iter > start) { | |
| 254 (--iter)->~T(); | |
| 255 } | |
| 256 if (fCount > N) { | |
| 257 sk_free(fArray); | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 /** Return the number of T elements in the array | 277 /** Return the number of T elements in the array |
| 262 */ | 278 */ |
| 263 size_t count() const { return fCount; } | 279 size_t count() const { return fCount; } |
| 264 | 280 |
| 265 /** Return the array of T elements. Will be NULL if count == 0 | 281 /** Return the array of T elements. Will be NULL if count == 0 |
| 266 */ | 282 */ |
| 267 T* get() const { return fArray; } | 283 T* get() const { return fArray; } |
| 268 | 284 |
| 269 /** Return the nth element in the array | 285 /** Return the nth element in the array |
| 270 */ | 286 */ |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 432 /** | 448 /** |
| 433 * Returns void* because this object does not initialize the | 449 * Returns void* because this object does not initialize the |
| 434 * memory. Use placement new for types that require a cons. | 450 * memory. Use placement new for types that require a cons. |
| 435 */ | 451 */ |
| 436 void* get() { return fStorage.get(); } | 452 void* get() { return fStorage.get(); } |
| 437 private: | 453 private: |
| 438 SkAlignedSStorage<sizeof(T)*N> fStorage; | 454 SkAlignedSStorage<sizeof(T)*N> fStorage; |
| 439 }; | 455 }; |
| 440 | 456 |
| 441 #endif | 457 #endif |
| OLD | NEW |