lib: wrap the memory allocation functions

In preparation for adding custom memory allocator support, don't call
the standard memory allocation functions directly but rather wrap them
with libdeflate_malloc() and libdeflate_free().
This commit is contained in:
Eric Biggers 2020-04-02 23:49:00 -07:00
parent 66bd59c4be
commit 944500af9f
4 changed files with 24 additions and 6 deletions

View File

@ -27,7 +27,6 @@
* OTHER DEALINGS IN THE SOFTWARE. * OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "deflate_compress.h" #include "deflate_compress.h"

View File

@ -45,7 +45,6 @@
*/ */
#include <limits.h> #include <limits.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "deflate_constants.h" #include "deflate_constants.h"
@ -987,11 +986,16 @@ libdeflate_alloc_decompressor(void)
* *
* But for simplicity, we currently just zero the whole decompressor. * But for simplicity, we currently just zero the whole decompressor.
*/ */
return calloc(1, sizeof(struct libdeflate_decompressor)); struct libdeflate_decompressor *d = libdeflate_malloc(sizeof(*d));
if (d == NULL)
return NULL;
memset(d, 0, sizeof(*d));
return d;
} }
LIBDEFLATEEXPORT void LIBDEFLATEAPI LIBDEFLATEEXPORT void LIBDEFLATEAPI
libdeflate_free_decompressor(struct libdeflate_decompressor *d) libdeflate_free_decompressor(struct libdeflate_decompressor *d)
{ {
free(d); libdeflate_free(d);
} }

View File

@ -30,6 +30,9 @@
#define _cpu_features SYM_FIXUP(_cpu_features) #define _cpu_features SYM_FIXUP(_cpu_features)
#define setup_cpu_features SYM_FIXUP(setup_cpu_features) #define setup_cpu_features SYM_FIXUP(setup_cpu_features)
void *libdeflate_malloc(size_t size);
void libdeflate_free(void *ptr);
void *libdeflate_aligned_malloc(size_t alignment, size_t size); void *libdeflate_aligned_malloc(size_t alignment, size_t size);
void libdeflate_aligned_free(void *ptr); void libdeflate_aligned_free(void *ptr);

View File

@ -29,10 +29,22 @@
#include "lib_common.h" #include "lib_common.h"
void *
libdeflate_malloc(size_t size)
{
return malloc(size);
}
void
libdeflate_free(void *ptr)
{
free(ptr);
}
void * void *
libdeflate_aligned_malloc(size_t alignment, size_t size) libdeflate_aligned_malloc(size_t alignment, size_t size)
{ {
void *ptr = malloc(sizeof(void *) + alignment - 1 + size); void *ptr = libdeflate_malloc(sizeof(void *) + alignment - 1 + size);
if (ptr) { if (ptr) {
void *orig_ptr = ptr; void *orig_ptr = ptr;
ptr = (void *)ALIGN((uintptr_t)ptr + sizeof(void *), alignment); ptr = (void *)ALIGN((uintptr_t)ptr + sizeof(void *), alignment);
@ -45,5 +57,5 @@ void
libdeflate_aligned_free(void *ptr) libdeflate_aligned_free(void *ptr)
{ {
if (ptr) if (ptr)
free(((void **)ptr)[-1]); libdeflate_free(((void **)ptr)[-1]);
} }