diff --git a/src/linked_list.c b/src/linked_list.c index 49504b1..4edc78b 100644 --- a/src/linked_list.c +++ b/src/linked_list.c @@ -1,7 +1,5 @@ -#include #include "linked_list.h" - -#include +#include "malloc.h" void linked_list_init(linked_list_t *linked_list, const size_t data_size) { @@ -13,13 +11,13 @@ void linked_list_init(linked_list_t *linked_list, const size_t data_size) elem_t *elem_create(linked_list_t *linked_list) { elem_t *elem; - elem = kmalloc(sizeof(elem_t), NULL); + elem = malloc(sizeof(elem_t)); if(!elem) return NULL; - elem->data = kmalloc(linked_list->data_size, NULL); + elem->data = malloc(linked_list->data_size); if(!elem->data) { - kfree(elem); + free(elem); return NULL; } @@ -68,12 +66,8 @@ void linked_list_pop_back(linked_list_t *linked_list) else linked_list->first = NULL; - kfree(tmp->data); - kfree(tmp); - if(tmp != NULL) - { - dtext(80, 80, C_RED, "WHAT THE HELLL ??"); - } + free(tmp->data); + free(tmp); } void linked_list_pop_front(linked_list_t *linked_list) @@ -88,8 +82,8 @@ void linked_list_pop_front(linked_list_t *linked_list) else linked_list->last = NULL; - kfree(tmp->data); - kfree(tmp); + free(tmp->data); + free(tmp); } size_t linked_list_get_size(linked_list_t *linked_list) diff --git a/src/main.c b/src/main.c index 858aa47..b0ae622 100644 --- a/src/main.c +++ b/src/main.c @@ -17,9 +17,9 @@ int main(void) } */ -#include #include #include +#include "malloc.h" #include "linked_list.h" int main(void) @@ -58,22 +58,22 @@ int main(void) } linked_list_pop_back(&list); - if(list.first == list.last && list.first == elem) + if(list.first == list.last && list.first == elem && !elem2) { dtext(1, 40, C_BLACK, "element 2 is correctly removed from the back"); } -/* + linked_list_pop_back(&list); - if(list.first == list.last && list.first == NULL) + if(list.first == list.last && !list.first && !elem) { dtext(1, 60, C_BLACK, "element is correctly removed from the back"); } -*/ + Exit: - //if(elem) kfree(elem->data); - //if(elem) kfree(elem); - //if(elem2) kfree(elem2->data); - //if(elem2) kfree(elem2); + if(elem) free(elem->data); + if(elem) free(elem); + if(elem2) free(elem2->data); + if(elem2) free(elem2); dupdate(); diff --git a/src/malloc.h b/src/malloc.h new file mode 100644 index 0000000..6813226 --- /dev/null +++ b/src/malloc.h @@ -0,0 +1,25 @@ +#ifndef MALLOC_H +#define MALLOC_H + +#include + +/* malloc(): Just call kmalloc from gint/kmalloc.h. + + @size Size of requested block + Returns address of allocated block, NULL on error. */ +inline void *malloc(size_t size) +{ + return kmalloc(size, NULL); +} + +/* malloc(): Just call kmalloc from gint/kmalloc.h. + + @ptr Pointer to free + Free given ptr and reaffect to NULL. */ +inline void free(void *ptr) +{ + kfree(ptr); + ptr = NULL; +} + +#endif // MALLOC_H \ No newline at end of file