From 4bd4198293b6bceb05bd4a59e8f3f67ac7bc92ab Mon Sep 17 00:00:00 2001 From: 0x35c Date: Tue, 3 Dec 2024 09:28:17 +0100 Subject: [PATCH] core: rename vzones to zones --- headers/alloc.h | 2 +- src/memory/virt/allocator.c | 13 ++++++------- src/memory/virt/info.c | 8 ++++---- src/memory/virt/vfree.c | 6 +++--- src/memory/virt/vmalloc.c | 32 ++++---------------------------- 5 files changed, 18 insertions(+), 43 deletions(-) diff --git a/headers/alloc.h b/headers/alloc.h index ba9191a..c91511b 100644 --- a/headers/alloc.h +++ b/headers/alloc.h @@ -65,7 +65,7 @@ typedef struct Zone { * For LARGE, it will be entire page(s). */ extern Zone *kzones[3]; -extern Zone *vzones[3]; +extern Zone *zones[3]; /*----------- UTILS ----------*/ block_type_t get_type(size_t size); diff --git a/src/memory/virt/allocator.c b/src/memory/virt/allocator.c index 5d3b4ac..28f3255 100644 --- a/src/memory/virt/allocator.c +++ b/src/memory/virt/allocator.c @@ -4,16 +4,17 @@ #include "kprintf.h" #include "memory.h" -Zone *vzones[3]; +Zone *zones[3]; static void add_zone(Zone *zone, block_type_t type) { // We put the zone at the beginning of the list - if (vzones[type] && vzones[type] != zone) { - zone->next = vzones[type]; - vzones[type]->prev = zone; + if (zones[type]) { + assert(zones[type] != zone); + zone->next = zones[type]; + zones[type]->prev = zone; } - vzones[type] = zone; + zones[type] = zone; } static void new_block(Zone *zone, uint32_t zone_size) @@ -40,8 +41,6 @@ static void new_block(Zone *zone, uint32_t zone_size) zone->free->prev_free = new_block; new_block->next = zone->free; new_block->next_free = zone->free; - PRINT_PTR(new_block); - PRINT_PTR(new_block->next_free); } zone->free = new_block; assert(zone->free == new_block); diff --git a/src/memory/virt/info.c b/src/memory/virt/info.c index 7100f3e..175ab8c 100644 --- a/src/memory/virt/info.c +++ b/src/memory/virt/info.c @@ -7,17 +7,17 @@ void show_valloc_mem(void) { - char *const vzones_name[3] = {"TINY", "SMALL", "LARGE"}; + char *const zones_name[3] = {"TINY", "SMALL", "LARGE"}; uint32_t total_size = 0; for (block_type_t type = 0; type < 3; ++type) { int count = 0; - for (Zone *zone_it = vzones[type]; zone_it != NULL; + for (Zone *zone_it = zones[type]; zone_it != NULL; zone_it = zone_it->next) { if (zone_it->used) kprintf("---------- IN_USE %s [n°%d - %p] " "----------\n", - vzones_name[type], count, zone_it); + zones_name[type], count, zone_it); for (Block *block_it = zone_it->used; block_it != NULL; block_it = block_it->next_used) { /* i++; */ @@ -35,7 +35,7 @@ void show_valloc_mem(void) if (zone_it->free) kprintf("---------- AVAILABLE %s [n°%d - %p] " "----------\n", - vzones_name[type], count, zone_it); + zones_name[type], count, zone_it); for (Block *block_it = zone_it->free; block_it != NULL; block_it = block_it->next_free) { kprintf("%p - %p : %u bytes\n", block_it->ptr, diff --git a/src/memory/virt/vfree.c b/src/memory/virt/vfree.c index b712202..40bb27d 100644 --- a/src/memory/virt/vfree.c +++ b/src/memory/virt/vfree.c @@ -25,7 +25,7 @@ static void remove_used(Block *to_free) /* * If all the blocks of the zone have been kfreed, - * we can unmap the zone and delete it from the list of vzones + * we can unmap the zone and delete it from the list of zones */ static int unmap_zone(Zone *zone) { @@ -37,11 +37,11 @@ static int unmap_zone(Zone *zone) zone->next = NULL; if (!left && !right) { - vzones[type] = NULL; + zones[type] = NULL; goto unmap; } if (!left) - vzones[type] = right; + zones[type] = right; else left->next = right; if (right) diff --git a/src/memory/virt/vmalloc.c b/src/memory/virt/vmalloc.c index dce5352..8dc3c72 100644 --- a/src/memory/virt/vmalloc.c +++ b/src/memory/virt/vmalloc.c @@ -4,8 +4,6 @@ #include "terminal.h" #include -int new_zone(block_type_t type, uint32_t size); - /* * Find first available (not in_use) block * in a zone matching the size we need @@ -16,10 +14,6 @@ static Block *find_block(Zone *head, uint32_t size) for (Block *block_it = zone_it->free; block_it != NULL; block_it = block_it->next_free) { assert(block_it); - if (block_it->in_use) { - /* PRINT_PTR(block_it); */ - /* PRINT_PTR(block_it->zone); */ - } assert(!block_it->in_use); if (size <= block_it->size) { assert(block_it->zone == zone_it); @@ -43,16 +37,6 @@ static Block *find_block(Zone *head, uint32_t size) * Let's say the metadata takes a size of 2: * ... -> [metadata][data][remaining size] -> [6] * ^ ^ ^ - PRINT_UINT(size); - PRINT_UINT(size); - PRINT_UINT(block_it->size); - if (size <= block_it->size) { - assert(block_it->zone == zone_it); - return (block_it); - PRINT_UINT(block_it->size); - if (size <= block_it->size) { - assert(block_it->zone == zone_it); - return (block_it); * 2 10 20 * * So now our block [new] will become: @@ -65,11 +49,6 @@ static Block *find_block(Zone *head, uint32_t size) static void frag_block(Zone *zone, Block *old_block, uint32_t size) { Block *new_block = (Block *)align_mem((uint32_t)old_block + size); - /* PRINT_PTR(old_block); */ - /* PRINT_PTR(new_block); */ - /* PRINT_UINT(old_block->size); */ - /* PRINT_UINT(size); */ - /* PRINT_PTR(zone); */ assert(new_block < (Block *)((uint32_t)zone + get_zone_size(zone->type))); @@ -113,7 +92,7 @@ static void frag_block(Zone *zone, Block *old_block, uint32_t size) zone->used = old_block; } -// Set the block to use and unset free +// Set the block to used and unset free static void save_block(Zone *head, Block *block, Zone *zone) { zone->free = NULL; @@ -132,7 +111,7 @@ static void save_block(Zone *head, Block *block, Zone *zone) * * First, we init the allocator if it's the first time * Then we search if there is an available block in all - * the vzones currently mapped + * the zones currently mapped * If no block has been found (NULL), we create 1 new zone of * the corresponding type * We then search again for an available block (should not be NULL) @@ -152,7 +131,7 @@ void *vmalloc(uint32_t size) // Find the zone we need to search block_type_t type = get_type(size); - Zone *head = vzones[type]; + Zone *head = zones[type]; // Find an available block in a zone of type "type" Block *available = find_block(head, size); @@ -164,13 +143,10 @@ void *vmalloc(uint32_t size) full_size = get_zone_size(type); if (new_vzone(type, full_size) == -1) return NULL; - head = vzones[type]; + head = zones[type]; available = find_block(head, size); } assert(available != NULL); - /* PRINT_PTR(available); */ - /* PRINT_PTR(available->zone); */ - /* PRINT_PTR(available->zone->size); */ if (type == LARGE) save_block(head, available, available->zone); else