This source file includes following definitions.
- kinit
- freerange
- kfree
- kalloc
1
2
3
4
5 #include "types.h"
6 #include "param.h"
7 #include "memlayout.h"
8 #include "spinlock.h"
9 #include "riscv.h"
10 #include "defs.h"
11
12 void freerange(void *pa_start, void *pa_end);
13
14 extern char end[];
15
16
17 struct run {
18 struct run *next;
19 };
20
21 struct {
22 struct spinlock lock;
23 struct run *freelist;
24 } kmem;
25
26 void
27 kinit()
28 {
29 initlock(&kmem.lock, "kmem");
30 freerange(end, (void*)PHYSTOP);
31 }
32
33 void
34 freerange(void *pa_start, void *pa_end)
35 {
36 char *p;
37 p = (char*)PGROUNDUP((uint64)pa_start);
38 for(; p + PGSIZE <= (char*)pa_end; p += PGSIZE)
39 kfree(p);
40 }
41
42
43
44
45
46 void
47 kfree(void *pa)
48 {
49 struct run *r;
50
51 if(((uint64)pa % PGSIZE) != 0 || (char*)pa < end || (uint64)pa >= PHYSTOP)
52 panic("kfree");
53
54
55 memset(pa, 1, PGSIZE);
56
57 r = (struct run*)pa;
58
59 acquire(&kmem.lock);
60 r->next = kmem.freelist;
61 kmem.freelist = r;
62 release(&kmem.lock);
63 }
64
65
66
67
68 void *
69 kalloc(void)
70 {
71 struct run *r;
72
73 acquire(&kmem.lock);
74 r = kmem.freelist;
75 if(r)
76 kmem.freelist = r->next;
77 release(&kmem.lock);
78
79 if(r)
80 memset((char*)r, 5, PGSIZE);
81 return (void*)r;
82 }