Sun, 07 Feb 2021 20:37:20 +0100
copy list items when they are added to the list
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 | |
src/list.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/cx/linked_list.h Sun Feb 07 20:08:13 2021 +0100 1.2 +++ b/src/cx/linked_list.h Sun Feb 07 20:37:20 2021 +0100 1.3 @@ -36,7 +36,7 @@ 1.4 1.5 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_next, ptrdiff_t loc_prev, void *newnode); 1.6 1.7 -CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator); 1.8 +CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t itemsize); 1.9 1.10 extern cx_list_class cx_linked_list_class; 1.11
2.1 --- a/src/cx/list.h Sun Feb 07 20:08:13 2021 +0100 2.2 +++ b/src/cx/list.h Sun Feb 07 20:37:20 2021 +0100 2.3 @@ -37,6 +37,9 @@ 2.4 typedef struct { 2.5 CxAllocator allocator; 2.6 CxListComparator cmpfunc; 2.7 + size_t itemsize; 2.8 + size_t size; 2.9 + size_t capacity; 2.10 void *listdata; 2.11 } cx_list; 2.12 2.13 @@ -48,14 +51,11 @@ 2.14 2.15 typedef size_t (*cx_list_find)(cx_list *list, void *elem); 2.16 2.17 -typedef size_t (*cx_list_size)(cx_list *list); 2.18 - 2.19 typedef struct { 2.20 cx_list_add add; 2.21 cx_list_insert insert; 2.22 cx_list_remove remove; 2.23 cx_list_find find; 2.24 - cx_list_size size; 2.25 } cx_list_class; 2.26 2.27 struct cx_list_s { 2.28 @@ -73,6 +73,4 @@ 2.29 2.30 size_t cxListFind(CxList list, void *elem); 2.31 2.32 -size_t cxListSize(CxList list); 2.33 - 2.34 #endif /* UCX_LIST_H */
3.1 --- a/src/linked_list.c Sun Feb 07 20:08:13 2021 +0100 3.2 +++ b/src/linked_list.c Sun Feb 07 20:37:20 2021 +0100 3.3 @@ -27,6 +27,8 @@ 3.4 */ 3.5 3.6 #include "cx/linked_list.h" 3.7 +#include <stdint.h> 3.8 +#include <string.h> 3.9 3.10 /* LOW LEVEL LINKED LIST FUNCTIONS */ 3.11 3.12 @@ -77,40 +79,37 @@ 3.13 3.14 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ 3.15 3.16 -typedef struct cx_list_node_s cx_list_node; 3.17 - 3.18 -struct cx_list_node_s { 3.19 - cx_list_node *next; 3.20 - cx_list_node *prev; 3.21 - void *data; 3.22 -}; 3.23 - 3.24 typedef struct { 3.25 - cx_list_node *begin; 3.26 - cx_list_node *end; 3.27 - size_t size; 3.28 + void *begin; 3.29 + void *end; 3.30 } cx_linked_list; 3.31 3.32 int cx_ll_add(cx_list *list, void *elem) { 3.33 cx_linked_list *listdata = list->listdata; 3.34 CxAllocator allocator = list->allocator; 3.35 3.36 - struct cx_list_node_s *node = cxMalloc(allocator, sizeof(struct cx_list_node_s)); 3.37 + /* 3.38 + * Memory layout: 3.39 + * next : sizeof(void*) 3.40 + * prev : sizeof(void*) 3.41 + * data : itemsize 3.42 + */ 3.43 + void *node = cxMalloc(allocator,2*sizeof(void*)+list->itemsize); 3.44 if (node == NULL) 3.45 return 1; 3.46 3.47 - node->next = NULL; 3.48 - node->data = elem; 3.49 + memset(node, 0, sizeof(void*)); 3.50 + memcpy(node+2, elem, list->itemsize); 3.51 3.52 int ret = cx_linked_list_add( 3.53 - (void **) &listdata->begin, 3.54 - (void **) &listdata->end, 3.55 - offsetof(struct cx_list_node_s, next), 3.56 - offsetof(struct cx_list_node_s, prev), 3.57 + &listdata->begin, 3.58 + &listdata->end, 3.59 + 0, 3.60 + sizeof(void*), 3.61 node 3.62 ); 3.63 if (ret == 0) { 3.64 - listdata->size++; 3.65 + list->size++; 3.66 return 0; 3.67 } else { 3.68 return ret; 3.69 @@ -135,20 +134,15 @@ 3.70 return 0; 3.71 } 3.72 3.73 -size_t cx_ll_size(cx_list *list) { 3.74 - cx_linked_list *listdata = list->listdata; 3.75 - return listdata->size; 3.76 -} 3.77 3.78 cx_list_class cx_linked_list_class = { 3.79 cx_ll_add, 3.80 cx_ll_insert, 3.81 cx_ll_remove, 3.82 - cx_ll_find, 3.83 - cx_ll_size 3.84 + cx_ll_find 3.85 }; 3.86 3.87 -CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator) { 3.88 +CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t itemsize) { 3.89 CxList list = cxMalloc(allocator, sizeof(list)); 3.90 if (list == NULL) 3.91 return NULL; 3.92 @@ -156,6 +150,9 @@ 3.93 list->cl = &cx_linked_list_class; 3.94 list->data.allocator = allocator; 3.95 list->data.cmpfunc = comparator; 3.96 + list->data.size = 0; 3.97 + list->data.itemsize = itemsize; 3.98 + list->data.capacity = SIZE_MAX; 3.99 list->data.listdata = cxCalloc(allocator, 1, sizeof(cx_linked_list)); 3.100 if (list->data.listdata == NULL) { 3.101 cxFree(allocator, list);
4.1 --- a/src/list.c Sun Feb 07 20:08:13 2021 +0100 4.2 +++ b/src/list.c Sun Feb 07 20:37:20 2021 +0100 4.3 @@ -43,7 +43,3 @@ 4.4 size_t cxListFind(CxList list, void *elem) { 4.5 return list->cl->find(&list->data, elem); 4.6 } 4.7 - 4.8 -size_t cxListSize(CxList list) { 4.9 - return list->cl->size(&list->data); 4.10 -}