Chromium Code Reviews| Index: include/core/SkTDArray.h |
| diff --git a/include/core/SkTDArray.h b/include/core/SkTDArray.h |
| index 67254ccc9a88fa93fe6489ea82bccb6efe8959e6..a77dfa187ebd9d81406df575e56a5bd2a20a74b7 100644 |
| --- a/include/core/SkTDArray.h |
| +++ b/include/core/SkTDArray.h |
| @@ -151,6 +151,12 @@ public: |
| fCount = 0; |
| } |
| + /** |
| + * Sets the number of elements in the array. |
| + * If the array does not have space for count elements, it will increase |
| + * the storage allocated to some amount greater than that required. |
| + * It will never shrink the shrink the storage. |
| + */ |
| void setCount(int count) { |
| if (count > fReserve) { |
| this->growBy(count - fCount); |
| @@ -159,6 +165,20 @@ public: |
| } |
| } |
| + /** |
| + * Sets the number of elements in the array. |
| + * If the array does not have space for count elements, it will increase |
| + * the storage allocated to exactly the amount required, with no remaining |
| + * reserved space. |
| + * It will never shrink the shrink the storage. |
|
reed1
2014/02/07 17:05:15
... * this call allows the caller explicit control
|
| + */ |
| + void setCountExact(int count) { |
| + if (count > fReserve) { |
| + this->growTo(count); |
| + } |
| + fCount = count; |
| + } |
| + |
| void setReserve(int reserve) { |
| if (reserve > fReserve) { |
| SkASSERT(reserve > fCount); |
| @@ -356,18 +376,31 @@ private: |
| int fReserve; |
| int fCount; |
| + /** |
| + * This resizes the storage to *exactly* count elements, growing or |
| + * shrinking the allocation as needed. It does not ASSERT anything about |
| + * the previous allocation size, or about fCount. |
| + */ |
| + void growTo(int count) { |
|
reed1
2014/02/07 17:05:15
rename to: resizeStorageTo() so we know what's goi
|
| + fArray = (T*)sk_realloc_throw(fArray, count * sizeof(T)); |
| +#ifdef SK_DEBUG |
| + fData = (ArrayT*)fArray; |
| +#endif |
| + fReserve = count; |
| + } |
| + |
| + /** |
| + * Increase the storage allocation such that it can hold (fCount + extra) |
| + * elements. |
| + * It never shrinks the allocation, and it may increase the allocation by |
| + * more than is strictly required, based on a private growth heuristic. |
| + */ |
| void growBy(int extra) { |
| SkASSERT(extra); |
| if (fCount + extra > fReserve) { |
|
reed1
2014/02/07 17:05:15
Slightly unrelated to this CL, but I feel like we
|
| - int size = fCount + extra + 4; |
| - size += size >> 2; |
| - |
| - fArray = (T*)sk_realloc_throw(fArray, size * sizeof(T)); |
| -#ifdef SK_DEBUG |
| - fData = (ArrayT*)fArray; |
| -#endif |
| - fReserve = size; |
| + int count = fCount + extra + 4; |
| + growTo(count + (count >> 2)); |
| } |
| fCount += extra; |
| } |