# HG changeset patch # User Mike Becker # Date 1643645759 -3600 # Node ID 9a08f5e515cc291a6c0dfdb361e0aeafaf443f47 # Parent eb9e7bd40a8e3ba734d28f662b6c999608f3d532 add allocator support to CxBuffer Also change how the buffer itself is allocated and destroyed. diff -r eb9e7bd40a8e -r 9a08f5e515cc src/buffer.c --- a/src/buffer.c Sun Jan 30 14:19:00 2022 +0100 +++ b/src/buffer.c Mon Jan 31 17:15:59 2022 +0100 @@ -32,39 +32,37 @@ #include #include -CxBuffer *cxBufferCreate( +int cxBufferInit( + CxBuffer *buffer, void *space, size_t capacity, + CxAllocator *allocator, int flags ) { - CxBuffer *buffer = (CxBuffer *) malloc(sizeof(cx_buffer_s)); - if (buffer) { - buffer->flags = flags; - if (!space) { - buffer->bytes = malloc(capacity); - if (!buffer->bytes) { - free(buffer); - return NULL; - } - memset(buffer->bytes, 0, capacity); - buffer->flags |= CX_BUFFER_FREE_CONTENTS; - } else { - buffer->bytes = space; + buffer->allocator = allocator; + buffer->flags = flags; + if (!space) { + buffer->bytes = cxMalloc(allocator, capacity); + if (buffer->bytes == NULL) { + return 1; } - buffer->capacity = capacity; - buffer->size = 0; + memset(buffer->bytes, 0, capacity); + buffer->flags |= CX_BUFFER_FREE_CONTENTS; + } else { + buffer->bytes = space; + } + buffer->capacity = capacity; + buffer->size = 0; - buffer->pos = 0; - } + buffer->pos = 0; - return buffer; + return 0; } void cxBufferDestroy(CxBuffer *buffer) { if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) { - free(buffer->bytes); + cxFree(buffer->allocator, buffer->bytes); } - free(buffer); } CxBuffer *cxBufferExtract( diff -r eb9e7bd40a8e -r 9a08f5e515cc src/cx/buffer.h --- a/src/cx/buffer.h Sun Jan 30 14:19:00 2022 +0100 +++ b/src/cx/buffer.h Mon Jan 31 17:15:59 2022 +0100 @@ -48,6 +48,7 @@ #define UCX_BUFFER_H #include "common.h" +#include "allocator.h" #include #ifdef __cplusplus @@ -82,6 +83,8 @@ */ unsigned char *bytes; }; + /** The allocator to use for automatic memory management. */ + CxAllocator *allocator; /** Current position of the buffer. */ size_t pos; /** Current capacity (i.e. maximum size) of the buffer. */ @@ -103,31 +106,34 @@ typedef cx_buffer_s CxBuffer; /** - * Creates a new buffer. + * Initializes a fresh buffer. * * \note You may provide \c NULL as argument for \p space. * Then this function will allocate the space and enforce * the #CX_BUFFER_FREE_CONTENTS flag. * - * @param space pointer to the memory area, or NULL to allocate + * @param buffer the buffer to initialize + * @param space pointer to the memory area, or \c NULL to allocate * new memory * @param capacity the capacity of the buffer + * @param allocator the allocator this buffer shall use for automatic memory management * @param flags buffer features (see cx_buffer_s.flags) - * @return the new buffer + * @return zero on success, non-zero if a required allocation failed */ -CxBuffer *cxBufferCreate( +int cxBufferInit( + CxBuffer *buffer, void *space, size_t capacity, + CxAllocator *allocator, int flags ); /** - * Destroys a buffer. + * Destroys the buffer contents. * - * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, the contents of the buffer - * are also freed. + * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. * - * @param buffer the buffer to destroy + * @param buffer the buffer which contents shall be destroyed */ void cxBufferDestroy(CxBuffer *buffer);