core: rename vzones to zones

This commit is contained in:
0x35c 2024-12-03 09:28:17 +01:00
parent f626edd0f0
commit 4bd4198293
5 changed files with 18 additions and 43 deletions

View File

@ -65,7 +65,7 @@ typedef struct Zone {
* For LARGE, it will be entire page(s). * For LARGE, it will be entire page(s).
*/ */
extern Zone *kzones[3]; extern Zone *kzones[3];
extern Zone *vzones[3]; extern Zone *zones[3];
/*----------- UTILS ----------*/ /*----------- UTILS ----------*/
block_type_t get_type(size_t size); block_type_t get_type(size_t size);

View File

@ -4,16 +4,17 @@
#include "kprintf.h" #include "kprintf.h"
#include "memory.h" #include "memory.h"
Zone *vzones[3]; Zone *zones[3];
static void add_zone(Zone *zone, block_type_t type) static void add_zone(Zone *zone, block_type_t type)
{ {
// We put the zone at the beginning of the list // We put the zone at the beginning of the list
if (vzones[type] && vzones[type] != zone) { if (zones[type]) {
zone->next = vzones[type]; assert(zones[type] != zone);
vzones[type]->prev = 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) 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; zone->free->prev_free = new_block;
new_block->next = zone->free; new_block->next = zone->free;
new_block->next_free = zone->free; new_block->next_free = zone->free;
PRINT_PTR(new_block);
PRINT_PTR(new_block->next_free);
} }
zone->free = new_block; zone->free = new_block;
assert(zone->free == new_block); assert(zone->free == new_block);

View File

@ -7,17 +7,17 @@
void show_valloc_mem(void) 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; uint32_t total_size = 0;
for (block_type_t type = 0; type < 3; ++type) { for (block_type_t type = 0; type < 3; ++type) {
int count = 0; 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) { zone_it = zone_it->next) {
if (zone_it->used) if (zone_it->used)
kprintf("---------- IN_USE %s [n°%d - %p] " kprintf("---------- IN_USE %s [n°%d - %p] "
"----------\n", "----------\n",
vzones_name[type], count, zone_it); zones_name[type], count, zone_it);
for (Block *block_it = zone_it->used; block_it != NULL; for (Block *block_it = zone_it->used; block_it != NULL;
block_it = block_it->next_used) { block_it = block_it->next_used) {
/* i++; */ /* i++; */
@ -35,7 +35,7 @@ void show_valloc_mem(void)
if (zone_it->free) if (zone_it->free)
kprintf("---------- AVAILABLE %s [n°%d - %p] " kprintf("---------- AVAILABLE %s [n°%d - %p] "
"----------\n", "----------\n",
vzones_name[type], count, zone_it); zones_name[type], count, zone_it);
for (Block *block_it = zone_it->free; block_it != NULL; for (Block *block_it = zone_it->free; block_it != NULL;
block_it = block_it->next_free) { block_it = block_it->next_free) {
kprintf("%p - %p : %u bytes\n", block_it->ptr, kprintf("%p - %p : %u bytes\n", block_it->ptr,

View File

@ -25,7 +25,7 @@ static void remove_used(Block *to_free)
/* /*
* If all the blocks of the zone have been kfreed, * 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) static int unmap_zone(Zone *zone)
{ {
@ -37,11 +37,11 @@ static int unmap_zone(Zone *zone)
zone->next = NULL; zone->next = NULL;
if (!left && !right) { if (!left && !right) {
vzones[type] = NULL; zones[type] = NULL;
goto unmap; goto unmap;
} }
if (!left) if (!left)
vzones[type] = right; zones[type] = right;
else else
left->next = right; left->next = right;
if (right) if (right)

View File

@ -4,8 +4,6 @@
#include "terminal.h" #include "terminal.h"
#include <stdint.h> #include <stdint.h>
int new_zone(block_type_t type, uint32_t size);
/* /*
* Find first available (not in_use) block * Find first available (not in_use) block
* in a zone matching the size we need * 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; for (Block *block_it = zone_it->free; block_it != NULL;
block_it = block_it->next_free) { block_it = block_it->next_free) {
assert(block_it); assert(block_it);
if (block_it->in_use) {
/* PRINT_PTR(block_it); */
/* PRINT_PTR(block_it->zone); */
}
assert(!block_it->in_use); assert(!block_it->in_use);
if (size <= block_it->size) { if (size <= block_it->size) {
assert(block_it->zone == zone_it); 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: * Let's say the metadata takes a size of 2:
* ... -> [metadata][data][remaining size] -> [6] * ... -> [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 * 2 10 20
* *
* So now our block [new] will become: * 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) static void frag_block(Zone *zone, Block *old_block, uint32_t size)
{ {
Block *new_block = (Block *)align_mem((uint32_t)old_block + 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 < assert(new_block <
(Block *)((uint32_t)zone + get_zone_size(zone->type))); (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; 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) static void save_block(Zone *head, Block *block, Zone *zone)
{ {
zone->free = NULL; 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 * First, we init the allocator if it's the first time
* Then we search if there is an available block in all * 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 * If no block has been found (NULL), we create 1 new zone of
* the corresponding type * the corresponding type
* We then search again for an available block (should not be NULL) * 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 // Find the zone we need to search
block_type_t type = get_type(size); 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" // Find an available block in a zone of type "type"
Block *available = find_block(head, size); Block *available = find_block(head, size);
@ -164,13 +143,10 @@ void *vmalloc(uint32_t size)
full_size = get_zone_size(type); full_size = get_zone_size(type);
if (new_vzone(type, full_size) == -1) if (new_vzone(type, full_size) == -1)
return NULL; return NULL;
head = vzones[type]; head = zones[type];
available = find_block(head, size); available = find_block(head, size);
} }
assert(available != NULL); assert(available != NULL);
/* PRINT_PTR(available); */
/* PRINT_PTR(available->zone); */
/* PRINT_PTR(available->zone->size); */
if (type == LARGE) if (type == LARGE)
save_block(head, available, available->zone); save_block(head, available, available->zone);
else else