Expose WebPMalloc() in addition to WebPFree()

and use it at various places, including for WebPData.

This is an API change!

Change-Id: Ic041323a1179c465292a4f981a86c4c34635d243
This commit is contained in:
Skal
2019-10-15 23:16:51 +02:00
parent af650c0bd2
commit a4df4aae73
12 changed files with 59 additions and 43 deletions

View File

@ -60,15 +60,15 @@ static int AllocateFrames(AnimatedImage* const image, uint32_t num_frames) {
!CheckSizeForOverflow(total_frame_size)) {
return 0;
}
mem = (uint8_t*)malloc((size_t)total_size);
frames = (DecodedFrame*)malloc((size_t)total_frame_size);
mem = (uint8_t*)WebPMalloc((size_t)total_size);
frames = (DecodedFrame*)WebPMalloc((size_t)total_frame_size);
if (mem == NULL || frames == NULL) {
free(mem);
free(frames);
WebPFree(mem);
WebPFree(frames);
return 0;
}
free(image->raw_mem);
WebPFree(image->raw_mem);
image->num_frames = num_frames;
image->frames = frames;
for (i = 0; i < num_frames; ++i) {
@ -82,8 +82,8 @@ static int AllocateFrames(AnimatedImage* const image, uint32_t num_frames) {
void ClearAnimatedImage(AnimatedImage* const image) {
if (image != NULL) {
free(image->raw_mem);
free(image->frames);
WebPFree(image->raw_mem);
WebPFree(image->frames);
image->num_frames = 0;
image->frames = NULL;
image->raw_mem = NULL;
@ -165,7 +165,7 @@ static int DumpFrame(const char filename[], const char dump_folder[],
base_name = (base_name == NULL) ? (const W_CHAR*)filename : base_name + 1;
max_len = WSTRLEN(dump_folder) + 1 + WSTRLEN(base_name)
+ strlen("_frame_") + strlen(".pam") + 8;
file_name = (W_CHAR*)malloc(max_len * sizeof(*file_name));
file_name = (W_CHAR*)WebPMalloc(max_len * sizeof(*file_name));
if (file_name == NULL) goto End;
if (WSNPRINTF(file_name, max_len, "%s/%s_frame_%d.pam",
@ -197,7 +197,7 @@ static int DumpFrame(const char filename[], const char dump_folder[],
ok = 1;
End:
if (f != NULL) fclose(f);
free(file_name);
WebPFree(file_name);
return ok;
}

View File

@ -128,7 +128,8 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic,
static void AllocExtraInfo(WebPPicture* const pic) {
const int mb_w = (pic->width + 15) / 16;
const int mb_h = (pic->height + 15) / 16;
pic->extra_info = (uint8_t*)malloc(mb_w * mb_h * sizeof(*pic->extra_info));
pic->extra_info =
(uint8_t*)WebPMalloc(mb_w * mb_h * sizeof(*pic->extra_info));
}
static void PrintByteCount(const int bytes[4], int total_size,
@ -1168,7 +1169,7 @@ int main(int argc, const char* argv[]) {
Error:
WebPMemoryWriterClear(&memory_writer);
free(picture.extra_info);
WebPFree(picture.extra_info);
MetadataFree(&metadata);
WebPPictureFree(&picture);
WebPPictureFree(&original_picture);

View File

@ -132,7 +132,7 @@ static uint8_t* AllocateExternalBuffer(WebPDecoderConfig* config,
format == RGB_565) ? 2
: 4;
uint32_t stride = bpp * w + 7; // <- just for exercising
external_buffer = (uint8_t*)malloc(stride * h);
external_buffer = (uint8_t*)WebPMalloc(stride * h);
if (external_buffer == NULL) return NULL;
output_buffer->u.RGBA.stride = stride;
output_buffer->u.RGBA.size = stride * h;
@ -145,7 +145,7 @@ static uint8_t* AllocateExternalBuffer(WebPDecoderConfig* config,
uint32_t total_size = stride * h * (has_alpha ? 2 : 1)
+ 2 * uv_stride * (h + 1) / 2;
assert(format >= YUV && format <= YUVA);
external_buffer = (uint8_t*)malloc(total_size);
external_buffer = (uint8_t*)WebPMalloc(total_size);
if (external_buffer == NULL) return NULL;
tmp = external_buffer;
output_buffer->u.YUVA.y = tmp;
@ -412,8 +412,8 @@ int main(int argc, const char* argv[]) {
}
Exit:
WebPFreeDecBuffer(output_buffer);
free((void*)external_buffer);
free((void*)data);
WebPFree((void*)external_buffer);
WebPFree((void*)data);
FREE_WARGV_AND_RETURN(ok ? 0 : -1);
}

View File

@ -75,7 +75,7 @@ static void ResetCommandLineArguments(int argc, const char* argv[],
void ExUtilDeleteCommandLineArguments(CommandLineArguments* const args) {
if (args != NULL) {
if (args->own_argv_) {
free((void*)args->argv_);
WebPFree((void*)args->argv_);
WebPDataClear(&args->argv_data_);
}
ResetCommandLineArguments(0, NULL, args);
@ -102,7 +102,7 @@ int ExUtilInitCommandLineArguments(int argc, const char* argv[],
return 0;
}
args->own_argv_ = 1;
args->argv_ = (const char**)malloc(MAX_ARGC * sizeof(*args->argv_));
args->argv_ = (const char**)WebPMalloc(MAX_ARGC * sizeof(*args->argv_));
if (args->argv_ == NULL) return 0;
argc = 0;

View File

@ -137,7 +137,7 @@ int GIFReadFrame(GifFileType* const gif, int transparent_index,
}
dst = sub_image.argb;
tmp = (uint8_t*)malloc(rect.width * sizeof(*tmp));
tmp = (uint8_t*)WebPMalloc(rect.width * sizeof(*tmp));
if (tmp == NULL) goto End;
if (image_desc->Interlace) { // Interlaced image.
@ -168,7 +168,7 @@ int GIFReadFrame(GifFileType* const gif, int transparent_index,
End:
if (!ok) picture->error_code = sub_image.error_code;
WebPPictureFree(&sub_image);
free(tmp);
WebPFree(tmp);
return ok;
}

View File

@ -1045,7 +1045,7 @@ static int Process(const Config* config) {
int* durations = NULL;
WebPMux* new_mux = DuplicateMuxHeader(mux);
if (new_mux == NULL) goto Err2;
durations = (int*)malloc((size_t)num_frames * sizeof(*durations));
durations = (int*)WebPMalloc((size_t)num_frames * sizeof(*durations));
if (durations == NULL) goto Err2;
for (i = 0; i < num_frames; ++i) durations[i] = -1;
@ -1103,7 +1103,7 @@ static int Process(const Config* config) {
new_mux = NULL;
Err3:
free(durations);
WebPFree(durations);
WebPMuxDelete(new_mux);
if (!ok) goto Err2;
}