# HG changeset patch # User Mike Becker # Date 1687972681 -7200 # Node ID 44986c0e2b05ed17cb44188225b86f2c9412c3b1 # Parent c9b882bef838b3d20159aa96e00c84917848e965 add a low-level stdlib-based cx_reallocate() diff -r c9b882bef838 -r 44986c0e2b05 src/allocator.c --- a/src/allocator.c Tue Jun 27 20:04:48 2023 +0200 +++ b/src/allocator.c Wed Jun 28 19:18:01 2023 +0200 @@ -75,6 +75,20 @@ }; CxAllocator *cxDefaultAllocator = &cx_default_allocator; + +int cx_reallocate( + void **mem, + size_t n +) { + void *nmem = realloc(*mem, n); + if (nmem == NULL) { + return 1; + } else { + *mem = nmem; + return 0; + } +} + // IMPLEMENTATION OF HIGH LEVEL API void *cxMalloc( diff -r c9b882bef838 -r 44986c0e2b05 src/cx/allocator.h --- a/src/cx/allocator.h Tue Jun 27 20:04:48 2023 +0200 +++ b/src/cx/allocator.h Wed Jun 28 19:18:01 2023 +0200 @@ -133,6 +133,22 @@ ) __attribute__((__nonnull__(2))); /** + * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * + * \par Error handling + * \c errno will be set by realloc() on failure. + * + * @param mem pointer to the pointer to allocated block + * @param n the new size in bytes + * @return zero on success, non-zero on failure + */ +int cx_reallocate( + void **mem, + size_t n +) +__attribute__((__nonnull__)); + +/** * Allocate \p n bytes of memory. * * @param allocator the allocator @@ -169,7 +185,6 @@ /** * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. * This function acts like cxRealloc() using the pointer pointed to by \p mem. - * On success, the pointer is changed to the new location (in case the * * \note Re-allocating a block allocated by a different allocator is undefined. *