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 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 T* fPtr; | 319 T* fPtr; |
| 320 }; | 320 }; |
| 321 | 321 |
| 322 template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyab le { | 322 template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyab le { |
| 323 public: | 323 public: |
| 324 SkAutoSTMalloc() : fPtr(fTStorage) {} | 324 SkAutoSTMalloc() : fPtr(fTStorage) {} |
| 325 | 325 |
| 326 SkAutoSTMalloc(size_t count) { | 326 SkAutoSTMalloc(size_t count) { |
| 327 if (count > kCount) { | 327 if (count > kCount) { |
| 328 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_M ALLOC_TEMP); | 328 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_M ALLOC_TEMP); |
| 329 } else if (count > 0) { | |
| 330 fPtr = fTStorage; | |
| 329 } else { | 331 } else { |
| 330 fPtr = fTStorage; | 332 SkASSERT(0 == count); |
|
reed1
2016/06/22 11:33:30
Can this ever not be true?
csmartdalton
2016/06/23 17:07:46
The idea would be that if somebody unintentionally
reed1
2016/06/23 17:44:36
but count is size_t, which will never appear to be
csmartdalton
2016/06/23 18:07:26
Yeah, I realized it was size_t after the fact :)
| |
| 333 fPtr = nullptr; | |
| 331 } | 334 } |
| 332 } | 335 } |
| 333 | 336 |
| 334 ~SkAutoSTMalloc() { | 337 ~SkAutoSTMalloc() { |
| 335 if (fPtr != fTStorage) { | 338 if (fPtr != fTStorage) { |
| 336 sk_free(fPtr); | 339 sk_free(fPtr); |
| 337 } | 340 } |
| 338 } | 341 } |
| 339 | 342 |
| 340 // doesn't preserve contents | 343 // doesn't preserve contents |
| 341 T* reset(size_t count) { | 344 T* reset(size_t count) { |
| 342 if (fPtr != fTStorage) { | 345 if (fPtr != fTStorage) { |
| 343 sk_free(fPtr); | 346 sk_free(fPtr); |
| 344 } | 347 } |
| 345 if (count > kCount) { | 348 if (count > kCount) { |
| 346 fPtr = (T*)sk_malloc_throw(count * sizeof(T)); | 349 fPtr = (T*)sk_malloc_throw(count * sizeof(T)); |
| 350 } else if (count > 0) { | |
| 351 fPtr = fTStorage; | |
| 347 } else { | 352 } else { |
| 348 fPtr = fTStorage; | 353 SkASSERT(0 == count); |
| 354 fPtr = nullptr; | |
| 349 } | 355 } |
| 350 return fPtr; | 356 return fPtr; |
| 351 } | 357 } |
| 352 | 358 |
| 353 T* get() const { return fPtr; } | 359 T* get() const { return fPtr; } |
| 354 | 360 |
| 355 operator T*() { | 361 operator T*() { |
| 356 return fPtr; | 362 return fPtr; |
| 357 } | 363 } |
| 358 | 364 |
| 359 operator const T*() const { | 365 operator const T*() const { |
| 360 return fPtr; | 366 return fPtr; |
| 361 } | 367 } |
| 362 | 368 |
| 363 T& operator[](int index) { | 369 T& operator[](int index) { |
| 364 return fPtr[index]; | 370 return fPtr[index]; |
| 365 } | 371 } |
| 366 | 372 |
| 367 const T& operator[](int index) const { | 373 const T& operator[](int index) const { |
| 368 return fPtr[index]; | 374 return fPtr[index]; |
| 369 } | 375 } |
| 370 | 376 |
| 371 // Reallocs the array, can be used to shrink the allocation. Makes no attem pt to be intelligent | 377 // Reallocs the array, can be used to shrink the allocation. Makes no attem pt to be intelligent |
| 372 void realloc(size_t count) { | 378 void realloc(size_t count) { |
|
bsalomon
2016/06/22 13:30:29
Wouldn't this need to be updated, too?
csmartdalton
2016/06/23 18:07:26
Updated to what?
| |
| 373 if (count > kCount) { | 379 if (count > kCount) { |
| 374 if (fPtr == fTStorage) { | 380 if (fPtr == fTStorage) { |
| 375 fPtr = (T*)sk_malloc_throw(count * sizeof(T)); | 381 fPtr = (T*)sk_malloc_throw(count * sizeof(T)); |
| 376 memcpy(fPtr, fTStorage, kCount * sizeof(T)); | 382 memcpy(fPtr, fTStorage, kCount * sizeof(T)); |
| 377 } else { | 383 } else { |
| 378 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); | 384 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); |
| 379 } | 385 } |
| 380 } else if (fPtr != fTStorage) { | 386 } else if (fPtr != fTStorage) { |
| 381 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); | 387 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); |
| 382 } | 388 } |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 470 * Returns void* because this object does not initialize the | 476 * Returns void* because this object does not initialize the |
| 471 * memory. Use placement new for types that require a cons. | 477 * memory. Use placement new for types that require a cons. |
| 472 */ | 478 */ |
| 473 void* get() { return fStorage.get(); } | 479 void* get() { return fStorage.get(); } |
| 474 const void* get() const { return fStorage.get(); } | 480 const void* get() const { return fStorage.get(); } |
| 475 private: | 481 private: |
| 476 SkAlignedSStorage<sizeof(T)*N> fStorage; | 482 SkAlignedSStorage<sizeof(T)*N> fStorage; |
| 477 }; | 483 }; |
| 478 | 484 |
| 479 #endif | 485 #endif |
| OLD | NEW |