comparison th_datastruct.c @ 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
comparison
equal deleted inserted replaced
730:1a3ea8f7bb35 731:98d12f33da7b
286 /* 286 /*
287 * Ringbuffers 287 * Ringbuffers
288 */ 288 */
289 int th_ringbuf_init(th_ringbuf_t *buf, const size_t size, void (*mdeallocator)(void *data)) 289 int th_ringbuf_init(th_ringbuf_t *buf, const size_t size, void (*mdeallocator)(void *data))
290 { 290 {
291 memset(buf, 0, sizeof(*buf));
292
291 // Must have at least 2 elements 293 // Must have at least 2 elements
292 memset(buf, 0, sizeof(th_ringbuf_t));
293
294 if (size < 2) 294 if (size < 2)
295 return THERR_BOUNDS; 295 return THERR_BOUNDS;
296 296
297 if ((buf->data = th_calloc(size, sizeof(void *))) == NULL) 297 if ((buf->data = th_calloc(size, sizeof(void *))) == NULL)
298 return THERR_MALLOC; 298 return THERR_MALLOC;
303 303
304 return THERR_OK; 304 return THERR_OK;
305 } 305 }
306 306
307 307
308 th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data)) 308 int th_ringbuf_new(th_ringbuf_t **buf, const size_t size, void (*mdeallocator)(void *data))
309 { 309 {
310 th_ringbuf_t *buf;
311 int res; 310 int res;
312 311
313 if ((buf = th_malloc0(sizeof(th_ringbuf_t))) == NULL) 312 if ((*buf = th_malloc0(sizeof(th_ringbuf_t))) == NULL)
314 return NULL; 313 return THERR_MALLOC;
315 314
316 if ((res = th_ringbuf_init(buf, size, mdeallocator)) != THERR_OK) 315 if ((res = th_ringbuf_init(*buf, size, mdeallocator)) != THERR_OK)
317 { 316 {
318 th_free(buf); 317 th_free(buf);
319 return NULL; 318 return res;
320 } 319 }
321 320
322 buf->allocated = TRUE; 321 (*buf)->allocated = TRUE;
323 return buf; 322
323 return THERR_OK;
324 } 324 }
325 325
326 326
327 int th_ringbuf_grow(th_ringbuf_t *buf, const size_t nadd) 327 int th_ringbuf_grow(th_ringbuf_t *buf, const size_t nadd)
328 { 328 {