# HG changeset patch # User Mike Becker # Date 1612726640 -3600 # Node ID e6a8f7fb0c4557b1de13b14721c7f2d3fdbea763 # Parent 8cd2743524193ac5959510f24e066383db3f36d5 copy list items when they are added to the list diff -r 8cd274352419 -r e6a8f7fb0c45 src/cx/linked_list.h --- a/src/cx/linked_list.h Sun Feb 07 20:08:13 2021 +0100 +++ b/src/cx/linked_list.h Sun Feb 07 20:37:20 2021 +0100 @@ -36,7 +36,7 @@ int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_next, ptrdiff_t loc_prev, void *newnode); -CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator); +CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t itemsize); extern cx_list_class cx_linked_list_class; diff -r 8cd274352419 -r e6a8f7fb0c45 src/cx/list.h --- a/src/cx/list.h Sun Feb 07 20:08:13 2021 +0100 +++ b/src/cx/list.h Sun Feb 07 20:37:20 2021 +0100 @@ -37,6 +37,9 @@ typedef struct { CxAllocator allocator; CxListComparator cmpfunc; + size_t itemsize; + size_t size; + size_t capacity; void *listdata; } cx_list; @@ -48,14 +51,11 @@ typedef size_t (*cx_list_find)(cx_list *list, void *elem); -typedef size_t (*cx_list_size)(cx_list *list); - typedef struct { cx_list_add add; cx_list_insert insert; cx_list_remove remove; cx_list_find find; - cx_list_size size; } cx_list_class; struct cx_list_s { @@ -73,6 +73,4 @@ size_t cxListFind(CxList list, void *elem); -size_t cxListSize(CxList list); - #endif /* UCX_LIST_H */ 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); diff -r 8cd274352419 -r e6a8f7fb0c45 src/list.c --- a/src/list.c Sun Feb 07 20:08:13 2021 +0100 +++ b/src/list.c Sun Feb 07 20:37:20 2021 +0100 @@ -43,7 +43,3 @@ size_t cxListFind(CxList list, void *elem) { return list->cl->find(&list->data, elem); } - -size_t cxListSize(CxList list) { - return list->cl->size(&list->data); -}