--- a/src/cx/buffer.h Thu Nov 07 20:22:56 2024 +0100 +++ b/src/cx/buffer.h Thu Nov 07 22:46:58 2024 +0100 @@ -51,7 +51,7 @@ #include <sys/types.h> -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -155,7 +155,7 @@ * @param flags buffer features (see cx_buffer_s.flags) * @return zero on success, non-zero if a required allocation failed */ -__attribute__((__nonnull__(1))) +cx_attr_nonnull_arg(1) int cxBufferInit( CxBuffer *buffer, void *space, @@ -165,6 +165,32 @@ ); /** + * Destroys the buffer contents. + * + * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. + * If you want to free the memory of the entire buffer, use cxBufferFree(). + * + * @param buffer the buffer which contents shall be destroyed + * @see cxBufferInit() + */ +cx_attr_nonnull +void cxBufferDestroy(CxBuffer *buffer); + +/** + * Deallocates the buffer. + * + * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys + * the contents. If you \em only want to destroy the contents, use cxBufferDestroy(). + * + * \remark As with all free() functions, this accepts \c NULL arguments in which + * case it does nothing. + * + * @param buffer the buffer to deallocate + * @see cxBufferCreate() + */ +void cxBufferFree(CxBuffer *buffer); + +/** * Allocates and initializes a fresh buffer. * * \note You may provide \c NULL as argument for \p space. @@ -179,6 +205,9 @@ * @param flags buffer features (see cx_buffer_s.flags) * @return a pointer to the buffer on success, \c NULL if a required allocation failed */ +cx_attr_malloc +cx_attr_dealloc(cxBufferFree, 1) +cx_attr_nodiscard CxBuffer *cxBufferCreate( void *space, size_t capacity, @@ -187,30 +216,6 @@ ); /** - * Destroys the buffer contents. - * - * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. - * If you want to free the memory of the entire buffer, use cxBufferFree(). - * - * @param buffer the buffer which contents shall be destroyed - * @see cxBufferInit() - */ -__attribute__((__nonnull__)) -void cxBufferDestroy(CxBuffer *buffer); - -/** - * Deallocates the buffer. - * - * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys - * the contents. If you \em only want to destroy the contents, use cxBufferDestroy(). - * - * @param buffer the buffer to deallocate - * @see cxBufferCreate() - */ -__attribute__((__nonnull__)) -void cxBufferFree(CxBuffer *buffer); - -/** * Shifts the contents of the buffer by the given offset. * * If the offset is positive, the contents are shifted to the right. @@ -243,7 +248,7 @@ * @param shift the shift offset (negative means left shift) * @return 0 on success, non-zero if a required auto-extension fails */ -__attribute__((__nonnull__)) +cx_attr_nonnull int cxBufferShift( CxBuffer *buffer, off_t shift @@ -258,7 +263,7 @@ * @return 0 on success, non-zero if a required auto-extension fails * @see cxBufferShift() */ -__attribute__((__nonnull__)) +cx_attr_nonnull int cxBufferShiftRight( CxBuffer *buffer, size_t shift @@ -276,7 +281,7 @@ * @return always zero * @see cxBufferShift() */ -__attribute__((__nonnull__)) +cx_attr_nonnull int cxBufferShiftLeft( CxBuffer *buffer, size_t shift @@ -302,7 +307,7 @@ * @return 0 on success, non-zero if the position is invalid * */ -__attribute__((__nonnull__)) +cx_attr_nonnull int cxBufferSeek( CxBuffer *buffer, off_t offset, @@ -318,7 +323,7 @@ * @param buffer the buffer to be cleared * @see cxBufferReset() */ -__attribute__((__nonnull__)) +cx_attr_nonnull void cxBufferClear(CxBuffer *buffer); /** @@ -330,7 +335,7 @@ * @param buffer the buffer to be cleared * @see cxBufferClear() */ -__attribute__((__nonnull__)) +cx_attr_nonnull void cxBufferReset(CxBuffer *buffer); /** @@ -340,7 +345,8 @@ * @return non-zero, if the current buffer position has exceeded the last * byte of the buffer's contents. */ -__attribute__((__nonnull__)) +cx_attr_nonnull +cx_attr_nodiscard int cxBufferEof(const CxBuffer *buffer); @@ -353,7 +359,7 @@ * @param capacity the minimum required capacity for this buffer * @return 0 on success or a non-zero value on failure */ -__attribute__((__nonnull__)) +cx_attr_nonnull int cxBufferMinimumCapacity( CxBuffer *buffer, size_t capacity @@ -385,7 +391,7 @@ * @param buffer the CxBuffer to write to * @return the total count of elements written */ -__attribute__((__nonnull__)) +cx_attr_nonnull size_t cxBufferWrite( const void *ptr, size_t size, @@ -406,7 +412,7 @@ * @param buffer the CxBuffer to read from * @return the total number of elements read */ -__attribute__((__nonnull__)) +cx_attr_nonnull size_t cxBufferRead( void *ptr, size_t size, @@ -429,7 +435,7 @@ * @return the byte that has bean written or \c EOF when the end of the stream is * reached and automatic extension is not enabled or not possible */ -__attribute__((__nonnull__)) +cx_attr_nonnull int cxBufferPut( CxBuffer *buffer, int c @@ -442,7 +448,8 @@ * @param str the zero-terminated string * @return the number of bytes written */ -__attribute__((__nonnull__)) +cx_attr_nonnull +cx_attr_cstr_arg(2) size_t cxBufferPutString( CxBuffer *buffer, const char *str @@ -456,7 +463,7 @@ * @param buffer the buffer to read from * @return the character or \c EOF, if the end of the buffer is reached */ -__attribute__((__nonnull__)) +cx_attr_nonnull int cxBufferGet(CxBuffer *buffer); #ifdef __cplusplus