aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2014-02-11 15:56:57 +0000
committerGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2014-02-11 15:56:57 +0000
commit09f0ba7eb3c1933c7be878a043de5c287edd00ab (patch)
treee185464e99d70a1fa9b05533f9362fd28fc4ce5e
parentd321a45aea4b1140d76a21d97b54deb13ca261e5 (diff)
Revert of Change growth function for SkWriter32 (https://codereview.chromium.org/150663014/)
Reason for revert: See https://codereview.chromium.org/152703007/. Original issue's description: > Change growth function for SkWriter32 > > Add setCountExact to SkTDArray to allow external control of array growth. > Use it to allow SkWriter to control the buffer growth pattern. > Change buffer growth pattern to 1.5n+4096 > > BUG=skia:2125 > > Committed: http://code.google.com/p/skia/source/detail?r=13401 R=tomhudson@google.com, reed@google.com, iancottrell@google.com TBR=iancottrell@google.com, reed@google.com, tomhudson@google.com NOTREECHECKS=true NOTRY=true BUG=skia:2125 Author: mtklein@google.com Review URL: https://codereview.chromium.org/132313013 git-svn-id: http://skia.googlecode.com/svn/trunk@13406 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r--include/core/SkTDArray.h83
-rw-r--r--src/core/SkWriter32.cpp12
2 files changed, 27 insertions, 68 deletions
diff --git a/include/core/SkTDArray.h b/include/core/SkTDArray.h
index e51f2cd008..67254ccc9a 100644
--- a/include/core/SkTDArray.h
+++ b/include/core/SkTDArray.h
@@ -151,43 +151,25 @@ 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) {
- // TODO(mtklein): eliminate this method, setCountExact -> setCount
- SkASSERT(count >= 0);
if (count > fReserve) {
- resizeStorageToAtLeast(count);
- }
- 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);
+ this->growBy(count - fCount);
+ } else {
+ fCount = count;
}
- fCount = count;
}
void setReserve(int reserve) {
if (reserve > fReserve) {
- resizeStorageToAtLeast(reserve);
+ SkASSERT(reserve > fCount);
+ int count = fCount;
+ this->growBy(reserve - fCount);
+ fCount = count;
}
}
T* prepend() {
- this->adjustCount(1);
+ this->growBy(1);
memmove(fArray + 1, fArray, (fCount - 1) * sizeof(T));
return fArray;
}
@@ -201,7 +183,7 @@ public:
SkASSERT(src == NULL || fArray == NULL ||
src + count <= fArray || fArray + oldCount <= src);
- this->adjustCount(count);
+ this->growBy(count);
if (src) {
memcpy(fArray + oldCount, src, sizeof(T) * count);
}
@@ -222,7 +204,7 @@ public:
SkASSERT(count);
SkASSERT(index <= fCount);
size_t oldCount = fCount;
- this->adjustCount(count);
+ this->growBy(count);
T* dst = fArray + index;
memmove(dst + count, dst, sizeof(T) * (oldCount - index));
if (src) {
@@ -374,43 +356,20 @@ private:
int fReserve;
int fCount;
- /**
- * Adjusts the number of elements in the array.
- * This is the same as calling setCount(count() + delta).
- */
- void adjustCount(int delta) {
- setCount(fCount + delta);
- }
+ void growBy(int extra) {
+ SkASSERT(extra);
- /**
- * 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));
+ if (fCount + extra > fReserve) {
+ int size = fCount + extra + 4;
+ size += size >> 2;
+
+ fArray = (T*)sk_realloc_throw(fArray, size * sizeof(T));
#ifdef SK_DEBUG
- fData = (ArrayT*)fArray;
+ 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.
- *
- * note: does NOT modify fCount
- */
- void resizeStorageToAtLeast(int count) {
- SkASSERT(count > fReserve);
- int space = count + 4;
- space += space>>2;
- resizeStorageToExact(space);
+ fReserve = size;
+ }
+ fCount += extra;
}
};
diff --git a/src/core/SkWriter32.cpp b/src/core/SkWriter32.cpp
index 76664e72b3..1e8a10c87c 100644
--- a/src/core/SkWriter32.cpp
+++ b/src/core/SkWriter32.cpp
@@ -67,20 +67,20 @@ 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) {
bool wasExternal = (fExternal != NULL) && (fData == fExternal);
- fCapacity = kMinBufferBytes +
- SkTMax(size, fCapacity + (fCapacity >> 1));
-
// cause the buffer to grow
- fInternal.setCountExact(fCapacity);
+ fInternal.setCount(size);
fData = fInternal.begin();
if (wasExternal) {
// we were external, so copy in the data
memcpy(fData, fExternal, fUsed);
}
+ // Find out the size the buffer grew to, it may be more than we asked for.
+ fCapacity = fInternal.reserved();
+ // Expand the array so all reserved space is "used", we maintain the
+ // amount we have written manually outside the array
+ fInternal.setCount(fCapacity);
SkASSERT(fInternal.count() == (int)fCapacity);
SkASSERT(fInternal.reserved() == (int)fCapacity);
}