mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-19 07:20:02 +02:00
use WebPSafe[CM]alloc/WebPSafeFree instead of [cm]alloc/free
there's still some malloc/free in the external example This is an encoder API change because of the introduction of WebPMemoryWriterClear() for symmetry reasons. The MemoryWriter object should probably go in examples/ instead of being in the main lib, though. mux_types.h stil contain some inlined free()/malloc() that are harder to remove (we need to put them in the libwebputils lib and make sure link is ok). Left as a TODO for now. Also: WebPDecodeRGB*() function are still returning a pointer that needs to be free()'d. We should call WebPSafeFree() on these, but it means exposing the whole mechanism. TODO(later). Change-Id: Iad2c9060f7fa6040e3ba489c8b07f4caadfab77b
This commit is contained in:
@ -108,7 +108,7 @@ static int AnalyzeEntropy(const uint32_t* argb,
|
||||
|
||||
VP8LHistogram* nonpredicted = NULL;
|
||||
VP8LHistogram* predicted =
|
||||
(VP8LHistogram*)malloc(2 * sizeof(*predicted));
|
||||
(VP8LHistogram*)WebPSafeMalloc(2ULL, sizeof(*predicted));
|
||||
if (predicted == NULL) return 0;
|
||||
nonpredicted = predicted + 1;
|
||||
|
||||
@ -135,7 +135,7 @@ static int AnalyzeEntropy(const uint32_t* argb,
|
||||
}
|
||||
*nonpredicted_bits = VP8LHistogramEstimateBitsBulk(nonpredicted);
|
||||
*predicted_bits = VP8LHistogramEstimateBitsBulk(predicted);
|
||||
free(predicted);
|
||||
WebPSafeFree(predicted);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -228,7 +228,7 @@ static int GetHuffBitLengthsAndCodes(
|
||||
|
||||
End:
|
||||
if (!ok) {
|
||||
free(mem_buf);
|
||||
WebPSafeFree(mem_buf);
|
||||
// If one VP8LCreateHuffmanTree() above fails, we need to clean up behind.
|
||||
memset(huffman_codes, 0, 5 * histogram_image_size * sizeof(*huffman_codes));
|
||||
}
|
||||
@ -362,7 +362,7 @@ static int StoreFullHuffmanCode(VP8LBitWriter* const bw,
|
||||
}
|
||||
ok = 1;
|
||||
End:
|
||||
free(tokens);
|
||||
WebPSafeFree(tokens);
|
||||
return ok;
|
||||
}
|
||||
|
||||
@ -503,9 +503,9 @@ static int EncodeImageNoHuffman(VP8LBitWriter* const bw,
|
||||
ok = 1;
|
||||
|
||||
Error:
|
||||
free(histogram_image);
|
||||
WebPSafeFree(histogram_image);
|
||||
VP8LClearBackwardRefs(&refs);
|
||||
free(huffman_codes[0].codes);
|
||||
WebPSafeFree(huffman_codes[0].codes);
|
||||
return ok;
|
||||
}
|
||||
|
||||
@ -532,8 +532,8 @@ static int EncodeImageInternal(VP8LBitWriter* const bw,
|
||||
assert(histogram_bits <= MAX_HUFFMAN_BITS);
|
||||
|
||||
if (histogram_image == NULL || histogram_symbols == NULL) {
|
||||
free(histogram_image);
|
||||
free(histogram_symbols);
|
||||
WebPSafeFree(histogram_image);
|
||||
WebPSafeFree(histogram_symbols);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -559,7 +559,7 @@ static int EncodeImageInternal(VP8LBitWriter* const bw,
|
||||
goto Error;
|
||||
}
|
||||
// Free combined histograms.
|
||||
free(histogram_image);
|
||||
WebPSafeFree(histogram_image);
|
||||
histogram_image = NULL;
|
||||
|
||||
// Color Cache parameters.
|
||||
@ -593,7 +593,7 @@ static int EncodeImageInternal(VP8LBitWriter* const bw,
|
||||
VP8LSubSampleSize(width, histogram_bits),
|
||||
VP8LSubSampleSize(height, histogram_bits),
|
||||
quality);
|
||||
free(histogram_argb);
|
||||
WebPSafeFree(histogram_argb);
|
||||
if (!ok) goto Error;
|
||||
}
|
||||
}
|
||||
@ -614,14 +614,14 @@ static int EncodeImageInternal(VP8LBitWriter* const bw,
|
||||
ok = 1;
|
||||
|
||||
Error:
|
||||
free(histogram_image);
|
||||
WebPSafeFree(histogram_image);
|
||||
|
||||
VP8LClearBackwardRefs(&refs);
|
||||
if (huffman_codes != NULL) {
|
||||
free(huffman_codes->codes);
|
||||
free(huffman_codes);
|
||||
WebPSafeFree(huffman_codes->codes);
|
||||
WebPSafeFree(huffman_codes);
|
||||
}
|
||||
free(histogram_symbols);
|
||||
WebPSafeFree(histogram_symbols);
|
||||
return ok;
|
||||
}
|
||||
|
||||
@ -637,7 +637,8 @@ static int EvalAndApplySubtractGreen(VP8LEncoder* const enc,
|
||||
int i;
|
||||
const uint32_t* const argb = enc->argb_;
|
||||
double bit_cost_before, bit_cost_after;
|
||||
VP8LHistogram* const histo = (VP8LHistogram*)malloc(sizeof(*histo));
|
||||
VP8LHistogram* const histo =
|
||||
(VP8LHistogram*)WebPSafeMalloc(1ULL, sizeof(*histo));
|
||||
if (histo == NULL) return 0;
|
||||
|
||||
VP8LHistogramInit(histo, 1);
|
||||
@ -656,7 +657,7 @@ static int EvalAndApplySubtractGreen(VP8LEncoder* const enc,
|
||||
++histo->blue_[((c >> 0) - green) & 0xff];
|
||||
}
|
||||
bit_cost_after = VP8LHistogramEstimateBits(histo);
|
||||
free(histo);
|
||||
WebPSafeFree(histo);
|
||||
|
||||
// Check if subtracting green yields low entropy.
|
||||
enc->use_subtract_green_ = (bit_cost_after < bit_cost_before);
|
||||
@ -907,7 +908,7 @@ static WebPEncodingError EncodePalette(VP8LBitWriter* const bw,
|
||||
}
|
||||
|
||||
Error:
|
||||
free(row);
|
||||
WebPSafeFree(row);
|
||||
return err;
|
||||
}
|
||||
|
||||
@ -944,7 +945,7 @@ static void FinishEncParams(VP8LEncoder* const enc) {
|
||||
|
||||
static VP8LEncoder* VP8LEncoderNew(const WebPConfig* const config,
|
||||
const WebPPicture* const picture) {
|
||||
VP8LEncoder* const enc = (VP8LEncoder*)calloc(1, sizeof(*enc));
|
||||
VP8LEncoder* const enc = (VP8LEncoder*)WebPSafeCalloc(1ULL, sizeof(*enc));
|
||||
if (enc == NULL) {
|
||||
WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
|
||||
return NULL;
|
||||
@ -959,9 +960,9 @@ static VP8LEncoder* VP8LEncoderNew(const WebPConfig* const config,
|
||||
|
||||
static void VP8LEncoderDelete(VP8LEncoder* enc) {
|
||||
if (enc != NULL) {
|
||||
free(enc->argb_);
|
||||
WebPSafeFree(enc->argb_);
|
||||
WebPSafeFree(enc);
|
||||
}
|
||||
free(enc);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user