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 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
457 } | 457 } |
458 | 458 |
459 T& operator[](int index) { | 459 T& operator[](int index) { |
460 return fPtr[index]; | 460 return fPtr[index]; |
461 } | 461 } |
462 | 462 |
463 const T& operator[](int index) const { | 463 const T& operator[](int index) const { |
464 return fPtr[index]; | 464 return fPtr[index]; |
465 } | 465 } |
466 | 466 |
467 // Reallocs the array, can be used to shrink the allocation. By reset above , we know fPtr's | |
468 // size is > N so the below memcpys are safe | |
469 void realloc(size_t count) { | |
470 if (count > N) { | |
471 if (fPtr == fTStorage) { | |
472 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP); | |
mtklein
2015/04/07 19:27:33
Let's use sk_malloc_throw. I don't believe SK_MAL
| |
473 memcpy(fPtr, fTStorage, N); | |
474 } else { | |
475 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); | |
476 } | |
477 } else if (fPtr != fTStorage) { | |
478 memcpy(fTStorage, fPtr, N); | |
mtklein
2015/04/07 19:27:34
Do you ever really hit this case that you shrink b
| |
479 SkDELETE(fPtr); | |
480 fPtr = fTStorage; | |
481 } | |
482 } | |
483 | |
467 private: | 484 private: |
468 T* fPtr; | 485 T* fPtr; |
469 union { | 486 union { |
470 uint32_t fStorage32[(N*sizeof(T) + 3) >> 2]; | 487 uint32_t fStorage32[(N*sizeof(T) + 3) >> 2]; |
471 T fTStorage[1]; // do NOT want to invoke T::T() | 488 T fTStorage[1]; // do NOT want to invoke T::T() |
472 }; | 489 }; |
473 }; | 490 }; |
474 | 491 |
475 /** | 492 /** |
476 * Reserves memory that is aligned on double and pointer boundaries. | 493 * Reserves memory that is aligned on double and pointer boundaries. |
(...skipping 22 matching lines...) Expand all Loading... | |
499 /** | 516 /** |
500 * Returns void* because this object does not initialize the | 517 * Returns void* because this object does not initialize the |
501 * memory. Use placement new for types that require a cons. | 518 * memory. Use placement new for types that require a cons. |
502 */ | 519 */ |
503 void* get() { return fStorage.get(); } | 520 void* get() { return fStorage.get(); } |
504 private: | 521 private: |
505 SkAlignedSStorage<sizeof(T)*N> fStorage; | 522 SkAlignedSStorage<sizeof(T)*N> fStorage; |
506 }; | 523 }; |
507 | 524 |
508 #endif | 525 #endif |
OLD | NEW |