Chromium Code Reviews| Index: include/core/SkTDArray.h |
| diff --git a/include/core/SkTDArray.h b/include/core/SkTDArray.h |
| index 67254ccc9a88fa93fe6489ea82bccb6efe8959e6..67fb61054a31f5b9b9f1f9f2fe9d426e630da6b6 100644 |
| --- a/include/core/SkTDArray.h |
| +++ b/include/core/SkTDArray.h |
| @@ -151,25 +151,50 @@ 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) { |
|
mtklein
2014/02/11 12:52:55
I suspect that we'll ultimately find we can get aw
iancottrell
2014/02/11 13:25:04
Done.
|
| + SkASSERT(count >= 0); |
| if (count > fReserve) { |
| - this->growBy(count - fCount); |
| - } else { |
| - fCount = count; |
| + resizeStorageToAtLeast(count); |
| } |
| + fCount = count; |
| + } |
| + |
| + /** |
| + * Adjusts the number of elements in the array. |
| + * This is the same as calling setCount(count() + delta). |
| + */ |
| + void adjustCount(int delta) { |
|
mtklein
2014/02/11 12:52:55
Can you move this down to private?
iancottrell
2014/02/11 13:25:04
Done.
|
| + setCount(fCount + delta); |
| + } |
| + |
| + /** |
| + * 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. |
| + */ |
| + void setCountExact(int count) { |
| + if (count > fReserve) { |
| + this->resizeStorageToExact(count); |
| + } |
| + fCount = count; |
| } |
| void setReserve(int reserve) { |
| if (reserve > fReserve) { |
| - SkASSERT(reserve > fCount); |
| - int count = fCount; |
| - this->growBy(reserve - fCount); |
| - fCount = count; |
| + resizeStorageToAtLeast(reserve); |
| } |
| } |
| T* prepend() { |
| - this->growBy(1); |
| + this->adjustCount(1); |
| memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T)); |
| return fArray; |
| } |
| @@ -183,7 +208,7 @@ public: |
| SkASSERT(src == NULL || fArray == NULL || |
| src + count <= fArray || fArray + oldCount <= src); |
| - this->growBy(count); |
| + this->adjustCount(count); |
| if (src) { |
| memcpy(fArray + oldCount, src, sizeof(T) * count); |
| } |
| @@ -204,7 +229,7 @@ public: |
| SkASSERT(count); |
| SkASSERT(index <= fCount); |
| size_t oldCount = fCount; |
| - this->growBy(count); |
| + this->adjustCount(count); |
| T* dst = fArray + index; |
| memmove(dst + count, dst, sizeof(T) * (oldCount - index)); |
| if (src) { |
| @@ -356,20 +381,35 @@ private: |
| int fReserve; |
| int fCount; |
| - void growBy(int extra) { |
| - SkASSERT(extra); |
| - |
| - if (fCount + extra > fReserve) { |
| - int size = fCount + extra + 4; |
| - size += size >> 2; |
| - |
| - fArray = (T*)sk_realloc_throw(fArray, size * sizeof(T)); |
| + /** |
| + * 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. |
| + * |
| + * note: does NOT modify fCount |
| + */ |
| + void resizeStorageToExact(int count) { |
| + SkASSERT(count >= 0); |
| + fArray = (T*)sk_realloc_throw(fArray, count * sizeof(T)); |
| #ifdef SK_DEBUG |
| - fData = (ArrayT*)fArray; |
| + fData = (ArrayT*)fArray; |
| #endif |
| - fReserve = size; |
| - } |
| - fCount += extra; |
| + 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. |
| + * |
| + * note: does NOT modify fCount |
| + */ |
| + void resizeStorageToAtLeast(int count) { |
| + SkASSERT(count > fReserve); |
| + int space = count + 4; |
| + space += space>>2; |
|
mtklein
2014/02/11 12:52:55
Is this CL blocking any others? If not, I'd be in
iancottrell
2014/02/11 13:25:04
It is blocking one more cl, yes.
I have already do
|
| + resizeStorageToExact(space); |
| } |
| }; |