# HG changeset patch # User Matti Hamalainen # Date 1670364938 -7200 # Node ID 98d12f33da7bcf01b168b37d9188360147d51542 # Parent 1a3ea8f7bb354afc2d6e706730882d5fe7688e44 Change th_ringbuf_new() API. diff -r 1a3ea8f7bb35 -r 98d12f33da7b th_datastruct.c --- 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; } diff -r 1a3ea8f7bb35 -r 98d12f33da7b th_datastruct.h --- 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);