summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/kernel/malloc.c22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/kernel/malloc.c b/src/kernel/malloc.c
index 471c945..0d5cba3 100644
--- a/src/kernel/malloc.c
+++ b/src/kernel/malloc.c
@@ -51,7 +51,7 @@ pbitmap_set(void *p, bool v)
assert(b < pbitmap_len);
bool prev = (pbitmap[b]&m) != 0;
if (v) {
- pbitmap[b] |= m;
+ pbitmap[b] |= m;
} else {
pbitmap[b] &= ~m;
}
@@ -83,9 +83,13 @@ mem_reserve(void *addr, size_t len)
void *top = min(addr + len, memtop);
addr = (void*)((uintptr_t)addr & ~PAGE_MASK); /* round down to page */
for (void *p = max(addr, (void*)pbitmap); p < top; p += PAGE_SIZE) {
+ /* this doesn't allow overlapping reserved regions, but, more
+ * importantly, it prevents reserving an already allocated page */
+ if (pbitmap_get(p)) {
+ panic_invalid_state();
+ }
pbitmap_set(p, true);
}
-
}
void
@@ -147,6 +151,7 @@ page_free(void *addr, size_t pages)
panic_invalid_state();
}
}
+ firstfreepage = min(firstfreepage, addr);
}
static void
@@ -206,14 +211,13 @@ void
kfree(void *ptr)
{
Allocation *hdr;
- size_t pages;
+ size_t len;
if (ptr == NULL) return;
- hdr = ptr - sizeof(Allocation);
kmalloc_sanity(ptr);
- pages = page_amt(hdr->len);
+ hdr = ptr - sizeof(Allocation);
+ len = hdr->len;
- hdr->magic = ~MALLOC_MAGIC; // (hopefully) detect double frees
if (hdr->next)
hdr->next->prev = hdr->prev;
if (hdr->prev)
@@ -221,7 +225,9 @@ kfree(void *ptr)
if (malloc_last == hdr)
malloc_last = hdr->prev;
#ifndef NDEBUG
- memset(hdr, 0xC0, pages * PAGE_SIZE);
+ memset(hdr, 0xC0, len);
+#else
+ hdr->magic = ~MALLOC_MAGIC; /* (hopefully) detect double frees */
#endif
- page_free(hdr, pages);
+ page_free(hdr, page_amt(len));
}