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

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 | « gyp/tests.gypi ('k') | tests/TemplatesTest.cpp » ('j') | 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 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 fPtr = NULL; 404 fPtr = NULL;
405 return ptr; 405 return ptr;
406 } 406 }
407 407
408 private: 408 private:
409 T* fPtr; 409 T* fPtr;
410 }; 410 };
411 411
412 template <size_t N, typename T> class SkAutoSTMalloc : SkNoncopyable { 412 template <size_t N, typename T> class SkAutoSTMalloc : SkNoncopyable {
413 public: 413 public:
414 SkAutoSTMalloc() { 414 SkAutoSTMalloc() : fPtr(fTStorage) {}
415 fPtr = NULL;
416 }
417 415
418 SkAutoSTMalloc(size_t count) { 416 SkAutoSTMalloc(size_t count) {
419 if (count > N) { 417 if (count > N) {
420 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_M ALLOC_TEMP); 418 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_M ALLOC_TEMP);
421 } else if (count) { 419 } else {
422 fPtr = fTStorage; 420 fPtr = fTStorage;
423 } else {
424 fPtr = NULL;
425 } 421 }
426 } 422 }
427 423
428 ~SkAutoSTMalloc() { 424 ~SkAutoSTMalloc() {
429 if (fPtr != fTStorage) { 425 if (fPtr != fTStorage) {
430 sk_free(fPtr); 426 sk_free(fPtr);
431 } 427 }
432 } 428 }
433 429
434 // doesn't preserve contents 430 // doesn't preserve contents
435 T* reset(size_t count) { 431 T* reset(size_t count) {
436 if (fPtr != fTStorage) { 432 if (fPtr != fTStorage) {
437 sk_free(fPtr); 433 sk_free(fPtr);
438 } 434 }
439 if (count > N) { 435 if (count > N) {
440 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_M ALLOC_TEMP); 436 fPtr = (T*)sk_malloc_throw(count * sizeof(T));
441 } else if (count) { 437 } else {
442 fPtr = fTStorage; 438 fPtr = fTStorage;
443 } else {
444 fPtr = NULL;
445 } 439 }
446 return fPtr; 440 return fPtr;
447 } 441 }
448 442
449 T* get() const { return fPtr; } 443 T* get() const { return fPtr; }
450 444
451 operator T*() { 445 operator T*() {
452 return fPtr; 446 return fPtr;
453 } 447 }
454 448
455 operator const T*() const { 449 operator const T*() const {
456 return fPtr; 450 return fPtr;
457 } 451 }
458 452
459 T& operator[](int index) { 453 T& operator[](int index) {
460 return fPtr[index]; 454 return fPtr[index];
461 } 455 }
462 456
463 const T& operator[](int index) const { 457 const T& operator[](int index) const {
464 return fPtr[index]; 458 return fPtr[index];
465 } 459 }
466 460
461 // Reallocs the array, can be used to shrink the allocation. Makes no attem pt to be intelligent
462 void realloc(size_t count) {
463 if (count > N) {
464 if (fPtr == fTStorage) {
465 fPtr = (T*)sk_malloc_throw(count * sizeof(T));
466 memcpy(fPtr, fTStorage, N * sizeof(T));
467 } else {
468 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T));
469 }
470 } else if (fPtr != fTStorage) {
471 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T));
472 }
473 }
474
467 private: 475 private:
468 T* fPtr; 476 T* fPtr;
469 union { 477 union {
470 uint32_t fStorage32[(N*sizeof(T) + 3) >> 2]; 478 uint32_t fStorage32[(N*sizeof(T) + 3) >> 2];
471 T fTStorage[1]; // do NOT want to invoke T::T() 479 T fTStorage[1]; // do NOT want to invoke T::T()
472 }; 480 };
473 }; 481 };
474 482
475 /** 483 /**
476 * Reserves memory that is aligned on double and pointer boundaries. 484 * Reserves memory that is aligned on double and pointer boundaries.
(...skipping 22 matching lines...) Expand all
499 /** 507 /**
500 * Returns void* because this object does not initialize the 508 * Returns void* because this object does not initialize the
501 * memory. Use placement new for types that require a cons. 509 * memory. Use placement new for types that require a cons.
502 */ 510 */
503 void* get() { return fStorage.get(); } 511 void* get() { return fStorage.get(); }
504 private: 512 private:
505 SkAlignedSStorage<sizeof(T)*N> fStorage; 513 SkAlignedSStorage<sizeof(T)*N> fStorage;
506 }; 514 };
507 515
508 #endif 516 #endif
OLDNEW
« no previous file with comments | « gyp/tests.gypi ('k') | tests/TemplatesTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698