summaryrefslogtreecommitdiff
path: root/kernel/src/libk/list.c
blob: be2e4e5adfb9660d27ced2dce78d72fcaf321689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <libk/list.h>
#include <heap.h>

void add_to_list_head(list_t** ptr, void* data)
{
	list_t* node = (list_t*)kalloc(sizeof(list_t));
	node->data = data;

	node->next = *ptr;
	*ptr = node;
}

void add_to_list_tail(list_t** ptr, void* data)
{
	list_t* node = (list_t*)kalloc(sizeof(list_t));
	node->data = data;
	node->next = NULL;

	if (*ptr == NULL) {
		*ptr = node;
	} else {
		list_t* tmp = *ptr;
		while (tmp->next != NULL)
			tmp = tmp->next;
		tmp->next = node;
	}
}

void free_list(list_t** ptr)
{
	if (*ptr == NULL)
		return;

	for (list_t* tmp = (*ptr)->next; tmp != NULL; tmp = tmp->next) {
		kfree(*ptr);
		*ptr = tmp;
	}
}