From 7d4679a2e1aaa1953bc20d668135c517ee488c11 Mon Sep 17 00:00:00 2001 From: "bsalomon@google.com" Date: Fri, 2 Sep 2011 22:06:24 +0000 Subject: [PATCH] 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 --- gpu/src/GrAtlas.cpp | 2 +- gpu/src/GrBufferAllocPool.cpp | 4 +- gpu/src/GrDefaultPathRenderer.cpp | 4 +- gpu/src/GrGLTexture.cpp | 2 +- gpu/src/GrGpuGL.cpp | 2 +- include/core/SkTemplates.h | 4 +- include/core/SkTypes.h | 67 +++++++++++++++++++++------ src/gpu/SkGpuDevice.cpp | 2 +- src/ports/SkFontHost_mac_coretext.cpp | 2 +- 9 files changed, 63 insertions(+), 26 deletions(-) diff --git a/gpu/src/GrAtlas.cpp b/gpu/src/GrAtlas.cpp index 285ded460c..0838895a13 100644 --- a/gpu/src/GrAtlas.cpp +++ b/gpu/src/GrAtlas.cpp @@ -95,7 +95,7 @@ bool GrAtlas::addSubImage(int width, int height, const void* image, 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 5b4935085b..667e43789b 100644 --- a/gpu/src/GrBufferAllocPool.cpp +++ b/gpu/src/GrBufferAllocPool.cpp @@ -91,7 +91,7 @@ void GrBufferAllocPool::reset() { 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 @@ bool GrBufferAllocPool::createBlock(size_t requestSize) { } 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 79d6e25570..b70b8631d5 100644 --- a/gpu/src/GrDefaultPathRenderer.cpp +++ b/gpu/src/GrDefaultPathRenderer.cpp @@ -193,7 +193,7 @@ bool GrDefaultPathRenderer::requiresStencilPass(const GrDrawTarget* target, } void GrDefaultPathRenderer::pathWillClear() { - fSubpathVertCount.realloc(0); + fSubpathVertCount.reset(0); fTarget->resetVertexSource(); if (fUseIndexedDraw) { fTarget->resetIndexSource(); @@ -278,7 +278,7 @@ bool GrDefaultPathRenderer::createGeom(GrScalar srcSpaceTol, idx = idxBase; } - fSubpathVertCount.realloc(fSubpathCount); + fSubpathVertCount.reset(fSubpathCount); GrPoint pts[4]; diff --git a/gpu/src/GrGLTexture.cpp b/gpu/src/GrGLTexture.cpp index be7a0f1b91..e302694043 100644 --- a/gpu/src/GrGLTexture.cpp +++ b/gpu/src/GrGLTexture.cpp @@ -151,7 +151,7 @@ void GrGLTexture::uploadTextureData(int x, 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 c3b5f975d5..92d836da1e 100644 --- a/gpu/src/GrGpuGL.cpp +++ b/gpu/src/GrGpuGL.cpp @@ -788,7 +788,7 @@ void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc, 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 f79afff753..75a8b42497 100644 --- a/include/core/SkTemplates.h +++ b/include/core/SkTemplates.h @@ -181,7 +181,7 @@ public: } // 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 @@ public: } // 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 d299223215..b38d4d016c 100644 --- a/include/core/SkTypes.h +++ b/include/core/SkTypes.h @@ -387,20 +387,57 @@ private: 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 @@ template class SkAutoSMalloc : SkNoncopyable { 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 @@ public: */ explicit SkAutoSMalloc(size_t size) { fPtr = fStorage; - this->realloc(size); + this->reset(size); } /** @@ -454,7 +491,7 @@ public: * 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 cf94982481..ecdcd7a080 100644 --- a/src/gpu/SkGpuDevice.cpp +++ b/src/gpu/SkGpuDevice.cpp @@ -1393,7 +1393,7 @@ void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode, 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 a0c42c02c4..05818ce761 100644 --- a/src/ports/SkFontHost_mac_coretext.cpp +++ b/src/ports/SkFontHost_mac_coretext.cpp @@ -547,7 +547,7 @@ void SkScalerContext_Mac::generateImage(const SkGlyph& glyph) { } 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; }