diff -r e8f354a25ac8 -r 68754c7de906 src/cx/allocator.h --- a/src/cx/allocator.h Thu Nov 07 20:22:56 2024 +0100 +++ b/src/cx/allocator.h Thu Nov 07 22:46:58 2024 +0100 @@ -46,6 +46,8 @@ /** * The allocator's malloc() implementation. */ + cx_attr_nonnull + cx_attr_nodiscard cx_attr_allocsize(2) void *(*malloc)( void *data, size_t n @@ -54,7 +56,8 @@ /** * The allocator's realloc() implementation. */ - __attribute__((__warn_unused_result__)) + cx_attr_nodiscard + cx_attr_allocsize(3) void *(*realloc)( void *data, void *mem, @@ -64,6 +67,8 @@ /** * The allocator's calloc() implementation. */ + cx_attr_nodiscard + cx_attr_allocsize(2, 3) void *(*calloc)( void *data, size_t nelem, @@ -73,7 +78,7 @@ /** * The allocator's free() implementation. */ - __attribute__((__nonnull__)) + cx_attr_nonnull_arg(1) void (*free)( void *data, void *mem @@ -109,12 +114,11 @@ * * A destructor function deallocates possible contents and MAY free the memory * pointed to by \p memory. Read the documentation of the respective function - * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that - * particular implementation. + * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in + * that particular implementation. * * @param memory a pointer to the object to destruct */ -__attribute__((__nonnull__)) typedef void (*cx_destructor_func)(void *memory); /** @@ -122,20 +126,20 @@ * * A destructor function deallocates possible contents and MAY free the memory * pointed to by \p memory. Read the documentation of the respective function - * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that - * particular implementation. + * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in + * that particular implementation. * * @param data an optional pointer to custom data * @param memory a pointer to the object to destruct */ -__attribute__((__nonnull__(2))) typedef void (*cx_destructor_func2)( void *data, void *memory ); /** - * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * 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. @@ -144,14 +148,16 @@ * @param n the new size in bytes * @return zero on success, non-zero on failure */ -__attribute__((__nonnull__)) +cx_attr_nonnull +cx_attr_nodiscard int cx_reallocate( void **mem, size_t n ); /** - * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * 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. * @@ -164,7 +170,8 @@ * @param size the size of each element * @return zero on success, non-zero on failure */ -__attribute__((__nonnull__)) +cx_attr_nonnull +cx_attr_nodiscard int cx_reallocatearray( void **mem, size_t nmemb, @@ -172,7 +179,8 @@ ); /** - * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * 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. @@ -184,7 +192,8 @@ #define cx_reallocate(mem, n) cx_reallocate((void**)(mem), n) /** - * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * 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. * @@ -197,7 +206,22 @@ * @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) +#define cx_reallocatearray(mem, nmemb, size) \ + cx_reallocatearray((void**)(mem), nmemb, size) + +/** + * Free a block allocated by this allocator. + * + * \note Freeing a block of a different allocator is undefined. + * + * @param allocator the allocator + * @param mem a pointer to the block to free + */ +cx_attr_nonnull_arg(1) +void cxFree( + const CxAllocator *allocator, + void *mem +); /** * Allocate \p n bytes of memory. @@ -206,18 +230,21 @@ * @param n the number of bytes * @return a pointer to the allocated memory */ -__attribute__((__malloc__)) -__attribute__((__nonnull__)) -__attribute__((__alloc_size__(2))) +cx_attr_nodiscard +cx_attr_nonnull +cx_attr_malloc +cx_attr_dealloc_ucx +cx_attr_allocsize(2) void *cxMalloc( const CxAllocator *allocator, size_t n ); /** - * 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. + * 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. * * \note Re-allocating a block allocated by a different allocator is undefined. * @@ -226,9 +253,10 @@ * @param n the new size in bytes * @return a pointer to the re-allocated memory */ -__attribute__((__warn_unused_result__)) -__attribute__((__nonnull__(1))) -__attribute__((__alloc_size__(3))) +cx_attr_nodiscard +cx_attr_nonnull_arg(1) +cx_attr_dealloc_ucx +cx_attr_allocsize(3) void *cxRealloc( const CxAllocator *allocator, void *mem, @@ -236,12 +264,14 @@ ); /** - * 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. + * 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. + * 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. * @@ -251,9 +281,10 @@ * @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))) +cx_attr_nodiscard +cx_attr_nonnull_arg(1) +cx_attr_dealloc_ucx +cx_attr_allocsize(3, 4) void *cxReallocArray( const CxAllocator *allocator, void *mem, @@ -262,7 +293,8 @@ ); /** - * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * 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. @@ -275,7 +307,8 @@ * @param n the new size in bytes * @return zero on success, non-zero on failure */ -__attribute__((__nonnull__)) +cx_attr_nodiscard +cx_attr_nonnull int cxReallocate( const CxAllocator *allocator, void **mem, @@ -283,7 +316,8 @@ ); /** - * Re-allocate a previously allocated block and changes the pointer in-place, if necessary. + * 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. @@ -296,11 +330,14 @@ * @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) +#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. + * 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. * @@ -314,7 +351,8 @@ * @param size the size of each element * @return zero on success, non-zero on failure */ -__attribute__((__nonnull__)) +cx_attr_nodiscard +cx_attr_nonnull int cxReallocateArray( const CxAllocator *allocator, void **mem, @@ -323,8 +361,10 @@ ); /** - * 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. + * 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. * @@ -349,29 +389,17 @@ * @param n the size of each element in bytes * @return a pointer to the allocated memory */ -__attribute__((__malloc__)) -__attribute__((__nonnull__)) -__attribute__((__alloc_size__(2, 3))) +cx_attr_nonnull_arg(1) +cx_attr_nodiscard +cx_attr_malloc +cx_attr_dealloc_ucx +cx_attr_allocsize(2, 3) void *cxCalloc( const CxAllocator *allocator, size_t nelem, size_t n ); -/** - * Free a block allocated by this allocator. - * - * \note Freeing a block of a different allocator is undefined. - * - * @param allocator the allocator - * @param mem a pointer to the block to free - */ -__attribute__((__nonnull__(1))) -void cxFree( - const CxAllocator *allocator, - void *mem -); - #ifdef __cplusplus } // extern "C" #endif