diff -r 8cd274352419 -r e6a8f7fb0c45 src/linked_list.c --- a/src/linked_list.c Sun Feb 07 20:08:13 2021 +0100 +++ b/src/linked_list.c Sun Feb 07 20:37:20 2021 +0100 @@ -27,6 +27,8 @@ */ #include "cx/linked_list.h" +#include +#include /* LOW LEVEL LINKED LIST FUNCTIONS */ @@ -77,40 +79,37 @@ /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ -typedef struct cx_list_node_s cx_list_node; - -struct cx_list_node_s { - cx_list_node *next; - cx_list_node *prev; - void *data; -}; - typedef struct { - cx_list_node *begin; - cx_list_node *end; - size_t size; + void *begin; + void *end; } cx_linked_list; int cx_ll_add(cx_list *list, void *elem) { cx_linked_list *listdata = list->listdata; CxAllocator allocator = list->allocator; - struct cx_list_node_s *node = cxMalloc(allocator, sizeof(struct cx_list_node_s)); + /* + * Memory layout: + * next : sizeof(void*) + * prev : sizeof(void*) + * data : itemsize + */ + void *node = cxMalloc(allocator,2*sizeof(void*)+list->itemsize); if (node == NULL) return 1; - node->next = NULL; - node->data = elem; + memset(node, 0, sizeof(void*)); + memcpy(node+2, elem, list->itemsize); int ret = cx_linked_list_add( - (void **) &listdata->begin, - (void **) &listdata->end, - offsetof(struct cx_list_node_s, next), - offsetof(struct cx_list_node_s, prev), + &listdata->begin, + &listdata->end, + 0, + sizeof(void*), node ); if (ret == 0) { - listdata->size++; + list->size++; return 0; } else { return ret; @@ -135,20 +134,15 @@ return 0; } -size_t cx_ll_size(cx_list *list) { - cx_linked_list *listdata = list->listdata; - return listdata->size; -} cx_list_class cx_linked_list_class = { cx_ll_add, cx_ll_insert, cx_ll_remove, - cx_ll_find, - cx_ll_size + cx_ll_find }; -CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator) { +CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t itemsize) { CxList list = cxMalloc(allocator, sizeof(list)); if (list == NULL) return NULL; @@ -156,6 +150,9 @@ list->cl = &cx_linked_list_class; list->data.allocator = allocator; list->data.cmpfunc = comparator; + list->data.size = 0; + list->data.itemsize = itemsize; + list->data.capacity = SIZE_MAX; list->data.listdata = cxCalloc(allocator, 1, sizeof(cx_linked_list)); if (list->data.listdata == NULL) { cxFree(allocator, list);