--- a/ucx/allocator.c Fri Jan 03 21:40:57 2025 +0100 +++ b/ucx/allocator.c Sat Jan 04 13:03:01 2025 +0100 @@ -28,9 +28,11 @@ #include "cx/allocator.h" +#include <errno.h> + __attribute__((__malloc__, __alloc_size__(2))) static void *cx_malloc_stdlib( - __attribute__((__unused__)) void *d, + cx_attr_unused void *d, size_t n ) { return malloc(n); @@ -38,7 +40,7 @@ __attribute__((__warn_unused_result__, __alloc_size__(3))) static void *cx_realloc_stdlib( - __attribute__((__unused__)) void *d, + cx_attr_unused void *d, void *mem, size_t n ) { @@ -47,7 +49,7 @@ __attribute__((__malloc__, __alloc_size__(2, 3))) static void *cx_calloc_stdlib( - __attribute__((__unused__)) void *d, + cx_attr_unused void *d, size_t nelem, size_t n ) { @@ -56,7 +58,7 @@ __attribute__((__nonnull__)) static void cx_free_stdlib( - __attribute__((__unused__)) void *d, + cx_attr_unused void *d, void *mem ) { free(mem); @@ -75,45 +77,98 @@ }; CxAllocator *cxDefaultAllocator = &cx_default_allocator; - +#undef cx_reallocate int cx_reallocate( void **mem, size_t n ) { void *nmem = realloc(*mem, n); if (nmem == NULL) { - return 1; + return 1; // LCOV_EXCL_LINE } else { *mem = nmem; return 0; } } +#undef cx_reallocatearray +int cx_reallocatearray( + void **mem, + size_t nmemb, + size_t size +) { + size_t n; + if (cx_szmul(nmemb, size, &n)) { + errno = EOVERFLOW; + return 1; + } else { + void *nmem = realloc(*mem, n); + if (nmem == NULL) { + return 1; // LCOV_EXCL_LINE + } else { + *mem = nmem; + return 0; + } + } +} + // IMPLEMENTATION OF HIGH LEVEL API void *cxMalloc( - CxAllocator const *allocator, + const CxAllocator *allocator, size_t n ) { return allocator->cl->malloc(allocator->data, n); } void *cxRealloc( - CxAllocator const *allocator, + const CxAllocator *allocator, void *mem, size_t n ) { return allocator->cl->realloc(allocator->data, mem, n); } +void *cxReallocArray( + const CxAllocator *allocator, + void *mem, + size_t nmemb, + size_t size +) { + size_t n; + if (cx_szmul(nmemb, size, &n)) { + errno = EOVERFLOW; + return NULL; + } else { + return allocator->cl->realloc(allocator->data, mem, n); + } +} + +#undef cxReallocate int cxReallocate( - CxAllocator const *allocator, + const CxAllocator *allocator, void **mem, size_t n ) { void *nmem = allocator->cl->realloc(allocator->data, *mem, n); if (nmem == NULL) { - return 1; + return 1; // LCOV_EXCL_LINE + } else { + *mem = nmem; + return 0; + } +} + +#undef cxReallocateArray +int cxReallocateArray( + const CxAllocator *allocator, + void **mem, + size_t nmemb, + size_t size +) { + void *nmem = cxReallocArray(allocator, *mem, nmemb, size); + if (nmem == NULL) { + return 1; // LCOV_EXCL_LINE } else { *mem = nmem; return 0; @@ -121,7 +176,7 @@ } void *cxCalloc( - CxAllocator const *allocator, + const CxAllocator *allocator, size_t nelem, size_t n ) { @@ -129,7 +184,7 @@ } void cxFree( - CxAllocator const *allocator, + const CxAllocator *allocator, void *mem ) { allocator->cl->free(allocator->data, mem);