Mercurial > hg > th-libs
comparison th_datastruct.c @ 752:3710c117b7c7
Add some NULL pointer checks.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 11 Jan 2023 12:00:52 +0200 |
parents | 3091fd1987e9 |
children | 64cb2b1777a9 |
comparison
equal
deleted
inserted
replaced
751:3091fd1987e9 | 752:3710c117b7c7 |
---|---|
340 } | 340 } |
341 | 341 |
342 | 342 |
343 void th_ringbuf_free(th_ringbuf_t *buf) | 343 void th_ringbuf_free(th_ringbuf_t *buf) |
344 { | 344 { |
345 for (size_t i = 0; i < buf->size; i++) | 345 if (buf != NULL) |
346 { | 346 { |
347 buf->deallocator(buf->data[i]); | 347 for (size_t i = 0; i < buf->size; i++) |
348 } | 348 { |
349 | 349 buf->deallocator(buf->data[i]); |
350 th_free(buf->data); | 350 } |
351 | 351 |
352 if (buf->is_allocated) | 352 th_free(buf->data); |
353 th_free(buf); | 353 |
354 if (buf->is_allocated) | |
355 th_free(buf); | |
356 } | |
354 } | 357 } |
355 | 358 |
356 | 359 |
357 void th_ringbuf_add(th_ringbuf_t *buf, void *ptr) | 360 void th_ringbuf_add(th_ringbuf_t *buf, void *ptr) |
358 { | 361 { |
397 } | 400 } |
398 | 401 |
399 | 402 |
400 void th_growbuf_free(th_growbuf_t *buf) | 403 void th_growbuf_free(th_growbuf_t *buf) |
401 { | 404 { |
402 th_free(buf->data); | 405 if (buf != NULL) |
403 | 406 { |
404 if (buf->is_allocated) | 407 th_free(buf->data); |
405 th_free(buf); | 408 |
409 if (buf->is_allocated) | |
410 th_free(buf); | |
411 } | |
406 } | 412 } |
407 | 413 |
408 | 414 |
409 bool th_growbuf_grow(th_growbuf_t *buf, const size_t amount) | 415 bool th_growbuf_grow(th_growbuf_t *buf, const size_t amount) |
410 { | 416 { |
591 } | 597 } |
592 | 598 |
593 | 599 |
594 int th_ptrlist_init(th_ptrlist_t *list, const size_t ninitial, const size_t ngrow) | 600 int th_ptrlist_init(th_ptrlist_t *list, const size_t ninitial, const size_t ngrow) |
595 { | 601 { |
602 if (list == NULL) | |
603 return THERR_NULLPTR; | |
604 | |
596 memset(list, 0, sizeof(*list)); | 605 memset(list, 0, sizeof(*list)); |
597 | 606 |
598 if (ngrow == 0) | 607 if (ngrow == 0) |
599 return THERR_INVALID_ARGS; | 608 return THERR_INVALID_ARGS; |
600 | 609 |
611 | 620 |
612 int th_ptrlist_new(th_ptrlist_t **list, const size_t ninitial, const size_t ngrow) | 621 int th_ptrlist_new(th_ptrlist_t **list, const size_t ninitial, const size_t ngrow) |
613 { | 622 { |
614 int res; | 623 int res; |
615 | 624 |
625 if (list == NULL) | |
626 return THERR_NULLPTR; | |
627 | |
616 if ((*list = th_malloc0(sizeof(th_ptrlist_t))) == NULL) | 628 if ((*list = th_malloc0(sizeof(th_ptrlist_t))) == NULL) |
617 return THERR_MALLOC; | 629 return THERR_MALLOC; |
618 | 630 |
619 if ((res = th_ptrlist_init(*list, ninitial, ngrow)) != THERR_OK) | 631 if ((res = th_ptrlist_init(*list, ninitial, ngrow)) != THERR_OK) |
620 { | 632 { |
628 } | 640 } |
629 | 641 |
630 | 642 |
631 int th_ptrlist_append(th_ptrlist_t *list, void *data) | 643 int th_ptrlist_append(th_ptrlist_t *list, void *data) |
632 { | 644 { |
645 if (list == NULL) | |
646 return THERR_NULLPTR; | |
647 | |
633 if (list->nitems + 1 >= list->nallocated) | 648 if (list->nitems + 1 >= list->nallocated) |
634 { | 649 { |
635 list->nallocated += list->ngrow; | 650 list->nallocated += list->ngrow; |
636 if ((list->items = th_realloc(list->items, list->nallocated * sizeof(void *))) == NULL) | 651 if ((list->items = th_realloc(list->items, list->nallocated * sizeof(void *))) == NULL) |
637 return THERR_MALLOC; | 652 return THERR_MALLOC; |
643 } | 658 } |
644 | 659 |
645 | 660 |
646 void th_ptrlist_free(th_ptrlist_t *list, void (*mdeallocator)(void *data)) | 661 void th_ptrlist_free(th_ptrlist_t *list, void (*mdeallocator)(void *data)) |
647 { | 662 { |
648 if (mdeallocator != NULL) | 663 if (list != NULL) |
649 { | 664 { |
650 for (size_t i = 0; i < list->nitems; i++) | 665 if (mdeallocator != NULL) |
651 mdeallocator(list->items[i]); | 666 { |
652 } | 667 for (size_t i = 0; i < list->nitems; i++) |
653 | 668 mdeallocator(list->items[i]); |
654 th_free(list->items); | 669 } |
655 | 670 |
656 if (list->is_allocated) | 671 th_free(list->items); |
657 th_free(list); | 672 |
658 } | 673 if (list->is_allocated) |
674 th_free(list); | |
675 } | |
676 } |