proc calloc

This commit is contained in:
Imbus 2025-08-16 15:48:08 +02:00
parent 39ef34d41e
commit b0fe61094d
2 changed files with 86 additions and 10 deletions

View file

@ -1,8 +1,8 @@
#include <ispinlock.h>
#include <kalloc.h>
#include <memory.h>
#include <panic.h>
#include <riscv.h>
#include <spinlock.h>
#include <string.h>
#include <types.h>
@ -23,12 +23,12 @@ struct Run {
/** Kernel memory allocator. */
struct {
struct Spinlock lock;
struct Run *freelist;
spinlock_t lock;
struct Run *freelist;
} kmem;
void kalloc_init() {
initlock(&kmem.lock, "kmem");
spinlock_init(&kmem.lock);
freerange(kernel_end, (void *)PHYSTOP);
}
@ -43,8 +43,7 @@ void kfree(void *pa) {
// Assert that page is a ligned to a page boundary and that its correctly
// sized
if (((u64)pa % PGSIZE) != 0 || (char *)pa < kernel_end ||
(u64)pa >= PHYSTOP)
if (((u64)pa % PGSIZE) != 0 || (char *)pa < kernel_end || (u64)pa >= PHYSTOP)
panic("kfree");
// Fill with junk to catch dangling refs.
@ -52,23 +51,23 @@ void kfree(void *pa) {
r = (struct Run *)pa;
acquire(&kmem.lock);
spin_lock(&kmem.lock);
r->next = kmem.freelist;
kmem.freelist = r;
release(&kmem.lock);
spin_unlock(&kmem.lock);
}
void *kalloc(void) {
struct Run *r;
acquire(&kmem.lock);
spin_lock(&kmem.lock);
r = kmem.freelist;
if (r)
kmem.freelist = r->next;
release(&kmem.lock);
spin_unlock(&kmem.lock);
if (r)
memset((char *)r, 5, PGSIZE); // fill with junk