changeset 731:98d12f33da7b

Change th_ringbuf_new() API.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 07 Dec 2022 00:15:38 +0200
parents 1a3ea8f7bb35
children d190dccc3ee6
files th_datastruct.c th_datastruct.h
diffstat 2 files changed, 11 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/th_datastruct.c	Wed Dec 07 00:06:45 2022 +0200
+++ b/th_datastruct.c	Wed Dec 07 00:15:38 2022 +0200
@@ -288,9 +288,9 @@
  */
 int th_ringbuf_init(th_ringbuf_t *buf, const size_t size, void (*mdeallocator)(void *data))
 {
+    memset(buf, 0, sizeof(*buf));
+
     // Must have at least 2 elements
-    memset(buf, 0, sizeof(th_ringbuf_t));
-
     if (size < 2)
         return THERR_BOUNDS;
 
@@ -305,22 +305,22 @@
 }
 
 
-th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data))
+int th_ringbuf_new(th_ringbuf_t **buf, const size_t size, void (*mdeallocator)(void *data))
 {
-    th_ringbuf_t *buf;
     int res;
 
-    if ((buf = th_malloc0(sizeof(th_ringbuf_t))) == NULL)
-        return NULL;
+    if ((*buf = th_malloc0(sizeof(th_ringbuf_t))) == NULL)
+        return THERR_MALLOC;
 
-    if ((res = th_ringbuf_init(buf, size, mdeallocator)) != THERR_OK)
+    if ((res = th_ringbuf_init(*buf, size, mdeallocator)) != THERR_OK)
     {
         th_free(buf);
-        return NULL;
+        return res;
     }
 
-    buf->allocated = TRUE;
-    return buf;
+    (*buf)->allocated = TRUE;
+
+    return THERR_OK;
 }
 
 
--- a/th_datastruct.h	Wed Dec 07 00:06:45 2022 +0200
+++ b/th_datastruct.h	Wed Dec 07 00:15:38 2022 +0200
@@ -64,7 +64,7 @@
 
 
 int            th_ringbuf_init(th_ringbuf_t *buf, const size_t size, void (*mdeallocator)(void *data));
-th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data));
+int            th_ringbuf_new(th_ringbuf_t **buf, const size_t size, void (*mdeallocator)(void *data));
 int            th_ringbuf_grow(th_ringbuf_t *buf, const size_t n);
 void           th_ringbuf_free(th_ringbuf_t *buf);
 void           th_ringbuf_add(th_ringbuf_t *buf, void *ptr);