Sun, 07 Feb 2021 21:03:30 +0100
use named fields to access node memory
src/cx/linked_list.h | file | annotate | diff | comparison | revisions | |
src/linked_list.c | file | annotate | diff | comparison | revisions |
--- a/src/cx/linked_list.h Sun Feb 07 20:37:20 2021 +0100 +++ b/src/cx/linked_list.h Sun Feb 07 21:03:30 2021 +0100 @@ -34,7 +34,7 @@ void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next); -int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_next, ptrdiff_t loc_prev, void *newnode); +int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *newnode); CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t itemsize);
--- a/src/linked_list.c Sun Feb 07 20:37:20 2021 +0100 +++ b/src/linked_list.c Sun Feb 07 21:03:30 2021 +0100 @@ -51,7 +51,7 @@ } } -int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_next, ptrdiff_t loc_prev, void *newnode) { +int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *newnode) { // TODO: how do we report error messages? if (loc_next < 0 || (begin == NULL && end == NULL)) { return 1; @@ -79,6 +79,12 @@ /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ +struct cx_linked_list_node { + void *prev; + void *next; + int payload; +}; + typedef struct { void *begin; void *end; @@ -88,24 +94,19 @@ cx_linked_list *listdata = list->listdata; CxAllocator allocator = list->allocator; - /* - * Memory layout: - * next : sizeof(void*) - * prev : sizeof(void*) - * data : itemsize - */ - void *node = cxMalloc(allocator,2*sizeof(void*)+list->itemsize); + struct cx_linked_list_node *node = cxMalloc(allocator, + sizeof(struct cx_linked_list_node) - sizeof(int) + list->itemsize); if (node == NULL) return 1; - memset(node, 0, sizeof(void*)); - memcpy(node+2, elem, list->itemsize); + node->next = NULL; + memcpy(&node->payload, elem, list->itemsize); int ret = cx_linked_list_add( &listdata->begin, &listdata->end, - 0, - sizeof(void*), + offsetof(struct cx_linked_list_node, prev), + offsetof(struct cx_linked_list_node, next), node ); if (ret == 0) {