diff options
author | dzwdz | 2024-07-15 12:20:04 +0200 |
---|---|---|
committer | dzwdz | 2024-07-15 12:20:04 +0200 |
commit | c92276ee58473ea1ae42470d56972c427187e326 (patch) | |
tree | 8609beb2ae317ef0f043522b99e310182f7d8279 /src/kernel | |
parent | fb7949549435e735acef3674b10f429fa4c4789e (diff) |
kernel: minor malloc tweaks before refactor
* firstfreepage now updates properly, preventing a crash (oops)
* kfree only wipes the length of the allocation, not the entire page - which
should make it easier to see the performance impact of the pagealloc changes
Diffstat (limited to 'src/kernel')
-rw-r--r-- | src/kernel/malloc.c | 22 |
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)); } |