fix memory manager size calculation and clean up

This commit is contained in:
hippoz 2021-08-23 14:38:27 +03:00
parent 4c6c4a0150
commit fda9f4b9dd
Signed by untrusted user who does not match committer: hippoz
GPG key ID: 7C52899193467641
3 changed files with 7 additions and 14 deletions

View file

@ -162,12 +162,13 @@ void _start(struct stivale2_struct *stivale2_struct) {
usize start_mem_addr = (usize)_start;
printf("_start memory address: %#lX\n", start_mem_addr);
mem_populate_blocks(start_mem_addr + 0x42069);
mem_populate_blocks(start_mem_addr + 0x42069); /* TODO: pick an actual known free memory location instead of this random one */
/* TODO: only enable this in debug (or just handle testing in a better way) */
/*
if (mem_test_mm()) {
printf("memory manager test passed\n");
}
*/
printf("----------\n");
printf("hello yes");

View file

@ -2,7 +2,6 @@
#include <stdio.h>
struct mem_block mem_blocks[MEM_BLOCK_LIST_SIZE];
usize last_free_index = 0;
void mem_populate_blocks(usize starting_addr) {
usize last_addr = starting_addr;
@ -13,24 +12,20 @@ void mem_populate_blocks(usize starting_addr) {
mem_blocks[i].size = last_size;
last_addr += mem_blocks[i].size;
last_size += MEM_BLOCK_INCREASE_SIZE;
//printf("-- POPULATED BLOCK: addr = %#lX; size = %#lX\n", mem_blocks[i].addr, mem_blocks[i].size);
}
}
unsigned int find_available_block(usize start_index, usize size) {
struct mem_block block = mem_blocks[start_index];
if (!block.is_used) {
if ((block.addr - mem_blocks[last_free_index].addr) >= size)
return start_index; /* we found a free block */
else
return find_available_block(start_index + 1, size);
if (!block.is_used && block.size > size) {
return start_index; /* we found a free block */
} else {
return find_available_block(start_index + 1, size);
}
}
void* malloc(usize size) {
unsigned int block_index = find_available_block(last_free_index, size);
unsigned int block_index = find_available_block(0, size);
mem_blocks[block_index].is_used = true;
return mem_blocks[block_index].addr;
@ -65,7 +60,6 @@ bool mem_test_mm() {
return false;
}
last_addr = (usize)test_allocated;
//printf("test_struct #%d memory address: %#lX\n", i, (usize)test_allocated);
test_allocated->hello = i;
if (test_allocated->hello != i) {
printf("test_struct #%d: FAIL: test_allocated->hello(%d) != i(%d)", i, test_allocated->hello, i);
@ -77,8 +71,6 @@ bool mem_test_mm() {
will_be_freed->hello = 76;
free(will_be_freed);
//printf("test_struct will_be_freed memory address: %#lX\n", (usize)will_be_freed);
if ((usize)malloc(sizeof(struct test_struct)) != (usize)will_be_freed) {
printf("test_struct will_be_freed: FAIL: next allocation address is not equal to the freed struct's address");
return false;

View file

@ -3,7 +3,7 @@
#include <stdbool.h>
#include <types.h>
#define MEM_BLOCK_LIST_SIZE 4096
#define MEM_BLOCK_LIST_SIZE 8192
#define MEM_BLOCK_INCREASE_SIZE 4
struct mem_block {