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

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: missed a free 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 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
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
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