SkWriter32: throw in the SkTDArray towel.
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 13 Feb 2014 18:35:54 +0000 (18:35 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 13 Feb 2014 18:35:54 +0000 (18:35 +0000)
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

include/core/SkTDArray.h
include/core/SkWriter32.h
src/core/SkWriter32.cpp

index 804b84f..ecbfbd9 100644 (file)
@@ -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
     }
 };
 
index 586fe9f..2e7c956 100644 (file)
@@ -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.
 };
 
 /**
index 46150ee..fe33e4a 100644 (file)
@@ -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);