From b6f8c43dabd4725b3a74795b85364c1a685ec6eb Mon Sep 17 00:00:00 2001 From: Alessandro Iezzi Date: Wed, 10 May 2023 16:31:53 +0200 Subject: Add data size when adding new items to the list When adding new data to the list, we need exactly how long is the data. So, it's programer responsibility to calculate the exact size of data to store. --- src/list.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src/list.c') diff --git a/src/list.c b/src/list.c index 87e6960..01a6822 100644 --- a/src/list.c +++ b/src/list.c @@ -18,13 +18,15 @@ clist_create() void clist_add(list_t *list, - void *data) + void *data, + size_t data_s) { - struct list_item_t *item = malloc(sizeof(struct list_item_t)); + struct list_item_t *item = malloc(sizeof(struct list_item_t)); - void *dest = malloc(sizeof(data)); /* allocates memory like data parameter */ - memcpy(dest, data, sizeof(data)); /* copies data inside dest */ + void *dest = malloc(data_s); /* allocates memory like data parameter */ + memcpy(dest, data, data_s); /* copies data inside dest */ item->data = dest; + item->data_s = data_s; item->next = NULL; if (list->first == NULL) @@ -45,7 +47,8 @@ void clist_add_all(list_t *dest, iterator_t i = clist_iterator(other); while (clist_iterator_has_next(i)) { - clist_add(dest, clist_iterator_next(&i)); + list_item_t *item = clist_iterator_next_item(&i); + clist_add(dest, item->data, item->data_s); } } @@ -98,3 +101,16 @@ clist_iterator_next(iterator_t *i) return data; } + +list_item_t * +clist_iterator_next_item(iterator_t *i) +{ + if (i == NULL || i->current == NULL) + return NULL; + + list_item_t *item = i->current; + /* `next` can be NULL */ + i->current = i->current->next; + + return item; +} -- cgit v1.2.3