allow NULL for allocator and comparator

Tue, 28 Mar 2023 21:00:33 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Mar 2023 21:00:33 +0200
changeset 670
4ad8ea3aee49
parent 669
dce9b8450656
child 671
d7a67375a7ac

allow NULL for allocator and comparator

src/array_list.c file | annotate | diff | comparison | revisions
src/cx/array_list.h file | annotate | diff | comparison | revisions
src/cx/linked_list.h file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/array_list.c	Tue Mar 28 19:13:33 2023 +0200
     1.2 +++ b/src/array_list.c	Tue Mar 28 21:00:33 2023 +0200
     1.3 @@ -498,12 +498,16 @@
     1.4          cx_arl_iterator,
     1.5  };
     1.6  
     1.7 -static CxList *cx_array_list_create(
     1.8 +CxList *cxArrayListCreate(
     1.9          CxAllocator const *allocator,
    1.10          CxListComparator comparator,
    1.11          size_t item_size,
    1.12          size_t initial_capacity
    1.13  ) {
    1.14 +    if (allocator == NULL) {
    1.15 +        allocator = cxDefaultAllocator;
    1.16 +    }
    1.17 +
    1.18      cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
    1.19      if (list == NULL) return NULL;
    1.20  
    1.21 @@ -530,21 +534,3 @@
    1.22  
    1.23      return (CxList *) list;
    1.24  }
    1.25 -
    1.26 -CxList *cxArrayListCreate(
    1.27 -        CxAllocator const *allocator,
    1.28 -        CxListComparator comparator,
    1.29 -        size_t item_size,
    1.30 -        size_t initial_capacity
    1.31 -) {
    1.32 -    return cx_array_list_create(allocator, comparator,
    1.33 -                                item_size, initial_capacity);
    1.34 -}
    1.35 -
    1.36 -CxList *cxArrayListCreateSimple(
    1.37 -        size_t item_size,
    1.38 -        size_t initial_capacity
    1.39 -) {
    1.40 -    return cx_array_list_create(cxDefaultAllocator, NULL,
    1.41 -                                item_size, initial_capacity);
    1.42 -}
     2.1 --- a/src/cx/array_list.h	Tue Mar 28 19:13:33 2023 +0200
     2.2 +++ b/src/cx/array_list.h	Tue Mar 28 21:00:33 2023 +0200
     2.3 @@ -156,7 +156,9 @@
     2.4   * cxListStorePointers() was called immediately after creation.
     2.5   *
     2.6   * @param allocator the allocator for allocating the list memory
     2.7 + * (if \c NULL the cxDefaultAllocator will be used)
     2.8   * @param comparator the comparator for the elements
     2.9 + * (if \c NULL sort and find functions will not work)
    2.10   * @param item_size the size of each element in bytes
    2.11   * @param initial_capacity the initial number of elements the array can store
    2.12   * @return the created list
    2.13 @@ -166,7 +168,7 @@
    2.14          CxListComparator comparator,
    2.15          size_t item_size,
    2.16          size_t initial_capacity
    2.17 -) __attribute__((__nonnull__));
    2.18 +);
    2.19  
    2.20  /**
    2.21   * Allocates an array list for storing elements with \p item_size bytes each.
    2.22 @@ -182,11 +184,8 @@
    2.23   * @param initial_capacity the initial number of elements the array can store
    2.24   * @return the created list
    2.25   */
    2.26 -CxList *cxArrayListCreateSimple(
    2.27 -        size_t item_size,
    2.28 -        size_t initial_capacity
    2.29 -);
    2.30 -
    2.31 +#define cxArrayListCreateSimple(item_size, initial_capacity) \
    2.32 +    cxArrayListCreate(NULL, NULL, item_size, initial_capacity)
    2.33  
    2.34  #ifdef __cplusplus
    2.35  } // extern "C"
     3.1 --- a/src/cx/linked_list.h	Tue Mar 28 19:13:33 2023 +0200
     3.2 +++ b/src/cx/linked_list.h	Tue Mar 28 21:00:33 2023 +0200
     3.3 @@ -58,7 +58,9 @@
     3.4   * cxListStorePointers() was called immediately after creation.
     3.5   *
     3.6   * @param allocator the allocator for allocating the list nodes
     3.7 + * (if \c NULL the cxDefaultAllocator will be used)
     3.8   * @param comparator the comparator for the elements
     3.9 + * (if \c NULL sort and find functions will not work)
    3.10   * @param item_size the size of each element in bytes
    3.11   * @return the created list
    3.12   */
    3.13 @@ -66,7 +68,7 @@
    3.14          CxAllocator const *allocator,
    3.15          CxListComparator comparator,
    3.16          size_t item_size
    3.17 -) __attribute__((__nonnull__));
    3.18 +);
    3.19  
    3.20  /**
    3.21   * Allocates a linked list for storing elements with \p item_size bytes each.
    3.22 @@ -81,7 +83,8 @@
    3.23   * @param item_size the size of each element in bytes
    3.24   * @return the created list
    3.25   */
    3.26 -CxList *cxLinkedListCreateSimple(size_t item_size);
    3.27 +#define cxLinkedListCreateSimple(item_size) \
    3.28 +    cxLinkedListCreate(NULL, NULL, item_size)
    3.29  
    3.30  /**
    3.31   * Finds the node at a certain index.
     4.1 --- a/src/linked_list.c	Tue Mar 28 19:13:33 2023 +0200
     4.2 +++ b/src/linked_list.c	Tue Mar 28 21:00:33 2023 +0200
     4.3 @@ -925,11 +925,15 @@
     4.4          cx_ll_iterator,
     4.5  };
     4.6  
     4.7 -static CxList *cx_linked_list_create(
     4.8 +CxList *cxLinkedListCreate(
     4.9          CxAllocator const *allocator,
    4.10          CxListComparator comparator,
    4.11          size_t item_size
    4.12  ) {
    4.13 +    if (allocator == NULL) {
    4.14 +        allocator = cxDefaultAllocator;
    4.15 +    }
    4.16 +
    4.17      cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
    4.18      if (list == NULL) return NULL;
    4.19  
    4.20 @@ -946,15 +950,3 @@
    4.21  
    4.22      return (CxList *) list;
    4.23  }
    4.24 -
    4.25 -CxList *cxLinkedListCreate(
    4.26 -        CxAllocator const *allocator,
    4.27 -        CxListComparator comparator,
    4.28 -        size_t item_size
    4.29 -) {
    4.30 -    return cx_linked_list_create(allocator, comparator, item_size);
    4.31 -}
    4.32 -
    4.33 -CxList *cxLinkedListCreateSimple(size_t item_size) {
    4.34 -    return cx_linked_list_create(cxDefaultAllocator, NULL, item_size);
    4.35 -}

mercurial