summaryrefslogtreecommitdiff
path: root/sys/kern/mm/kalloc.c
blob: b17d4321c43ca1e020a164c4c3069bdb1ef06a2c (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
39
40
#include <stddef.h>
#include <string.h>
#include <mm/kalloc.h>

typedef struct freenode {
	struct freenode *next;
} freenode_t;

freenode_t *head = NULL;

void *
kalloc(void) 
{
	freenode_t *p = head;

	if (p == NULL)
		return NULL;

	head = p->next;
	return p;
}

void *
kzalloc(void)
{
	freenode_t *p = kalloc();
	memset(p, 0, PAGE_SIZE);
	return p;
}

void
kfree(void *p)
{
	if (p == NULL || (unsigned long)p % PAGE_SIZE)
		return;

	freenode_t *tmp = head;
	head = p;
	head->next = tmp;
}