Rename existing nonpreserving reallocs to reset, add reset to SkAutoMalloc, use reset in GrBufferAllocPool

Review URL: http://codereview.appspot.com/4951058/


git-svn-id: http://skia.googlecode.com/svn/trunk@2215 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/src/GrAtlas.cpp b/gpu/src/GrAtlas.cpp
index 285ded4..0838895 100644
--- a/gpu/src/GrAtlas.cpp
+++ b/gpu/src/GrAtlas.cpp
@@ -95,7 +95,7 @@
     if (BORDER) {
         const int bpp = GrMaskFormatBytesPerPixel(fMaskFormat);
         const size_t dstRB = dstW * bpp;
-        uint8_t* dst = (uint8_t*)storage.realloc(dstH * dstRB);
+        uint8_t* dst = (uint8_t*)storage.reset(dstH * dstRB);
         Gr_bzero(dst, dstRB);                // zero top row
         dst += dstRB;
         for (int y = 0; y < height; y++) {
diff --git a/gpu/src/GrBufferAllocPool.cpp b/gpu/src/GrBufferAllocPool.cpp
index 5b49350..667e437 100644
--- a/gpu/src/GrBufferAllocPool.cpp
+++ b/gpu/src/GrBufferAllocPool.cpp
@@ -91,7 +91,7 @@
         fFirstPreallocBuffer = (fFirstPreallocBuffer + fPreallocBuffersInUse) %
                                fPreallocBuffers.count();
     }
-    fCpuData.alloc(fGpu->supportsBufferLocking() ? 0 : fMinBlockSize);
+    fCpuData.reset(fGpu->supportsBufferLocking() ? 0 : fMinBlockSize);
     GrAssert(0 == fPreallocBuffersInUse);
     VALIDATE();
 }
@@ -283,7 +283,7 @@
     }
 
     if (NULL == fBufferPtr) {
-        fBufferPtr = fCpuData.alloc(size);
+        fBufferPtr = fCpuData.reset(size);
     }
 
     VALIDATE(true);
