comparison th_datastruct.c @ 628:140e2272471c

Add some error checking in th_ringbuf_new().
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 17 Jan 2020 19:22:04 +0200
parents 964657ad0960
children b695eb769e30
comparison
equal deleted inserted replaced
627:964657ad0960 628:140e2272471c
322 /* 322 /*
323 * Ringbuffers 323 * Ringbuffers
324 */ 324 */
325 th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data)) 325 th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data))
326 { 326 {
327 th_ringbuf_t *res = th_malloc0(sizeof(th_ringbuf_t)); 327 th_ringbuf_t *res;
328 328
329 res->data = (char **) th_calloc(size, sizeof(char *)); 329 // Must have at least 2 elements
330 if (size < 2)
331 return NULL;
332
333 if ((res = th_malloc0(sizeof(th_ringbuf_t))) == NULL)
334 return NULL;
335
336 if ((res->data = (char **) th_calloc(size, sizeof(char *))) == NULL)
337 {
338 th_free(res);
339 return NULL;
340 }
341
330 res->size = size; 342 res->size = size;
331 res->n = 0; 343 res->n = 0;
332 res->deallocator = mdeallocator; 344 res->deallocator = mdeallocator;
333 345
334 return res; 346 return res;