#168 - add attributes and const

Sat, 09 Apr 2022 16:37:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 09 Apr 2022 16:37:43 +0200
changeset 508
8aea65ae1eaf
parent 507
2e8878770de0
child 509
0d3c6075f82c

#168 - add attributes and const

src/allocator.c file | annotate | diff | comparison | revisions
src/cx/allocator.h file | annotate | diff | comparison | revisions
src/cx/linked_list.h file | annotate | diff | comparison | revisions
src/cx/list.h file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/allocator.c	Sun Mar 06 13:57:36 2022 +0100
     1.2 +++ b/src/allocator.c	Sat Apr 09 16:37:43 2022 +0200
     1.3 @@ -31,22 +31,36 @@
     1.4  #include <stdlib.h>
     1.5  
     1.6  __attribute__((__malloc__, __alloc_size__(2)))
     1.7 -static void *cx_malloc_stdlib(__attribute__((__unused__)) void *d, size_t n) {
     1.8 +static void *cx_malloc_stdlib(
     1.9 +        __attribute__((__unused__)) void *d,
    1.10 +        size_t n
    1.11 +) {
    1.12      return malloc(n);
    1.13  }
    1.14  
    1.15  __attribute__((__warn_unused_result__, __alloc_size__(3)))
    1.16 -static void *cx_realloc_stdlib(__attribute__((__unused__)) void *d, void *mem, size_t n) {
    1.17 +static void *cx_realloc_stdlib(
    1.18 +        __attribute__((__unused__)) void *d,
    1.19 +        void *mem,
    1.20 +        size_t n
    1.21 +) {
    1.22      return realloc(mem, n);
    1.23  }
    1.24  
    1.25  __attribute__((__malloc__, __alloc_size__(2, 3)))
    1.26 -static void *cx_calloc_stdlib(__attribute__((__unused__)) void *d, size_t nelem, size_t n) {
    1.27 +static void *cx_calloc_stdlib(
    1.28 +        __attribute__((__unused__)) void *d,
    1.29 +        size_t nelem,
    1.30 +        size_t n
    1.31 +) {
    1.32      return calloc(nelem, n);
    1.33  }
    1.34  
    1.35  __attribute__((__nonnull__))
    1.36 -static void cx_free_stdlib(__attribute__((__unused__)) void *d, void *mem) {
    1.37 +static void cx_free_stdlib(
    1.38 +        __attribute__((__unused__)) void *d,
    1.39 +        void *mem
    1.40 +) {
    1.41      free(mem);
    1.42  }
    1.43  
    1.44 @@ -66,14 +80,14 @@
    1.45  /* IMPLEMENTATION OF HIGH LEVEL API */
    1.46  
    1.47  void *cxMalloc(
    1.48 -        CxAllocator *allocator,
    1.49 +        CxAllocator const *allocator,
    1.50          size_t n
    1.51  ) {
    1.52      return allocator->cl->malloc(allocator->data, n);
    1.53  }
    1.54  
    1.55  void *cxRealloc(
    1.56 -        CxAllocator *allocator,
    1.57 +        CxAllocator const *allocator,
    1.58          void *mem,
    1.59          size_t n
    1.60  ) {
    1.61 @@ -81,7 +95,7 @@
    1.62  }
    1.63  
    1.64  int cxReallocate(
    1.65 -        CxAllocator *allocator,
    1.66 +        CxAllocator const *allocator,
    1.67          void **mem,
    1.68          size_t n
    1.69  ) {
    1.70 @@ -95,7 +109,7 @@
    1.71  }
    1.72  
    1.73  void *cxCalloc(
    1.74 -        CxAllocator *allocator,
    1.75 +        CxAllocator const *allocator,
    1.76          size_t nelem,
    1.77          size_t n
    1.78  ) {
    1.79 @@ -103,7 +117,7 @@
    1.80  }
    1.81  
    1.82  void cxFree(
    1.83 -        CxAllocator *allocator,
    1.84 +        CxAllocator const *allocator,
    1.85          void *mem
    1.86  ) {
    1.87      allocator->cl->free(allocator->data, mem);
     2.1 --- a/src/cx/allocator.h	Sun Mar 06 13:57:36 2022 +0100
     2.2 +++ b/src/cx/allocator.h	Sat Apr 09 16:37:43 2022 +0200
     2.3 @@ -124,7 +124,7 @@
     2.4   * @return a pointer to the allocated memory
     2.5   */
     2.6  void *cxMalloc(
     2.7 -        CxAllocator *allocator,
     2.8 +        CxAllocator const *allocator,
     2.9          size_t n
    2.10  )
    2.11  __attribute__((__malloc__))
    2.12 @@ -143,7 +143,7 @@
    2.13   * @return a pointer to the re-allocated memory
    2.14   */
    2.15  void *cxRealloc(
    2.16 -        CxAllocator *allocator,
    2.17 +        CxAllocator const *allocator,
    2.18          void *mem,
    2.19          size_t n
    2.20  )
    2.21 @@ -166,7 +166,7 @@
    2.22   * @return zero on success, non-zero on failure
    2.23   */
    2.24  int cxReallocate(
    2.25 -        CxAllocator *allocator,
    2.26 +        CxAllocator const *allocator,
    2.27          void **mem,
    2.28          size_t n
    2.29  )
    2.30 @@ -181,7 +181,7 @@
    2.31   * @return a pointer to the allocated memory
    2.32   */
    2.33  void *cxCalloc(
    2.34 -        CxAllocator *allocator,
    2.35 +        CxAllocator const *allocator,
    2.36          size_t nelem,
    2.37          size_t n
    2.38  )
    2.39 @@ -197,7 +197,7 @@
    2.40   * @param mem a pointer to the block to free
    2.41   */
    2.42  void cxFree(
    2.43 -        CxAllocator *allocator,
    2.44 +        CxAllocator const *allocator,
    2.45          void *mem
    2.46  )
    2.47  __attribute__((__nonnull__));
     3.1 --- a/src/cx/linked_list.h	Sun Mar 06 13:57:36 2022 +0100
     3.2 +++ b/src/cx/linked_list.h	Sat Apr 09 16:37:43 2022 +0200
     3.3 @@ -56,7 +56,7 @@
     3.4   * @return the created list
     3.5   */
     3.6  CxList *cxLinkedListCreate(
     3.7 -        CxAllocator *allocator,
     3.8 +        CxAllocator const *allocator,
     3.9          CxListComparator comparator,
    3.10          size_t item_size
    3.11  ) __attribute__((__nonnull__));
    3.12 @@ -71,7 +71,7 @@
    3.13   * @return the created list
    3.14   */
    3.15  CxList *cxPointerLinkedListCreate(
    3.16 -        CxAllocator *allocator,
    3.17 +        CxAllocator const *allocator,
    3.18          CxListComparator comparator
    3.19  ) __attribute__((__nonnull__));
    3.20  
    3.21 @@ -86,7 +86,7 @@
    3.22   * @return the created list
    3.23   */
    3.24  CxList *cxLinkedListFromArray(
    3.25 -        CxAllocator *allocator,
    3.26 +        CxAllocator const *allocator,
    3.27          CxListComparator comparator,
    3.28          size_t item_size,
    3.29          size_t num_items,
    3.30 @@ -111,7 +111,7 @@
    3.31   * @return the node found at the specified index
    3.32   */
    3.33  void *cx_linked_list_at(
    3.34 -        void *start,
    3.35 +        void const *start,
    3.36          size_t start_index,
    3.37          ptrdiff_t loc_advance,
    3.38          size_t index
    3.39 @@ -150,7 +150,7 @@
    3.40   * @return a pointer to the first node
    3.41   */
    3.42  void *cx_linked_list_first(
    3.43 -        void *node,
    3.44 +        void const *node,
    3.45          ptrdiff_t loc_prev
    3.46  ) __attribute__((__nonnull__));
    3.47  
    3.48 @@ -166,7 +166,7 @@
    3.49   * @return a pointer to the last node
    3.50   */
    3.51  void *cx_linked_list_last(
    3.52 -        void *node,
    3.53 +        void const *node,
    3.54          ptrdiff_t loc_next
    3.55  ) __attribute__((__nonnull__));
    3.56  
    3.57 @@ -181,9 +181,9 @@
    3.58   * @return the node or \c NULL if \p node has no predecessor
    3.59   */
    3.60  void *cx_linked_list_prev(
    3.61 -        void *begin,
    3.62 +        void const *begin,
    3.63          ptrdiff_t loc_next,
    3.64 -        void *node
    3.65 +        void const *node
    3.66  ) __attribute__((__nonnull__));
    3.67  
    3.68  /**
     4.1 --- a/src/cx/list.h	Sun Mar 06 13:57:36 2022 +0100
     4.2 +++ b/src/cx/list.h	Sat Apr 09 16:37:43 2022 +0200
     4.3 @@ -69,7 +69,7 @@
     4.4      /**
     4.5       * The allocator to use.
     4.6       */
     4.7 -    CxAllocator *allocator;
     4.8 +    CxAllocator const *allocator;
     4.9      /**
    4.10       * A mandatory destructor for the list structure.
    4.11       */
    4.12 @@ -202,6 +202,7 @@
    4.13   * @param content_autofree a flag indicating, if the list allocator shall free an element,
    4.14   * if the content destructor did not do that or no content destructor exists
    4.15   */
    4.16 +__attribute__((__nonnull__(1)))
    4.17  static inline void cxListMemoryMgmt(
    4.18          CxList *list,
    4.19          cx_destructor_func list_destructor,
    4.20 @@ -222,6 +223,7 @@
    4.21   * @param elem a pointer to the element to add
    4.22   * @return zero on success, non-zero on memory allocation failure
    4.23   */
    4.24 +__attribute__((__nonnull__))
    4.25  static inline int cxListAdd(
    4.26          CxList *list,
    4.27          void const *elem
    4.28 @@ -242,6 +244,7 @@
    4.29   * @see cxListInsertAfter()
    4.30   * @see cxListInsertBefore()
    4.31   */
    4.32 +__attribute__((__nonnull__))
    4.33  static inline int cxListInsert(
    4.34          CxList *list,
    4.35          size_t index,
    4.36 @@ -265,6 +268,7 @@
    4.37   * @see cxListInsert()
    4.38   * @see cxListInsertBefore()
    4.39   */
    4.40 +__attribute__((__nonnull__))
    4.41  static inline int cxListInsertAfter(
    4.42          CxIterator *iter,
    4.43          void const *elem
    4.44 @@ -287,6 +291,7 @@
    4.45   * @see cxListInsert()
    4.46   * @see cxListInsertAfter()
    4.47   */
    4.48 +__attribute__((__nonnull__))
    4.49  static inline int cxListInsertBefore(
    4.50          CxIterator *iter,
    4.51          void const *elem
    4.52 @@ -300,6 +305,7 @@
    4.53   * @param index the index of the element
    4.54   * @return zero on success, non-zero if the index is out of bounds
    4.55   */
    4.56 +__attribute__((__nonnull__))
    4.57  static inline int cxListRemove(
    4.58          CxList *list,
    4.59          size_t index
    4.60 @@ -314,6 +320,7 @@
    4.61   * @param index the index of the element
    4.62   * @return a pointer to the element or \c NULL if the index is out of bounds
    4.63   */
    4.64 +__attribute__((__nonnull__))
    4.65  static inline void *cxListAt(
    4.66          CxList *list,
    4.67          size_t index
    4.68 @@ -332,6 +339,7 @@
    4.69   * @param index the index where the iterator shall point at
    4.70   * @return a new iterator
    4.71   */
    4.72 +__attribute__((__nonnull__, __warn_unused_result__))
    4.73  static inline CxIterator cxListIterator(
    4.74          CxList *list,
    4.75          size_t index
    4.76 @@ -349,6 +357,7 @@
    4.77   * @param list the list
    4.78   * @return a new iterator
    4.79   */
    4.80 +__attribute__((__nonnull__, __warn_unused_result__))
    4.81  static inline CxIterator cxListBegin(CxList *list) {
    4.82      return list->cl->iterator(list, 0);
    4.83  }
    4.84 @@ -362,6 +371,7 @@
    4.85   * @param elem the element to find
    4.86   * @return the index of the element or \c (size+1) if the element is not found
    4.87   */
    4.88 +__attribute__((__nonnull__))
    4.89  static inline size_t cxListFind(
    4.90          CxList *list,
    4.91          void const *elem
    4.92 @@ -376,6 +386,7 @@
    4.93   *
    4.94   * @param list the list
    4.95   */
    4.96 +__attribute__((__nonnull__))
    4.97  static inline void cxListSort(CxList *list) {
    4.98      list->cl->sort(list);
    4.99  }
   4.100 @@ -385,6 +396,7 @@
   4.101   *
   4.102   * @param list the list
   4.103   */
   4.104 +__attribute__((__nonnull__))
   4.105  static inline void cxListReverse(CxList *list) {
   4.106      list->cl->reverse(list);
   4.107  }
   4.108 @@ -398,6 +410,7 @@
   4.109   * @param other the list to compare to
   4.110   * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger
   4.111   */
   4.112 +__attribute__((__nonnull__))
   4.113  static inline int cxListCompare(
   4.114          CxList *list,
   4.115          CxList *other
   4.116 @@ -415,7 +428,7 @@
   4.117   * This function itself is a destructor function for the CxList.
   4.118   *
   4.119   * @param list the list which contents shall be destroyed
   4.120 - * @return \p list if the list structure has been free'd during the process
   4.121 + * @return \p list if the list structure has not been free'd during the process
   4.122   */
   4.123  __attribute__((__nonnull__))
   4.124  CxList *cxListDestroy(CxList *list);
     5.1 --- a/src/linked_list.c	Sun Mar 06 13:57:36 2022 +0100
     5.2 +++ b/src/linked_list.c	Sat Apr 09 16:37:43 2022 +0200
     5.3 @@ -40,7 +40,7 @@
     5.4  #define ll_data(node) (follow_ptr?CX_LL_PTR(node, loc_data):(((char*)node)+loc_data))
     5.5  
     5.6  void *cx_linked_list_at(
     5.7 -        void *start,
     5.8 +        void const *start,
     5.9          size_t start_index,
    5.10          ptrdiff_t loc_advance,
    5.11          size_t index
    5.12 @@ -48,12 +48,12 @@
    5.13      assert(start != NULL);
    5.14      assert(loc_advance >= 0);
    5.15      size_t i = start_index;
    5.16 -    void *cur = start;
    5.17 +    void const *cur = start;
    5.18      while (i != index && cur != NULL) {
    5.19          cur = ll_advance(cur);
    5.20          i < index ? i++ : i--;
    5.21      }
    5.22 -    return cur;
    5.23 +    return (void *) cur;
    5.24  }
    5.25  
    5.26  size_t cx_linked_list_find(
    5.27 @@ -83,42 +83,42 @@
    5.28  }
    5.29  
    5.30  void *cx_linked_list_first(
    5.31 -        void *node,
    5.32 +        void const *node,
    5.33          ptrdiff_t loc_prev
    5.34  ) {
    5.35      return cx_linked_list_last(node, loc_prev);
    5.36  }
    5.37  
    5.38  void *cx_linked_list_last(
    5.39 -        void *node,
    5.40 +        void const *node,
    5.41          ptrdiff_t loc_next
    5.42  ) {
    5.43      assert(node != NULL);
    5.44      assert(loc_next >= 0);
    5.45  
    5.46 -    void *cur = node;
    5.47 -    void *last;
    5.48 +    void const *cur = node;
    5.49 +    void const *last;
    5.50      do {
    5.51          last = cur;
    5.52      } while ((cur = ll_next(cur)) != NULL);
    5.53  
    5.54 -    return last;
    5.55 +    return (void *) last;
    5.56  }
    5.57  
    5.58  void *cx_linked_list_prev(
    5.59 -        void *begin,
    5.60 +        void const *begin,
    5.61          ptrdiff_t loc_next,
    5.62 -        void *node
    5.63 +        void const *node
    5.64  ) {
    5.65      assert(begin != NULL);
    5.66      assert(node != NULL);
    5.67      assert(loc_next >= 0);
    5.68      if (begin == node) return NULL;
    5.69 -    void *cur = begin;
    5.70 -    void *next;
    5.71 +    void const *cur = begin;
    5.72 +    void const *next;
    5.73      while (1) {
    5.74          next = ll_next(cur);
    5.75 -        if (next == node) return cur;
    5.76 +        if (next == node) return (void *) cur;
    5.77          cur = next;
    5.78      }
    5.79  }
    5.80 @@ -294,6 +294,7 @@
    5.81      const size_t sbo_len = 1024;
    5.82      void *sbo[sbo_len];
    5.83      void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo;
    5.84 +    if (sorted == NULL) abort();
    5.85      void *rc, *lc;
    5.86  
    5.87      lc = ls;
    5.88 @@ -772,7 +773,7 @@
    5.89  }
    5.90  
    5.91  CxList *cxLinkedListCreate(
    5.92 -        CxAllocator *allocator,
    5.93 +        CxAllocator const *allocator,
    5.94          CxListComparator comparator,
    5.95          size_t item_size
    5.96  ) {
    5.97 @@ -791,7 +792,7 @@
    5.98  }
    5.99  
   5.100  CxList *cxPointerLinkedListCreate(
   5.101 -        CxAllocator *allocator,
   5.102 +        CxAllocator const *allocator,
   5.103          CxListComparator comparator
   5.104  ) {
   5.105      cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   5.106 @@ -809,7 +810,7 @@
   5.107  }
   5.108  
   5.109  CxList *cxLinkedListFromArray(
   5.110 -        CxAllocator *allocator,
   5.111 +        CxAllocator const *allocator,
   5.112          CxListComparator comparator,
   5.113          size_t item_size,
   5.114          size_t num_items,

mercurial