diff options
author | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-02-13 18:35:54 +0000 |
---|---|---|
committer | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-02-13 18:35:54 +0000 |
commit | ca21a00c736d05686c84ab874ce6a49008da6a76 (patch) | |
tree | 5c116161612fdf72d000988b738dcdf1935f78e8 | |
parent | 989ac549c16d583297cd1d9ab814bfa59573b950 (diff) |
SkWriter32: throw in the SkTDArray towel.
I think it's looking more clear we don't have a clean way to use SkTDArray in
SkWriter32. We can't give SkWriter32 meaningful control over SkTDArray's
reallocation without making moot SkTDArray's raison d'etre.
Let's just use an SkAutoTMalloc<uint8_t> instead. It wants SkWriter32 to
control it. Also, it's lower overhead: SkAutoTMalloc<uint8_t> is just a smart
poiter: no size or capacity stored.
BUG=skia:
R=reed@google.com, iancottrell@google.com, reed@chromium.org, mtklein@google.com
Author: mtklein@chromium.org
Review URL: https://codereview.chromium.org/163983002
git-svn-id: http://skia.googlecode.com/svn/trunk@13436 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | include/core/SkTDArray.h | 40 | ||||
-rw-r--r-- | include/core/SkWriter32.h | 19 | ||||
-rw-r--r-- | src/core/SkWriter32.cpp | 12 |
3 files changed, 17 insertions, 54 deletions
diff --git a/include/core/SkTDArray.h b/include/core/SkTDArray.h index 804b84f669..ecbfbd90e9 100644 --- a/include/core/SkTDArray.h +++ b/include/core/SkTDArray.h @@ -158,7 +158,6 @@ public: * It will never shrink the shrink the storage. */ void setCount(int count) { - // TODO(mtklein): eliminate this method, setCountExact -> setCount SkASSERT(count >= 0); if (count > fReserve) { this->resizeStorageToAtLeast(count); @@ -166,20 +165,6 @@ public: fCount = count; } - /** - * 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) { this->resizeStorageToAtLeast(reserve); @@ -383,22 +368,6 @@ private: } /** - * 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; -#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 @@ -408,9 +377,12 @@ private: */ void resizeStorageToAtLeast(int count) { SkASSERT(count > fReserve); - int space = count + 4; - space += space>>2; - this->resizeStorageToExact(space); + fReserve = count + 4; + fReserve += fReserve / 4; + fArray = (T*)sk_realloc_throw(fArray, fReserve * sizeof(T)); +#ifdef SK_DEBUG + fData = (ArrayT*)fArray; +#endif } }; diff --git a/include/core/SkWriter32.h b/include/core/SkWriter32.h index 586fe9fb08..2e7c9563d9 100644 --- a/include/core/SkWriter32.h +++ b/include/core/SkWriter32.h @@ -18,7 +18,7 @@ #include "SkRegion.h" #include "SkScalar.h" #include "SkStream.h" -#include "SkTDArray.h" +#include "SkTemplates.h" #include "SkTypes.h" class SkWriter32 : SkNoncopyable { @@ -30,12 +30,7 @@ public: * first time an allocation doesn't fit. From then it will use dynamically allocated storage. * This used to be optional behavior, but pipe now relies on it. */ - SkWriter32(void* external = NULL, size_t externalBytes = 0) - : fData(0) - , fCapacity(0) - , fUsed(0) - , fExternal(0) - { + SkWriter32(void* external = NULL, size_t externalBytes = 0) { this->reset(external, externalBytes); } @@ -238,11 +233,11 @@ public: private: void growToAtLeast(size_t size); - uint8_t* fData; // Points to either fInternal or fExternal. - size_t fCapacity; // Number of bytes we can write to fData. - size_t fUsed; // Number of bytes written. - void* fExternal; // Unmanaged memory block. - SkTDArray<uint8_t> fInternal; // Managed memory block. + uint8_t* fData; // Points to either fInternal or fExternal. + size_t fCapacity; // Number of bytes we can write to fData. + size_t fUsed; // Number of bytes written. + void* fExternal; // Unmanaged memory block. + SkAutoTMalloc<uint8_t> fInternal; // Managed memory block. }; /** diff --git a/src/core/SkWriter32.cpp b/src/core/SkWriter32.cpp index 46150ee752..fe33e4a39c 100644 --- a/src/core/SkWriter32.cpp +++ b/src/core/SkWriter32.cpp @@ -63,17 +63,13 @@ size_t SkWriter32::WriteStringSize(const char* str, size_t len) { return SkAlign4(lenBytes + len + 1); } -const size_t kMinBufferBytes = 4096; - void SkWriter32::growToAtLeast(size_t size) { const bool wasExternal = (fExternal != NULL) && (fData == fExternal); - const size_t minCapacity = kMinBufferBytes + - SkTMax(size, fCapacity + (fCapacity >> 1)); - // cause the buffer to grow - fInternal.setCountExact(minCapacity); - fData = fInternal.begin(); - fCapacity = fInternal.reserved(); + fCapacity = 4096 + SkTMax(size, fCapacity + (fCapacity / 2)); + fInternal.realloc(fCapacity); + fData = fInternal.get(); + if (wasExternal) { // we were external, so copy in the data memcpy(fData, fExternal, fUsed); |