From c85f0d96695ab2ac10501a8dc21b676b641c54d6 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Fri, 14 Apr 2023 15:28:14 +0200 Subject: [PATCH] added node value allocation by passed size --- include/llist.h | 2 +- src/core/lib/utils/llist.c | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/llist.h b/include/llist.h index 9c3a5c0..9e6b721 100644 --- a/include/llist.h +++ b/include/llist.h @@ -35,7 +35,7 @@ typedef struct } why2_list_t; //SINGLE LINKED LIST MADE OF why2_node_t //FUNCTIONS -void why2_list_push(why2_list_t *list, void *value); //PUSH ELEMENT TO LIST BACK +void why2_list_push(why2_list_t *list, void *value, unsigned long size); //PUSH ELEMENT TO LIST BACK void why2_list_remove(why2_list_t *list, why2_node_t *node); //REMOVE ELEMENT void why2_list_remove_back(why2_list_t *list); //REMOVE LAST ELEMENT why2_node_t *why2_list_find(why2_list_t *list, void *value); //FIND ELEMENT IN LIST diff --git a/src/core/lib/utils/llist.c b/src/core/lib/utils/llist.c index bfe1e25..b316e72 100644 --- a/src/core/lib/utils/llist.c +++ b/src/core/lib/utils/llist.c @@ -22,26 +22,29 @@ along with this program. If not, see . #include #include -void why2_list_push(why2_list_t *list, void *value) +void why2_list_push(why2_list_t *list, void *value, unsigned long size) { //CREATE NODE why2_node_t *head = list -> head; why2_node_t *new_node = malloc(sizeof(why2_node_t)); + new_node -> value = malloc(size); //TODO: Deallocation why2_node_t *buffer = head; //INSERT DATA - new_node -> value = value; + memcpy(new_node -> value, value, size); new_node -> next = NULL; if (head == NULL) //INIT LIST { - head = new_node; + buffer = new_node; } else { while (buffer -> next != NULL) buffer = buffer -> next; //GET TO THE END OF LIST buffer -> next = new_node; //LINK } + + list -> head = buffer; //TODO: Unused? } void why2_list_remove(why2_list_t *list, why2_node_t *node) @@ -77,6 +80,7 @@ void why2_list_remove(why2_list_t *list, why2_node_t *node) } //DEALLOCATION + free(node -> value); free(node); }