Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: include/core/SkTemplates.h

Issue 1069013002: add realloc method to SkAutoSTMalloc (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: feedback inc Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698