add allocator support to CxBuffer

Mon, 31 Jan 2022 17:15:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 31 Jan 2022 17:15:59 +0100
changeset 501
9a08f5e515cc
parent 500
eb9e7bd40a8e
child 502
33e7b6ebf403

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
--- 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 <stdlib.h>
 #include <string.h>
 
-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(
--- 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 <stdio.h>
 
 #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 <code>NULL</code> 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);
 

mercurial