From 4595f35e4fac021c97929945b3f087037793af41 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 14 Apr 2023 12:47:11 -0400 Subject: [PATCH] mem: rename `mem_region_header` to `buddy_region_header` --- include/kernel/mem.h | 2 +- mem/buddy.c | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/include/kernel/mem.h b/include/kernel/mem.h index affe8ac..c1749c2 100644 --- a/include/kernel/mem.h +++ b/include/kernel/mem.h @@ -52,7 +52,7 @@ struct mem_region { size_t len; }; -struct mem_region_header { +struct buddy_region_header { paddr_t usable_base; size_t usable_len; diff --git a/mem/buddy.c b/mem/buddy.c index 83c6aa1..1624a57 100644 --- a/mem/buddy.c +++ b/mem/buddy.c @@ -1,5 +1,4 @@ /* SPDX-License-Identifier: GPL-2.0-only */ -#include "math.h" #include #include @@ -12,7 +11,8 @@ struct limine_kernel_address_request kern_req = { .id = LIMINE_KERNEL_ADDRESS_RE static char *kmem; static size_t kmem_len; -static struct page *pml4 ALIGN(0x1000); + +struct page *pml4 ALIGN(0x1000); struct mem_region *mem_regions; size_t num_regions; @@ -51,11 +51,11 @@ static void buddy_init_region(struct mem_region *region) /* 2 bits per page */ size_t bitmap_size_bytes = (region->len >> 14) + 1; - struct mem_region_header *head = (void *)(region->base | hhdm_start); + struct buddy_region_header *head = (void *)(region->base | hhdm_start); memset(head->bitmap, 0, bitmap_size_bytes); paddr_t usable_end = region->base + region->len; - paddr_t curpos = region->base + sizeof(struct mem_region_header) + bitmap_size_bytes; + paddr_t curpos = region->base + sizeof(struct buddy_region_header) + bitmap_size_bytes; /* align to 4K */ curpos |= 0xFFF; curpos += 1; @@ -72,7 +72,7 @@ static void buddy_init_region(struct mem_region *region) head->flist = flist; } -static paddr_t buddy_get_slab(struct mem_region_header *head, size_t depth, size_t n) +static paddr_t buddy_get_slab(struct buddy_region_header *head, size_t depth, size_t n) { if (depth == 0) { return head->usable_base; @@ -81,12 +81,17 @@ static paddr_t buddy_get_slab(struct mem_region_header *head, size_t depth, size return head->usable_base + (n * (head->usable_len >> depth)); } -/* This bitmap for this allocator is 2 bits per page. It begins with one bit - * representing the entire region, then 2 bits representing that region split - * into two, then 4 bits representing that region split into 4, and so on, - * until a granularity of 4K is reached +/* The bitmap for this allocator is 2 bits per page within a region. + * It begins with one bit representing the entire region, then 2 bits + * representing that region split into two, then 4 bits representing that region + * split into 4, and so on, until a granularity of 4K is reached + * + * Conceptually, the bitmap is a tree, where each node has two children, and is + * laid out flat in memory. + * + * The buddy allocator */ -static paddr_t buddy_alloc_helper(struct mem_region_header *head, size_t size) +static paddr_t buddy_alloc_helper(struct buddy_region_header *head, size_t size) { if (npow2(size) != size) { printf(LOG_WARN "buddy_alloc: size not a power of 2\n"); @@ -142,7 +147,7 @@ static paddr_t buddy_alloc_helper(struct mem_region_header *head, size_t size) return 0; } -static void buddy_free_helper(struct mem_region_header *head, paddr_t paddr) +static void buddy_free_helper(struct buddy_region_header *head, paddr_t paddr) { /* find the region in the bitmap */ paddr_t offset = paddr - head->usable_base; @@ -221,7 +226,7 @@ void *buddy_alloc(size_t size) return NULL; for (size_t i = 0; i < num_regions; i++) { - struct mem_region_header *head = (void *)(mem_regions[num_regions - i - 1].base | hhdm_start); + struct buddy_region_header *head = (void *)(mem_regions[num_regions - i - 1].base | hhdm_start); paddr_t ret = buddy_alloc_helper(head, size); if (ret != 0) @@ -236,7 +241,7 @@ void buddy_free(void *ptr) paddr_t paddr = (paddr_t)ptr ^ hhdm_start; for (size_t i = 0; i < num_regions; i++) { - struct mem_region_header *head = (void *)(mem_regions[num_regions - i - 1].base | hhdm_start); + struct buddy_region_header *head = (void *)(mem_regions[num_regions - i - 1].base | hhdm_start); if (paddr >= head->usable_base && paddr < (head->usable_base + head->usable_len)) { buddy_free_helper(head, paddr); @@ -378,7 +383,7 @@ void mem_early_init(char *mem, size_t len) buddy_init_region(®ions[i]); if (!page_found) { - struct mem_region_header *head = (struct mem_region_header *)(regions[i].base | hhdm_start); + struct buddy_region_header *head = (struct buddy_region_header *)(regions[i].base | hhdm_start); paddr_t r_alloc = buddy_alloc_helper(head, 0x1000); if (r_alloc != 0) { page_found = true;