diff --git a/gpu/src/GrDefaultPathRenderer.cpp b/gpu/src/GrDefaultPathRenderer.cpp
index 79d6e25..b70b863 100644
--- a/gpu/src/GrDefaultPathRenderer.cpp
+++ b/gpu/src/GrDefaultPathRenderer.cpp
@@ -193,7 +193,7 @@
 }
 
 void GrDefaultPathRenderer::pathWillClear() {
-    fSubpathVertCount.realloc(0);
+    fSubpathVertCount.reset(0);
     fTarget->resetVertexSource();
     if (fUseIndexedDraw) {
         fTarget->resetIndexSource();
@@ -278,7 +278,7 @@
         idx = idxBase;
     }
 
-    fSubpathVertCount.realloc(fSubpathCount);
+    fSubpathVertCount.reset(fSubpathCount);
 
     GrPoint pts[4];
 
diff --git a/gpu/src/GrGLTexture.cpp b/gpu/src/GrGLTexture.cpp
index be7a0f1..e302694 100644
--- a/gpu/src/GrGLTexture.cpp
+++ b/gpu/src/GrGLTexture.cpp
@@ -151,7 +151,7 @@
             if (flipY) {
                 src += (height - 1) * rowBytes;
             }
-            char* dst = (char*)tempStorage.realloc(trimSize);
+            char* dst = (char*)tempStorage.reset(trimSize);
             for (int y = 0; y < height; y++) {
                 memcpy(dst, src, trimRowBytes);
                 if (flipY) {
diff --git a/gpu/src/GrGpuGL.cpp b/gpu/src/GrGpuGL.cpp
index c3b5f97..92d836d 100644
--- a/gpu/src/GrGpuGL.cpp
+++ b/gpu/src/GrGpuGL.cpp
@@ -788,7 +788,7 @@
             if (flipY) {
                 src += (desc.fContentHeight - 1) * rowBytes;
             }
-            char* dst = (char*)tempStorage.realloc(trimSize);
+            char* dst = (char*)tempStorage.reset(trimSize);
             for (int y = 0; y < desc.fContentHeight; y++) {
                 memcpy(dst, src, trimRowBytes);
                 if (flipY) {
diff --git a/include/core/SkTemplates.h b/include/core/SkTemplates.h
index f79afff..75a8b42 100644
--- a/include/core/SkTemplates.h
+++ b/include/core/SkTemplates.h
@@ -181,7 +181,7 @@
     }
 
     // doesn't preserve contents
-    void realloc (size_t count) {
+    void reset (size_t count) {
         sk_free(fPtr);
         fPtr = fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP);
     }
@@ -225,7 +225,7 @@
     }
 
     // doesn't preserve contents
-    void realloc (size_t count) {
+    void reset(size_t count) {
         if (fPtr != fTStorage) {
             sk_free(fPtr);
         }
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h
index d299223..b38d4d0 100644
--- a/include/core/SkTypes.h
+++ b/include/core/SkTypes.h
@@ -387,20 +387,57 @@
     SkAutoFree& operator=(const SkAutoFree&);
 };
 
-class SkAutoMalloc : public SkAutoFree {
+/**
+ *  Manage an allocated block of heap memory. This object is the sole manager of
+ *  the lifetime of the block, so the caller must not call sk_free() or delete
+ *  on the block.
+ */
+class SkAutoMalloc : public SkNoncopyable {
 public:
-    explicit SkAutoMalloc(size_t size)
-        : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {}
-
-    SkAutoMalloc(size_t size, unsigned flags)
-        : SkAutoFree(sk_malloc_flags(size, flags)) {}
-    SkAutoMalloc() {}
-
-    void* alloc(size_t size,
-                unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) {
-        sk_free(set(sk_malloc_flags(size, flags)));
-        return get();
+    explicit SkAutoMalloc(size_t size = 0,
+                          unsigned flags = SK_MALLOC_THROW | SK_MALLOC_TEMP) {
+        fPtr = size ? sk_malloc_flags(size, flags) : NULL;
+        fSize = size;
     }
+
+    ~SkAutoMalloc() {
+        sk_free(fPtr);
+    }
+
+    /**
+     *  Reallocates the block to a new size. The ptr may or may not change.
+     */
+    void* reset(size_t size,
+                 unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) {
+        if (size != fSize) {
+            sk_free(fPtr);
+            fPtr = size ? sk_malloc_flags(size, flags) : NULL;
+            fSize = size;
+        }
+        return fPtr;
+    }
+
+    /**
+     *  Releases the block back to the heap
+     */
+    void free() {
+        this->reset(0);
+    }
+
+    /**
+     *  Return the allocated block.
+     */
+    void* get() { return fPtr; }
+    const void* get() const { return fPtr; }
+
+    /**
+     * Gets the size of the block in bytes
+     */
+    size_t getSize() const { return fSize; }
+
+private:
+    void*   fPtr;
+    size_t  fSize;
 };
 
 /**
@@ -413,7 +450,7 @@
 public:
     /**
      *  Creates initially empty storage. get() returns a ptr, but it is to
-     *  a zero-byte allocation. Must call realloc(size) to return an allocated
+     *  a zero-byte allocation. Must call reset(size) to return an allocated
      *  block.
      */
     SkAutoSMalloc() {
@@ -427,7 +464,7 @@
      */
     explicit SkAutoSMalloc(size_t size) {
         fPtr = fStorage;
-        this->realloc(size);
+        this->reset(size);
     }
 
     /**
@@ -454,7 +491,7 @@
      *  then the return block may be allocated locally, rather than from the
      *  heap.
      */
-    void* realloc(size_t size) {
+    void* reset(size_t size) {
         if (fPtr != (void*)fStorage) {
             sk_free(fPtr);
         }
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index cf94982..ecdcd7a 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -1393,7 +1393,7 @@
     SkAutoSTMalloc<128, GrColor> convertedColors(0);
     if (NULL != colors) {
         // need to convert byte order and from non-PM to PM
-        convertedColors.realloc(vertexCount);
+        convertedColors.reset(vertexCount);
         for (int i = 0; i < vertexCount; ++i) {
             convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
         }
diff --git a/src/ports/SkFontHost_mac_coretext.cpp b/src/ports/SkFontHost_mac_coretext.cpp
index a0c42c0..05818ce 100644
--- a/src/ports/SkFontHost_mac_coretext.cpp
+++ b/src/ports/SkFontHost_mac_coretext.cpp
@@ -547,7 +547,7 @@
     } else if (SkMask::kBW_Format == glyph.fMaskFormat) {
         rowBytes = SkAlign4(glyph.fWidth);
         size_t size = glyph.fHeight * rowBytes;
-        image = storage.realloc(size);
+        image = storage.reset(size);
         sk_bzero(image, size);
         doAA = false;
     }