Mon, 31 Jan 2022 17:15:59 +0100
add allocator support to CxBuffer
Also change how the buffer itself is allocated and destroyed.
src/buffer.c | file | annotate | diff | comparison | revisions | |
src/cx/buffer.h | file | annotate | diff | comparison | revisions |
1.1 --- a/src/buffer.c Sun Jan 30 14:19:00 2022 +0100 1.2 +++ b/src/buffer.c Mon Jan 31 17:15:59 2022 +0100 1.3 @@ -32,39 +32,37 @@ 1.4 #include <stdlib.h> 1.5 #include <string.h> 1.6 1.7 -CxBuffer *cxBufferCreate( 1.8 +int cxBufferInit( 1.9 + CxBuffer *buffer, 1.10 void *space, 1.11 size_t capacity, 1.12 + CxAllocator *allocator, 1.13 int flags 1.14 ) { 1.15 - CxBuffer *buffer = (CxBuffer *) malloc(sizeof(cx_buffer_s)); 1.16 - if (buffer) { 1.17 - buffer->flags = flags; 1.18 - if (!space) { 1.19 - buffer->bytes = malloc(capacity); 1.20 - if (!buffer->bytes) { 1.21 - free(buffer); 1.22 - return NULL; 1.23 - } 1.24 - memset(buffer->bytes, 0, capacity); 1.25 - buffer->flags |= CX_BUFFER_FREE_CONTENTS; 1.26 - } else { 1.27 - buffer->bytes = space; 1.28 + buffer->allocator = allocator; 1.29 + buffer->flags = flags; 1.30 + if (!space) { 1.31 + buffer->bytes = cxMalloc(allocator, capacity); 1.32 + if (buffer->bytes == NULL) { 1.33 + return 1; 1.34 } 1.35 - buffer->capacity = capacity; 1.36 - buffer->size = 0; 1.37 + memset(buffer->bytes, 0, capacity); 1.38 + buffer->flags |= CX_BUFFER_FREE_CONTENTS; 1.39 + } else { 1.40 + buffer->bytes = space; 1.41 + } 1.42 + buffer->capacity = capacity; 1.43 + buffer->size = 0; 1.44 1.45 - buffer->pos = 0; 1.46 - } 1.47 + buffer->pos = 0; 1.48 1.49 - return buffer; 1.50 + return 0; 1.51 } 1.52 1.53 void cxBufferDestroy(CxBuffer *buffer) { 1.54 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) { 1.55 - free(buffer->bytes); 1.56 + cxFree(buffer->allocator, buffer->bytes); 1.57 } 1.58 - free(buffer); 1.59 } 1.60 1.61 CxBuffer *cxBufferExtract(
2.1 --- a/src/cx/buffer.h Sun Jan 30 14:19:00 2022 +0100 2.2 +++ b/src/cx/buffer.h Mon Jan 31 17:15:59 2022 +0100 2.3 @@ -48,6 +48,7 @@ 2.4 #define UCX_BUFFER_H 2.5 2.6 #include "common.h" 2.7 +#include "allocator.h" 2.8 #include <stdio.h> 2.9 2.10 #ifdef __cplusplus 2.11 @@ -82,6 +83,8 @@ 2.12 */ 2.13 unsigned char *bytes; 2.14 }; 2.15 + /** The allocator to use for automatic memory management. */ 2.16 + CxAllocator *allocator; 2.17 /** Current position of the buffer. */ 2.18 size_t pos; 2.19 /** Current capacity (i.e. maximum size) of the buffer. */ 2.20 @@ -103,31 +106,34 @@ 2.21 typedef cx_buffer_s CxBuffer; 2.22 2.23 /** 2.24 - * Creates a new buffer. 2.25 + * Initializes a fresh buffer. 2.26 * 2.27 * \note You may provide \c NULL as argument for \p space. 2.28 * Then this function will allocate the space and enforce 2.29 * the #CX_BUFFER_FREE_CONTENTS flag. 2.30 * 2.31 - * @param space pointer to the memory area, or <code>NULL</code> to allocate 2.32 + * @param buffer the buffer to initialize 2.33 + * @param space pointer to the memory area, or \c NULL to allocate 2.34 * new memory 2.35 * @param capacity the capacity of the buffer 2.36 + * @param allocator the allocator this buffer shall use for automatic memory management 2.37 * @param flags buffer features (see cx_buffer_s.flags) 2.38 - * @return the new buffer 2.39 + * @return zero on success, non-zero if a required allocation failed 2.40 */ 2.41 -CxBuffer *cxBufferCreate( 2.42 +int cxBufferInit( 2.43 + CxBuffer *buffer, 2.44 void *space, 2.45 size_t capacity, 2.46 + CxAllocator *allocator, 2.47 int flags 2.48 ); 2.49 2.50 /** 2.51 - * Destroys a buffer. 2.52 + * Destroys the buffer contents. 2.53 * 2.54 - * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, the contents of the buffer 2.55 - * are also freed. 2.56 + * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. 2.57 * 2.58 - * @param buffer the buffer to destroy 2.59 + * @param buffer the buffer which contents shall be destroyed 2.60 */ 2.61 void cxBufferDestroy(CxBuffer *buffer); 2.62