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 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 memcpy is safe | |
| 469 void realloc(size_t count) { | |
|
mtklein
2015/04/07 19:59:44
Don't we still need a count > N check for safety?
| |
| 470 if (fPtr == fTStorage) { | |
| 471 fPtr = (T*)sk_malloc_throw(count * sizeof(T)); | |
| 472 memcpy(fPtr, fTStorage, N); | |
| 473 } else { | |
| 474 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); | |
| 475 } | |
| 476 } | |
| 477 | |
| 467 private: | 478 private: |
| 468 T* fPtr; | 479 T* fPtr; |
| 469 union { | 480 union { |
| 470 uint32_t fStorage32[(N*sizeof(T) + 3) >> 2]; | 481 uint32_t fStorage32[(N*sizeof(T) + 3) >> 2]; |
| 471 T fTStorage[1]; // do NOT want to invoke T::T() | 482 T fTStorage[1]; // do NOT want to invoke T::T() |
| 472 }; | 483 }; |
| 473 }; | 484 }; |
| 474 | 485 |
| 475 /** | 486 /** |
| 476 * Reserves memory that is aligned on double and pointer boundaries. | 487 * Reserves memory that is aligned on double and pointer boundaries. |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 499 /** | 510 /** |
| 500 * Returns void* because this object does not initialize the | 511 * Returns void* because this object does not initialize the |
| 501 * memory. Use placement new for types that require a cons. | 512 * memory. Use placement new for types that require a cons. |
| 502 */ | 513 */ |
| 503 void* get() { return fStorage.get(); } | 514 void* get() { return fStorage.get(); } |
| 504 private: | 515 private: |
| 505 SkAlignedSStorage<sizeof(T)*N> fStorage; | 516 SkAlignedSStorage<sizeof(T)*N> fStorage; |
| 506 }; | 517 }; |
| 507 | 518 |
| 508 #endif | 519 #endif |
| OLD | NEW |