diff -r cd418898af5c -r 2f601274bbac src/cx/allocator.h --- a/src/cx/allocator.h Thu Oct 31 14:54:44 2024 +0100 +++ b/src/cx/allocator.h Thu Oct 31 17:53:55 2024 +0100 @@ -153,6 +153,27 @@ /** * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. * + * The size is calculated by multiplying \p nemb and \p size. + * + * \par Error handling + * \c errno will be set by realloc() on failure or when the multiplication of + * \p nmemb and \p size overflows. + * + * @param mem pointer to the pointer to allocated block + * @param nmemb the number of elements + * @param size the size of each element + * @return zero on success, non-zero on failure + */ +__attribute__((__nonnull__)) +int cx_reallocatearray( + void **mem, + size_t nmemb, + size_t size +); + +/** + * 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. * @@ -163,6 +184,22 @@ #define cx_reallocate(mem, n) cx_reallocate((void**)(mem), n) /** + * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * + * The size is calculated by multiplying \p nemb and \p size. + * + * \par Error handling + * \c errno will be set by realloc() on failure or when the multiplication of + * \p nmemb and \p size overflows. + * + * @param mem pointer to the pointer to allocated block + * @param nmemb the number of elements + * @param size the size of each element + * @return zero on success, non-zero on failure + */ +#define cx_reallocatearray(mem, nmemb, size) cx_reallocatearray((void**)(mem), nmemb, size) + +/** * Allocate \p n bytes of memory. * * @param allocator the allocator @@ -170,6 +207,7 @@ * @return a pointer to the allocated memory */ __attribute__((__malloc__)) +__attribute__((__nonnull__)) __attribute__((__alloc_size__(2))) void *cxMalloc( const CxAllocator *allocator, @@ -189,6 +227,7 @@ * @return a pointer to the re-allocated memory */ __attribute__((__warn_unused_result__)) +__attribute__((__nonnull__(1))) __attribute__((__alloc_size__(3))) void *cxRealloc( const CxAllocator *allocator, @@ -197,6 +236,32 @@ ); /** + * Re-allocate the previously allocated block in \p mem, making the new block \p n bytes long. + * This function may return the same pointer that was passed to it, if moving the memory + * was not necessary. + * + * The size is calculated by multiplying \p nemb and \p size. + * If that multiplication overflows, this function returns \c NULL and \c errno will be set. + * + * \note Re-allocating a block allocated by a different allocator is undefined. + * + * @param allocator the allocator + * @param mem pointer to the previously allocated block + * @param nmemb the number of elements + * @param size the size of each element + * @return a pointer to the re-allocated memory + */ +__attribute__((__warn_unused_result__)) +__attribute__((__nonnull__(1))) +__attribute__((__alloc_size__(3, 4))) +void *cxReallocArray( + const CxAllocator *allocator, + void *mem, + size_t nmemb, + size_t size +); + +/** * 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. * @@ -218,6 +283,65 @@ ); /** + * 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. + * + * \note Re-allocating a block allocated by a different allocator is undefined. + * + * \par Error handling + * \c errno will be set, if the underlying realloc function does so. + * + * @param allocator the allocator + * @param mem pointer to the pointer to allocated block + * @param n the new size in bytes + * @return zero on success, non-zero on failure + */ +#define cxReallocate(allocator, mem, n) cxReallocate(allocator, (void**)(mem), n) + +/** + * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * This function acts like cxReallocArray() using the pointer pointed to by \p mem. + * + * \note Re-allocating a block allocated by a different allocator is undefined. + * + * \par Error handling + * \c errno will be set, if the underlying realloc function does so or the + * multiplication of \p nmemb and \p size overflows. + * + * @param allocator the allocator + * @param mem pointer to the pointer to allocated block + * @param nmemb the number of elements + * @param size the size of each element + * @return zero on success, non-zero on failure + */ +__attribute__((__nonnull__)) +int cxReallocateArray( + const CxAllocator *allocator, + void **mem, + size_t nmemb, + size_t size +); + +/** + * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * This function acts like cxReallocArray() using the pointer pointed to by \p mem. + * + * \note Re-allocating a block allocated by a different allocator is undefined. + * + * \par Error handling + * \c errno will be set, if the underlying realloc function does so or the + * multiplication of \p nmemb and \p size overflows. + * + * @param allocator the allocator + * @param mem pointer to the pointer to allocated block + * @param nmemb the number of elements + * @param size the size of each element + * @return zero on success, non-zero on failure + */ +#define cxReallocateArray(allocator, mem, nmemb, size) \ + cxReallocateArray(allocator, (void**) (mem), nmemb, size) + +/** * Allocate \p nelem elements of \p n bytes each, all initialized to zero. * * @param allocator the allocator @@ -226,6 +350,7 @@ * @return a pointer to the allocated memory */ __attribute__((__malloc__)) +__attribute__((__nonnull__)) __attribute__((__alloc_size__(2, 3))) void *cxCalloc( const CxAllocator *allocator, @@ -241,7 +366,7 @@ * @param allocator the allocator * @param mem a pointer to the block to free */ -__attribute__((__nonnull__)) +__attribute__((__nonnull__(1))) void cxFree( const CxAllocator *allocator, void *mem