#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
--- a/src/allocator.c	Sun Mar 06 13:57:36 2022 +0100
+++ b/src/allocator.c	Sat Apr 09 16:37:43 2022 +0200
@@ -31,22 +31,36 @@
 #include <stdlib.h>
 
 __attribute__((__malloc__, __alloc_size__(2)))
-static void *cx_malloc_stdlib(__attribute__((__unused__)) void *d, size_t n) {
+static void *cx_malloc_stdlib(
+        __attribute__((__unused__)) void *d,
+        size_t n
+) {
     return malloc(n);
 }
 
 __attribute__((__warn_unused_result__, __alloc_size__(3)))
-static void *cx_realloc_stdlib(__attribute__((__unused__)) void *d, void *mem, size_t n) {
+static void *cx_realloc_stdlib(
+        __attribute__((__unused__)) void *d,
+        void *mem,
+        size_t n
+) {
     return realloc(mem, n);
 }
 
 __attribute__((__malloc__, __alloc_size__(2, 3)))
-static void *cx_calloc_stdlib(__attribute__((__unused__)) void *d, size_t nelem, size_t n) {
+static void *cx_calloc_stdlib(
+        __attribute__((__unused__)) void *d,
+        size_t nelem,
+        size_t n
+) {
     return calloc(nelem, n);
 }
 
 __attribute__((__nonnull__))
-static void cx_free_stdlib(__attribute__((__unused__)) void *d, void *mem) {
+static void cx_free_stdlib(
+        __attribute__((__unused__)) void *d,
+        void *mem
+) {
     free(mem);
 }
 
@@ -66,14 +80,14 @@
 /* IMPLEMENTATION OF HIGH LEVEL API */
 
 void *cxMalloc(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         size_t n
 ) {
     return allocator->cl->malloc(allocator->data, n);
 }
 
 void *cxRealloc(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         void *mem,
         size_t n
 ) {
@@ -81,7 +95,7 @@
 }
 
 int cxReallocate(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         void **mem,
         size_t n
 ) {
@@ -95,7 +109,7 @@
 }
 
 void *cxCalloc(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         size_t nelem,
         size_t n
 ) {
@@ -103,7 +117,7 @@
 }
 
 void cxFree(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         void *mem
 ) {
     allocator->cl->free(allocator->data, mem);
--- a/src/cx/allocator.h	Sun Mar 06 13:57:36 2022 +0100
+++ b/src/cx/allocator.h	Sat Apr 09 16:37:43 2022 +0200
@@ -124,7 +124,7 @@
  * @return a pointer to the allocated memory
  */
 void *cxMalloc(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         size_t n
 )
 __attribute__((__malloc__))
@@ -143,7 +143,7 @@
  * @return a pointer to the re-allocated memory
  */
 void *cxRealloc(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         void *mem,
         size_t n
 )
@@ -166,7 +166,7 @@
  * @return zero on success, non-zero on failure
  */
 int cxReallocate(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         void **mem,
         size_t n
 )
@@ -181,7 +181,7 @@
  * @return a pointer to the allocated memory
  */
 void *cxCalloc(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         size_t nelem,
         size_t n
 )
@@ -197,7 +197,7 @@
  * @param mem a pointer to the block to free
  */
 void cxFree(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         void *mem
 )
 __attribute__((__nonnull__));
--- a/src/cx/linked_list.h	Sun Mar 06 13:57:36 2022 +0100
+++ b/src/cx/linked_list.h	Sat Apr 09 16:37:43 2022 +0200
@@ -56,7 +56,7 @@
  * @return the created list
  */
 CxList *cxLinkedListCreate(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         CxListComparator comparator,
         size_t item_size
 ) __attribute__((__nonnull__));
@@ -71,7 +71,7 @@
  * @return the created list
  */
 CxList *cxPointerLinkedListCreate(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         CxListComparator comparator
 ) __attribute__((__nonnull__));
 
@@ -86,7 +86,7 @@
  * @return the created list
  */
 CxList *cxLinkedListFromArray(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         CxListComparator comparator,
         size_t item_size,
         size_t num_items,
@@ -111,7 +111,7 @@
  * @return the node found at the specified index
  */
 void *cx_linked_list_at(
-        void *start,
+        void const *start,
         size_t start_index,
         ptrdiff_t loc_advance,
         size_t index
@@ -150,7 +150,7 @@
  * @return a pointer to the first node
  */
 void *cx_linked_list_first(
-        void *node,
+        void const *node,
         ptrdiff_t loc_prev
 ) __attribute__((__nonnull__));
 
@@ -166,7 +166,7 @@
  * @return a pointer to the last node
  */
 void *cx_linked_list_last(
-        void *node,
+        void const *node,
         ptrdiff_t loc_next
 ) __attribute__((__nonnull__));
 
@@ -181,9 +181,9 @@
  * @return the node or \c NULL if \p node has no predecessor
  */
 void *cx_linked_list_prev(
-        void *begin,
+        void const *begin,
         ptrdiff_t loc_next,
-        void *node
+        void const *node
 ) __attribute__((__nonnull__));
 
 /**
--- a/src/cx/list.h	Sun Mar 06 13:57:36 2022 +0100
+++ b/src/cx/list.h	Sat Apr 09 16:37:43 2022 +0200
@@ -69,7 +69,7 @@
     /**
      * The allocator to use.
      */
-    CxAllocator *allocator;
+    CxAllocator const *allocator;
     /**
      * A mandatory destructor for the list structure.
      */
@@ -202,6 +202,7 @@
  * @param content_autofree a flag indicating, if the list allocator shall free an element,
  * if the content destructor did not do that or no content destructor exists
  */
+__attribute__((__nonnull__(1)))
 static inline void cxListMemoryMgmt(
         CxList *list,
         cx_destructor_func list_destructor,
@@ -222,6 +223,7 @@
  * @param elem a pointer to the element to add
  * @return zero on success, non-zero on memory allocation failure
  */
+__attribute__((__nonnull__))
 static inline int cxListAdd(
         CxList *list,
         void const *elem
@@ -242,6 +244,7 @@
  * @see cxListInsertAfter()
  * @see cxListInsertBefore()
  */
+__attribute__((__nonnull__))
 static inline int cxListInsert(
         CxList *list,
         size_t index,
@@ -265,6 +268,7 @@
  * @see cxListInsert()
  * @see cxListInsertBefore()
  */
+__attribute__((__nonnull__))
 static inline int cxListInsertAfter(
         CxIterator *iter,
         void const *elem
@@ -287,6 +291,7 @@
  * @see cxListInsert()
  * @see cxListInsertAfter()
  */
+__attribute__((__nonnull__))
 static inline int cxListInsertBefore(
         CxIterator *iter,
         void const *elem
@@ -300,6 +305,7 @@
  * @param index the index of the element
  * @return zero on success, non-zero if the index is out of bounds
  */
+__attribute__((__nonnull__))
 static inline int cxListRemove(
         CxList *list,
         size_t index
@@ -314,6 +320,7 @@
  * @param index the index of the element
  * @return a pointer to the element or \c NULL if the index is out of bounds
  */
+__attribute__((__nonnull__))
 static inline void *cxListAt(
         CxList *list,
         size_t index
@@ -332,6 +339,7 @@
  * @param index the index where the iterator shall point at
  * @return a new iterator
  */
+__attribute__((__nonnull__, __warn_unused_result__))
 static inline CxIterator cxListIterator(
         CxList *list,
         size_t index
@@ -349,6 +357,7 @@
  * @param list the list
  * @return a new iterator
  */
+__attribute__((__nonnull__, __warn_unused_result__))
 static inline CxIterator cxListBegin(CxList *list) {
     return list->cl->iterator(list, 0);
 }
@@ -362,6 +371,7 @@
  * @param elem the element to find
  * @return the index of the element or \c (size+1) if the element is not found
  */
+__attribute__((__nonnull__))
 static inline size_t cxListFind(
         CxList *list,
         void const *elem
@@ -376,6 +386,7 @@
  *
  * @param list the list
  */
+__attribute__((__nonnull__))
 static inline void cxListSort(CxList *list) {
     list->cl->sort(list);
 }
@@ -385,6 +396,7 @@
  *
  * @param list the list
  */
+__attribute__((__nonnull__))
 static inline void cxListReverse(CxList *list) {
     list->cl->reverse(list);
 }
@@ -398,6 +410,7 @@
  * @param other the list to compare to
  * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger
  */
+__attribute__((__nonnull__))
 static inline int cxListCompare(
         CxList *list,
         CxList *other
@@ -415,7 +428,7 @@
  * This function itself is a destructor function for the CxList.
  *
  * @param list the list which contents shall be destroyed
- * @return \p list if the list structure has been free'd during the process
+ * @return \p list if the list structure has not been free'd during the process
  */
 __attribute__((__nonnull__))
 CxList *cxListDestroy(CxList *list);
--- a/src/linked_list.c	Sun Mar 06 13:57:36 2022 +0100
+++ b/src/linked_list.c	Sat Apr 09 16:37:43 2022 +0200
@@ -40,7 +40,7 @@
 #define ll_data(node) (follow_ptr?CX_LL_PTR(node, loc_data):(((char*)node)+loc_data))
 
 void *cx_linked_list_at(
-        void *start,
+        void const *start,
         size_t start_index,
         ptrdiff_t loc_advance,
         size_t index
@@ -48,12 +48,12 @@
     assert(start != NULL);
     assert(loc_advance >= 0);
     size_t i = start_index;
-    void *cur = start;
+    void const *cur = start;
     while (i != index && cur != NULL) {
         cur = ll_advance(cur);
         i < index ? i++ : i--;
     }
-    return cur;
+    return (void *) cur;
 }
 
 size_t cx_linked_list_find(
@@ -83,42 +83,42 @@
 }
 
 void *cx_linked_list_first(
-        void *node,
+        void const *node,
         ptrdiff_t loc_prev
 ) {
     return cx_linked_list_last(node, loc_prev);
 }
 
 void *cx_linked_list_last(
-        void *node,
+        void const *node,
         ptrdiff_t loc_next
 ) {
     assert(node != NULL);
     assert(loc_next >= 0);
 
-    void *cur = node;
-    void *last;
+    void const *cur = node;
+    void const *last;
     do {
         last = cur;
     } while ((cur = ll_next(cur)) != NULL);
 
-    return last;
+    return (void *) last;
 }
 
 void *cx_linked_list_prev(
-        void *begin,
+        void const *begin,
         ptrdiff_t loc_next,
-        void *node
+        void const *node
 ) {
     assert(begin != NULL);
     assert(node != NULL);
     assert(loc_next >= 0);
     if (begin == node) return NULL;
-    void *cur = begin;
-    void *next;
+    void const *cur = begin;
+    void const *next;
     while (1) {
         next = ll_next(cur);
-        if (next == node) return cur;
+        if (next == node) return (void *) cur;
         cur = next;
     }
 }
@@ -294,6 +294,7 @@
     const size_t sbo_len = 1024;
     void *sbo[sbo_len];
     void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo;
+    if (sorted == NULL) abort();
     void *rc, *lc;
 
     lc = ls;
@@ -772,7 +773,7 @@
 }
 
 CxList *cxLinkedListCreate(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         CxListComparator comparator,
         size_t item_size
 ) {
@@ -791,7 +792,7 @@
 }
 
 CxList *cxPointerLinkedListCreate(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         CxListComparator comparator
 ) {
     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
@@ -809,7 +810,7 @@
 }
 
 CxList *cxLinkedListFromArray(
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         CxListComparator comparator,
         size_t item_size,
         size_t num_items,

mercurial