1.1 --- a/src/linked_list.c Sun Feb 07 20:08:13 2021 +0100 1.2 +++ b/src/linked_list.c Sun Feb 07 20:37:20 2021 +0100 1.3 @@ -27,6 +27,8 @@ 1.4 */ 1.5 1.6 #include "cx/linked_list.h" 1.7 +#include <stdint.h> 1.8 +#include <string.h> 1.9 1.10 /* LOW LEVEL LINKED LIST FUNCTIONS */ 1.11 1.12 @@ -77,40 +79,37 @@ 1.13 1.14 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ 1.15 1.16 -typedef struct cx_list_node_s cx_list_node; 1.17 - 1.18 -struct cx_list_node_s { 1.19 - cx_list_node *next; 1.20 - cx_list_node *prev; 1.21 - void *data; 1.22 -}; 1.23 - 1.24 typedef struct { 1.25 - cx_list_node *begin; 1.26 - cx_list_node *end; 1.27 - size_t size; 1.28 + void *begin; 1.29 + void *end; 1.30 } cx_linked_list; 1.31 1.32 int cx_ll_add(cx_list *list, void *elem) { 1.33 cx_linked_list *listdata = list->listdata; 1.34 CxAllocator allocator = list->allocator; 1.35 1.36 - struct cx_list_node_s *node = cxMalloc(allocator, sizeof(struct cx_list_node_s)); 1.37 + /* 1.38 + * Memory layout: 1.39 + * next : sizeof(void*) 1.40 + * prev : sizeof(void*) 1.41 + * data : itemsize 1.42 + */ 1.43 + void *node = cxMalloc(allocator,2*sizeof(void*)+list->itemsize); 1.44 if (node == NULL) 1.45 return 1; 1.46 1.47 - node->next = NULL; 1.48 - node->data = elem; 1.49 + memset(node, 0, sizeof(void*)); 1.50 + memcpy(node+2, elem, list->itemsize); 1.51 1.52 int ret = cx_linked_list_add( 1.53 - (void **) &listdata->begin, 1.54 - (void **) &listdata->end, 1.55 - offsetof(struct cx_list_node_s, next), 1.56 - offsetof(struct cx_list_node_s, prev), 1.57 + &listdata->begin, 1.58 + &listdata->end, 1.59 + 0, 1.60 + sizeof(void*), 1.61 node 1.62 ); 1.63 if (ret == 0) { 1.64 - listdata->size++; 1.65 + list->size++; 1.66 return 0; 1.67 } else { 1.68 return ret; 1.69 @@ -135,20 +134,15 @@ 1.70 return 0; 1.71 } 1.72 1.73 -size_t cx_ll_size(cx_list *list) { 1.74 - cx_linked_list *listdata = list->listdata; 1.75 - return listdata->size; 1.76 -} 1.77 1.78 cx_list_class cx_linked_list_class = { 1.79 cx_ll_add, 1.80 cx_ll_insert, 1.81 cx_ll_remove, 1.82 - cx_ll_find, 1.83 - cx_ll_size 1.84 + cx_ll_find 1.85 }; 1.86 1.87 -CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator) { 1.88 +CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t itemsize) { 1.89 CxList list = cxMalloc(allocator, sizeof(list)); 1.90 if (list == NULL) 1.91 return NULL; 1.92 @@ -156,6 +150,9 @@ 1.93 list->cl = &cx_linked_list_class; 1.94 list->data.allocator = allocator; 1.95 list->data.cmpfunc = comparator; 1.96 + list->data.size = 0; 1.97 + list->data.itemsize = itemsize; 1.98 + list->data.capacity = SIZE_MAX; 1.99 list->data.listdata = cxCalloc(allocator, 1, sizeof(cx_linked_list)); 1.100 if (list->data.listdata == NULL) { 1.101 cxFree(allocator, list);