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:
skal
2014-03-27 23:27:32 +01:00
committed by Gerrit Code Review
parent 51f406a5d7
commit af93bdd6bc
32 changed files with 168 additions and 134 deletions

View File

@ -175,7 +175,7 @@ static void WebPPictureGrabSpecs(const WebPPicture* const src,
// the other YUV(A) buffer.
static int PictureAllocARGB(WebPPicture* const picture) {
WebPPicture tmp;
free(picture->memory_argb_);
WebPSafeFree(picture->memory_argb_);
PictureResetARGB(picture);
picture->use_argb = 1;
WebPPictureGrabSpecs(picture, &tmp);
@ -191,8 +191,8 @@ static int PictureAllocARGB(WebPPicture* const picture) {
// Release memory owned by 'picture' (both YUV and ARGB buffers).
void WebPPictureFree(WebPPicture* picture) {
if (picture != NULL) {
free(picture->memory_);
free(picture->memory_argb_);
WebPSafeFree(picture->memory_);
WebPSafeFree(picture->memory_argb_);
PictureResetYUVA(picture);
PictureResetARGB(picture);
}
@ -504,7 +504,7 @@ int WebPPictureRescale(WebPPicture* pic, int width, int height) {
AlphaMultiplyARGB(&tmp, 1);
}
WebPPictureFree(pic);
free(work);
WebPSafeFree(work);
*pic = tmp;
return 1;
}
@ -538,7 +538,7 @@ int WebPMemoryWrite(const uint8_t* data, size_t data_size,
if (w->size > 0) {
memcpy(new_mem, w->mem, w->size);
}
free(w->mem);
WebPSafeFree(w->mem);
w->mem = new_mem;
// down-cast is ok, thanks to WebPSafeMalloc
w->max_size = (size_t)next_max_size;
@ -550,6 +550,15 @@ int WebPMemoryWrite(const uint8_t* data, size_t data_size,
return 1;
}
void WebPMemoryWriterClear(WebPMemoryWriter* writer) {
if (writer != NULL) {
WebPSafeFree(writer->mem);
writer->mem = NULL;
writer->size = 0;
writer->max_size = 0;
}
}
//------------------------------------------------------------------------------
// Detection of non-trivial transparency
@ -1323,7 +1332,7 @@ static size_t Encode(const uint8_t* rgba, int width, int height, int stride,
ok = import(&pic, rgba, stride) && WebPEncode(&config, &pic);
WebPPictureFree(&pic);
if (!ok) {
free(wrt.mem);
WebPMemoryWriterClear(&wrt);
*output = NULL;
return 0;
}