Adding malloc and free function that reaffect the ptr to NULL
This commit is contained in:
parent
fbc9544386
commit
aa2c63db4f
|
@ -1,7 +1,5 @@
|
|||
#include <gint/kmalloc.h>
|
||||
#include "linked_list.h"
|
||||
|
||||
#include <gint/display.h>
|
||||
#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)
|
||||
|
|
18
src/main.c
18
src/main.c
|
@ -17,9 +17,9 @@ int main(void)
|
|||
}
|
||||
*/
|
||||
|
||||
#include <gint/kmalloc.h>
|
||||
#include <gint/display.h>
|
||||
#include <gint/keyboard.h>
|
||||
#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();
|
||||
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef MALLOC_H
|
||||
#define MALLOC_H
|
||||
|
||||
#include <gint/kmalloc.h>
|
||||
|
||||
/* 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
|
Loading…
Reference in New Issue