Switch SkAutoMalloc to SkAutoTMalloc to avoid cast
Make SkAutoTMalloc's interface look more like SkAutoMalloc:
- add free(), which does what you expect
- make reset() return a pointer fPtr
No public API changes (SkAutoTMalloc is in include/private)
BUG=skia:2148
Review URL: https://codereview.chromium.org/1516833003
diff --git a/src/codec/SkCodec_libpng.cpp b/src/codec/SkCodec_libpng.cpp
index 744bca4..3086a36 100644
--- a/src/codec/SkCodec_libpng.cpp
+++ b/src/codec/SkCodec_libpng.cpp
@@ -14,6 +14,7 @@
#include "SkSize.h"
#include "SkStream.h"
#include "SkSwizzler.h"
+#include "SkTemplates.h"
///////////////////////////////////////////////////////////////////////////////
// Helper macros
@@ -503,7 +504,7 @@
// error?
int row = 0;
// This must be declared above the call to setjmp to avoid memory leaks on incomplete images.
- SkAutoMalloc storage;
+ SkAutoTMalloc<uint8_t> storage;
if (setjmp(png_jmpbuf(fPng_ptr))) {
// Assume that any error that occurs while reading rows is caused by an incomplete input.
if (fNumberPasses > 1) {
@@ -535,7 +536,7 @@
const size_t srcRowBytes = width * bpp;
storage.reset(width * height * bpp);
- uint8_t* const base = static_cast<uint8_t*>(storage.get());
+ uint8_t* const base = storage.get();
for (int i = 0; i < fNumberPasses; i++) {
uint8_t* srcRow = base;
@@ -555,7 +556,7 @@
}
} else {
storage.reset(requestedInfo.width() * SkSwizzler::BytesPerPixel(fSrcConfig));
- uint8_t* srcRow = static_cast<uint8_t*>(storage.get());
+ uint8_t* srcRow = storage.get();
for (; row < requestedInfo.height(); row++) {
png_read_rows(fPng_ptr, &srcRow, png_bytepp_NULL, 1);
// FIXME: Only call IsOpaque once, outside the loop. Same for onGetScanlines.
@@ -642,7 +643,7 @@
fAlphaState = kUnknown_AlphaState;
fStorage.reset(this->getInfo().width() * SkSwizzler::BytesPerPixel(this->srcConfig()));
- fSrcRow = static_cast<uint8_t*>(fStorage.get());
+ fSrcRow = fStorage.get();
return kSuccess;
}
@@ -696,7 +697,7 @@
private:
AlphaState fAlphaState;
- SkAutoMalloc fStorage;
+ SkAutoTMalloc<uint8_t> fStorage;
uint8_t* fSrcRow;
typedef SkPngCodec INHERITED;
@@ -769,8 +770,8 @@
// fail on the first pass, we can still report than some scanlines are initialized.
return 0;
}
- SkAutoMalloc storage(count * fSrcRowBytes);
- uint8_t* storagePtr = static_cast<uint8_t*>(storage.get());
+ SkAutoTMalloc<uint8_t> storage(count * fSrcRowBytes);
+ uint8_t* storagePtr = storage.get();
uint8_t* srcRow;
const int startRow = this->nextScanline();
for (int i = 0; i < this->numberPasses(); i++) {
diff --git a/src/codec/SkJpegCodec.cpp b/src/codec/SkJpegCodec.cpp
index 6ea13b7..7db772d 100644
--- a/src/codec/SkJpegCodec.cpp
+++ b/src/codec/SkJpegCodec.cpp
@@ -350,12 +350,12 @@
fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, options));
fStorage.reset(get_row_bytes(fDecoderMgr->dinfo()));
- fSrcRow = static_cast<uint8_t*>(fStorage.get());
+ fSrcRow = fStorage.get();
}
SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) {
if (!createIfNecessary || fSwizzler) {
- SkASSERT(!fSwizzler || (fSrcRow && static_cast<uint8_t*>(fStorage.get()) == fSrcRow));
+ SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow));
return fSwizzler;
}
@@ -433,8 +433,8 @@
#ifndef TURBO_HAS_SKIP
// TODO (msarett): Avoid reallocating the memory buffer on each call to skip.
static uint32_t jpeg_skip_scanlines(jpeg_decompress_struct* dinfo, int count) {
- SkAutoMalloc storage(get_row_bytes(dinfo));
- uint8_t* storagePtr = static_cast<uint8_t*>(storage.get());
+ SkAutoTMalloc<uint8_t> storage(get_row_bytes(dinfo));
+ uint8_t* storagePtr = storage.get();
for (int y = 0; y < count; y++) {
if (1 != jpeg_read_scanlines(dinfo, &storagePtr, 1)) {
return y;
diff --git a/src/codec/SkJpegCodec.h b/src/codec/SkJpegCodec.h
index ed94b61..8e2db81 100644
--- a/src/codec/SkJpegCodec.h
+++ b/src/codec/SkJpegCodec.h
@@ -13,6 +13,7 @@
#include "SkJpegDecoderMgr.h"
#include "SkJpegUtility_codec.h"
#include "SkStream.h"
+#include "SkTemplates.h"
extern "C" {
#include "jpeglib.h"
@@ -111,7 +112,7 @@
const int fReadyState;
// scanline decoding
- SkAutoMalloc fStorage; // Only used if sampling is needed
+ SkAutoTMalloc<uint8_t> fStorage; // Only used if sampling is needed
uint8_t* fSrcRow; // Only used if sampling is needed
SkAutoTDelete<SkSwizzler> fSwizzler;
diff --git a/src/codec/SkSampledCodec.cpp b/src/codec/SkSampledCodec.cpp
index 52e5648..38859ad 100644
--- a/src/codec/SkSampledCodec.cpp
+++ b/src/codec/SkSampledCodec.cpp
@@ -9,6 +9,7 @@
#include "SkCodecPriv.h"
#include "SkMath.h"
#include "SkSampledCodec.h"
+#include "SkTemplates.h"
SkSampledCodec::SkSampledCodec(SkCodec* codec)
: INHERITED(codec->getInfo())
@@ -267,8 +268,8 @@
}
case SkCodec::kNone_SkScanlineOrder: {
const int linesNeeded = subsetHeight - samplingOffsetY;
- SkAutoMalloc storage(linesNeeded * rowBytes);
- uint8_t* storagePtr = static_cast<uint8_t*>(storage.get());
+ SkAutoTMalloc<uint8_t> storage(linesNeeded * rowBytes);
+ uint8_t* storagePtr = storage.get();
if (!fCodec->skipScanlines(startY)) {
fCodec->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized,
diff --git a/src/codec/SkWebpCodec.cpp b/src/codec/SkWebpCodec.cpp
index 2137877..6cfb385 100644
--- a/src/codec/SkWebpCodec.cpp
+++ b/src/codec/SkWebpCodec.cpp
@@ -230,8 +230,8 @@
return kInvalidInput;
}
- SkAutoMalloc storage(BUFFER_SIZE);
- uint8_t* buffer = static_cast<uint8_t*>(storage.get());
+ SkAutoTMalloc<uint8_t> storage(BUFFER_SIZE);
+ uint8_t* buffer = storage.get();
while (true) {
const size_t bytesRead = stream()->read(buffer, BUFFER_SIZE);
if (0 == bytesRead) {
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp
index e8a2355..8997f2b 100644
--- a/src/core/SkBitmap.cpp
+++ b/src/core/SkBitmap.cpp
@@ -17,6 +17,7 @@
#include "SkReadBuffer.h"
#include "SkRect.h"
#include "SkScalar.h"
+#include "SkTemplates.h"
#include "SkUnPreMultiply.h"
#include "SkWriteBuffer.h"
@@ -1082,8 +1083,8 @@
info.flatten(*buffer);
const size_t size = snugRB * info.height();
- SkAutoMalloc storage(size);
- char* dst = (char*)storage.get();
+ SkAutoTMalloc<char> storage(size);
+ char* dst = storage.get();
for (int y = 0; y < info.height(); ++y) {
memcpy(dst, src, snugRB);
dst += snugRB;
diff --git a/src/core/SkDraw.cpp b/src/core/SkDraw.cpp
index addd6ec..7ebad61 100644
--- a/src/core/SkDraw.cpp
+++ b/src/core/SkDraw.cpp
@@ -1234,8 +1234,8 @@
}
// allocate (and clear) our temp buffer to hold the transformed bitmap
- SkAutoMalloc storage(size);
- mask.fImage = (uint8_t*)storage.get();
+ SkAutoTMalloc<uint8_t> storage(size);
+ mask.fImage = storage.get();
memset(mask.fImage, 0, size);
// now draw our bitmap(src) into mask(dst), transformed by the matrix
diff --git a/src/effects/SkColorCubeFilter.cpp b/src/effects/SkColorCubeFilter.cpp
index 07b229c..7f1f596 100644
--- a/src/effects/SkColorCubeFilter.cpp
+++ b/src/effects/SkColorCubeFilter.cpp
@@ -99,7 +99,7 @@
// We need 256 SkScalar * 2 for fColorToFactors and 256 SkScalar
// for fColorToScalar, so a total of 768 SkScalar.
cache->fLutStorage.reset(512 * sizeof(int) + 768 * sizeof(SkScalar));
- uint8_t* storage = (uint8_t*)cache->fLutStorage.get();
+ uint8_t* storage = cache->fLutStorage.get();
cache->fColorToIndex[0] = (int*)storage;
cache->fColorToIndex[1] = cache->fColorToIndex[0] + 256;
cache->fColorToFactors[0] = (SkScalar*)(storage + (512 * sizeof(int)));
diff --git a/src/images/SkImageDecoder_libgif.cpp b/src/images/SkImageDecoder_libgif.cpp
index ef55a8f..2677b13 100644
--- a/src/images/SkImageDecoder_libgif.cpp
+++ b/src/images/SkImageDecoder_libgif.cpp
@@ -378,8 +378,8 @@
SkAutoLockPixels alp(*bm);
- SkAutoMalloc storage(innerWidth);
- uint8_t* scanline = (uint8_t*) storage.get();
+ SkAutoTMalloc<uint8_t> storage(innerWidth);
+ uint8_t* scanline = storage.get();
// GIF has an option to store the scanlines of an image, plus a larger background,
// filled by a fill color. In this case, we will use a subset of the larger bitmap
diff --git a/src/images/SkImageDecoder_libjpeg.cpp b/src/images/SkImageDecoder_libjpeg.cpp
index 219dad6..0d02a65 100644
--- a/src/images/SkImageDecoder_libjpeg.cpp
+++ b/src/images/SkImageDecoder_libjpeg.cpp
@@ -496,8 +496,8 @@
return return_failure(cinfo, *bm, "sampler.begin");
}
- SkAutoMalloc srcStorage(cinfo.output_width * srcBytesPerPixel);
- uint8_t* srcRow = (uint8_t*)srcStorage.get();
+ SkAutoTMalloc<uint8_t> srcStorage(cinfo.output_width * srcBytesPerPixel);
+ uint8_t* srcRow = srcStorage.get();
// Possibly skip initial rows [sampler.srcY0]
if (!skip_src_rows(&cinfo, srcRow, sampler.srcY0())) {
@@ -931,7 +931,7 @@
skjpeg_destination_mgr sk_wstream(stream);
// allocate these before set call setjmp
- SkAutoMalloc oneRow;
+ SkAutoTMalloc<uint8_t> oneRow;
cinfo.err = jpeg_std_error(&sk_err);
sk_err.error_exit = skjpeg_error_exit;
@@ -966,7 +966,7 @@
jpeg_start_compress(&cinfo, TRUE);
const int width = bm.width();
- uint8_t* oneRowP = (uint8_t*)oneRow.reset(width * 3);
+ uint8_t* oneRowP = oneRow.reset(width * 3);
const SkPMColor* colors = bm.getColorTable() ? bm.getColorTable()->readColors() : nullptr;
const void* srcRow = bm.getPixels();
diff --git a/src/images/SkImageDecoder_libpng.cpp b/src/images/SkImageDecoder_libpng.cpp
index 6acbf29..cd8152a 100644
--- a/src/images/SkImageDecoder_libpng.cpp
+++ b/src/images/SkImageDecoder_libpng.cpp
@@ -401,8 +401,8 @@
const int height = decodedBitmap->height();
if (number_passes > 1) {
- SkAutoMalloc storage(origWidth * origHeight * srcBytesPerPixel);
- uint8_t* base = (uint8_t*)storage.get();
+ SkAutoTMalloc<uint8_t> storage(origWidth * origHeight * srcBytesPerPixel);
+ uint8_t* base = storage.get();
size_t rowBytes = origWidth * srcBytesPerPixel;
for (int i = 0; i < number_passes; i++) {
@@ -420,8 +420,8 @@
base += sampler.srcDY() * rowBytes;
}
} else {
- SkAutoMalloc storage(origWidth * srcBytesPerPixel);
- uint8_t* srcRow = (uint8_t*)storage.get();
+ SkAutoTMalloc<uint8_t> storage(origWidth * srcBytesPerPixel);
+ uint8_t* srcRow = storage.get();
skip_src_rows(png_ptr, srcRow, sampler.srcY0());
for (int y = 0; y < height; y++) {
@@ -966,8 +966,8 @@
png_write_info(png_ptr, info_ptr);
const char* srcImage = (const char*)bitmap.getPixels();
- SkAutoSMalloc<1024> rowStorage(bitmap.width() << 2);
- char* storage = (char*)rowStorage.get();
+ SkAutoSTMalloc<1024, char> rowStorage(bitmap.width() << 2);
+ char* storage = rowStorage.get();
transform_scanline_proc proc = choose_proc(ct, hasAlpha);
for (int y = 0; y < bitmap.height(); y++) {
diff --git a/src/images/SkImageDecoder_libwebp.cpp b/src/images/SkImageDecoder_libwebp.cpp
index 07ff83d..5253577 100644
--- a/src/images/SkImageDecoder_libwebp.cpp
+++ b/src/images/SkImageDecoder_libwebp.cpp
@@ -201,8 +201,8 @@
}
const size_t readBufferSize = stream->hasLength() ?
SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_BUFFER_SZ;
- SkAutoMalloc srcStorage(readBufferSize);
- unsigned char* input = (uint8_t*)srcStorage.get();
+ SkAutoTMalloc<unsigned char> srcStorage(readBufferSize);
+ unsigned char* input = srcStorage.get();
if (nullptr == input) {
WebPIDelete(idec);
WebPFreeDecBuffer(&config->output);
diff --git a/src/svg/parser/SkSVG.cpp b/src/svg/parser/SkSVG.cpp
index fdfc13a..8ee7d02 100644
--- a/src/svg/parser/SkSVG.cpp
+++ b/src/svg/parser/SkSVG.cpp
@@ -8,7 +8,8 @@
#include "SkSVG.h"
-#include 'SkSVGParser.h"
+#include "SkSVGParser.h"
+#include "SkTemplates.h"
SkSVG::SkSVG() {
}
@@ -19,8 +20,8 @@
bool SkSVG::decodeStream(SkStream* stream);
{
size_t size = stream->read(nil, 0);
- SkAutoMalloc storage(size);
- char* data = (char*)storage.get();
+ SkAutoTMalloc<char> storage(size);
+ char* data = storage.get();
size_t actual = stream->read(data, size);
SkASSERT(size == actual);
SkSVGParser parser(*fMaker);