mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-13 14:34:33 +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:
@ -15,7 +15,9 @@
|
||||
#include <assert.h>
|
||||
#include <string.h> // for memcpy()
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "./bit_writer.h"
|
||||
#include "./utils.h"
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// VP8BitWriter
|
||||
@ -34,7 +36,7 @@ static int BitWriterResize(VP8BitWriter* const bw, size_t extra_size) {
|
||||
new_size = 2 * bw->max_pos_;
|
||||
if (new_size < needed_size) new_size = needed_size;
|
||||
if (new_size < 1024) new_size = 1024;
|
||||
new_buf = (uint8_t*)malloc(new_size);
|
||||
new_buf = (uint8_t*)WebPSafeMalloc(1ULL, new_size);
|
||||
if (new_buf == NULL) {
|
||||
bw->error_ = 1;
|
||||
return 0;
|
||||
@ -43,7 +45,7 @@ static int BitWriterResize(VP8BitWriter* const bw, size_t extra_size) {
|
||||
assert(bw->buf_ != NULL);
|
||||
memcpy(new_buf, bw->buf_, bw->pos_);
|
||||
}
|
||||
free(bw->buf_);
|
||||
WebPSafeFree(bw->buf_);
|
||||
bw->buf_ = new_buf;
|
||||
bw->max_pos_ = new_size;
|
||||
return 1;
|
||||
@ -176,7 +178,7 @@ uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) {
|
||||
|
||||
int VP8BitWriterAppend(VP8BitWriter* const bw,
|
||||
const uint8_t* data, size_t size) {
|
||||
assert(data);
|
||||
assert(data != NULL);
|
||||
if (bw->nb_bits_ != -8) return 0; // kFlush() must have been called
|
||||
if (!BitWriterResize(bw, size)) return 0;
|
||||
memcpy(bw->buf_ + bw->pos_, data, size);
|
||||
@ -185,8 +187,8 @@ int VP8BitWriterAppend(VP8BitWriter* const bw,
|
||||
}
|
||||
|
||||
void VP8BitWriterWipeOut(VP8BitWriter* const bw) {
|
||||
if (bw) {
|
||||
free(bw->buf_);
|
||||
if (bw != NULL) {
|
||||
WebPSafeFree(bw->buf_);
|
||||
memset(bw, 0, sizeof(*bw));
|
||||
}
|
||||
}
|
||||
@ -245,7 +247,7 @@ static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) {
|
||||
if (allocated_size < size_required) allocated_size = size_required;
|
||||
// make allocated size multiple of 1k
|
||||
allocated_size = (((allocated_size >> 10) + 1) << 10);
|
||||
allocated_buf = (uint8_t*)malloc(allocated_size);
|
||||
allocated_buf = (uint8_t*)WebPSafeMalloc(1ULL, allocated_size);
|
||||
if (allocated_buf == NULL) {
|
||||
bw->error_ = 1;
|
||||
return 0;
|
||||
@ -253,7 +255,7 @@ static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) {
|
||||
if (current_size > 0) {
|
||||
memcpy(allocated_buf, bw->buf_, current_size);
|
||||
}
|
||||
free(bw->buf_);
|
||||
WebPSafeFree(bw->buf_);
|
||||
bw->buf_ = allocated_buf;
|
||||
bw->cur_ = bw->buf_ + current_size;
|
||||
bw->end_ = bw->buf_ + allocated_size;
|
||||
@ -267,7 +269,7 @@ int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size) {
|
||||
|
||||
void VP8LBitWriterDestroy(VP8LBitWriter* const bw) {
|
||||
if (bw != NULL) {
|
||||
free(bw->buf_);
|
||||
WebPSafeFree(bw->buf_);
|
||||
memset(bw, 0, sizeof(*bw));
|
||||
}
|
||||
}
|
||||
@ -307,4 +309,3 @@ uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw) {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
@ -32,7 +32,7 @@ int VP8LColorCacheInit(VP8LColorCache* const cc, int hash_bits) {
|
||||
|
||||
void VP8LColorCacheClear(VP8LColorCache* const cc) {
|
||||
if (cc != NULL) {
|
||||
free(cc->colors_);
|
||||
WebPSafeFree(cc->colors_);
|
||||
cc->colors_ = NULL;
|
||||
}
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ static int TreeInit(HuffmanTree* const tree, int num_leaves) {
|
||||
|
||||
void HuffmanTreeRelease(HuffmanTree* const tree) {
|
||||
if (tree != NULL) {
|
||||
free(tree->root_);
|
||||
WebPSafeFree(tree->root_);
|
||||
tree->root_ = NULL;
|
||||
tree->max_nodes_ = 0;
|
||||
tree->num_nodes_ = 0;
|
||||
@ -245,7 +245,7 @@ int HuffmanTreeBuildImplicit(HuffmanTree* const tree,
|
||||
}
|
||||
ok = 1;
|
||||
End:
|
||||
free(codes);
|
||||
WebPSafeFree(codes);
|
||||
ok = ok && IsFull(tree);
|
||||
if (!ok) HuffmanTreeRelease(tree);
|
||||
return ok;
|
||||
|
@ -43,7 +43,7 @@ static int OptimizeHuffmanForRle(int length, int* const counts) {
|
||||
}
|
||||
// 2) Let's mark all population counts that already can be encoded
|
||||
// with an rle code.
|
||||
good_for_rle = (uint8_t*)calloc(length, 1);
|
||||
good_for_rle = (uint8_t*)WebPSafeCalloc(1ULL, length);
|
||||
if (good_for_rle == NULL) {
|
||||
return 0;
|
||||
}
|
||||
@ -119,7 +119,7 @@ static int OptimizeHuffmanForRle(int length, int* const counts) {
|
||||
}
|
||||
}
|
||||
}
|
||||
free(good_for_rle);
|
||||
WebPSafeFree(good_for_rle);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -272,7 +272,7 @@ static int GenerateOptimalTree(const int* const histogram, int histogram_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
free(tree);
|
||||
WebPSafeFree(tree);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -38,5 +38,8 @@ void* WebPSafeCalloc(uint64_t nmemb, size_t size) {
|
||||
return calloc((size_t)nmemb, size);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void WebPSafeFree(void* const ptr) {
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -40,6 +40,9 @@ void* WebPSafeMalloc(uint64_t nmemb, size_t size);
|
||||
// in order to favor the "calloc(num_foo, sizeof(foo))" pattern.
|
||||
void* WebPSafeCalloc(uint64_t nmemb, size_t size);
|
||||
|
||||
// Companion deallocation function to the above allocations.
|
||||
void WebPSafeFree(void* const ptr);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Reading/writing data.
|
||||
|
||||
|
Reference in New Issue
Block a user