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 |
--- 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;
--- 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 */
--- 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 <stdint.h> +#include <string.h> /* 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);