summaryrefslogtreecommitdiff
path: root/sys/kern/mm/kalloc.c
blob: f354648a3edcdbd80b255f2ba30f724e8fc9f9c8 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <printf.h>
#include <mm/kalloc.h>

extern uint64_t HEAP_START; 

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)
		return;

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

void
kalloc_init(void)
{
        printf("1\n");
        freenode_t *p = (freenode_t*)HEAP_START;
        for (;(unsigned long)p + PAGE_SIZE <= (HEAP_START + 0x100000); p += PAGE_SIZE) {
                printf("freeing page at %p", p);
                kfree(p);
        }
}

void 
walkfree(void)
{
        freenode_t *node = head;
        int nfree = 0;
        while (node) {
                printf("freenode at %p\n", node);
                node = node->next;
                nfree++;
        }
        printf("found and printed %d free pages!\n", nfree);
}