89 lines
2.9 KiB
C
89 lines
2.9 KiB
C
#include <mem/mem.h>
|
|
#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;
|
|
unsigned int last_size = 1;
|
|
|
|
for (int i = 0; i < MEM_BLOCK_LIST_SIZE; i++) {
|
|
mem_blocks[i].addr = last_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);
|
|
} else {
|
|
return find_available_block(start_index + 1, size);
|
|
}
|
|
}
|
|
|
|
void* malloc(usize size) {
|
|
unsigned int block_index = find_available_block(last_free_index, size);
|
|
mem_blocks[block_index].is_used = true;
|
|
|
|
return mem_blocks[block_index].addr;
|
|
}
|
|
|
|
void free(void* memory) {
|
|
for (int i = 0; i < MEM_BLOCK_LIST_SIZE; i++) {
|
|
if (mem_blocks[i].addr == (usize)memory) {
|
|
mem_blocks[i].is_used = false;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
struct test_struct {
|
|
int hello;
|
|
};
|
|
|
|
/* terrible function to test the really bad memory manager i've made - it's not great at all (only checks if the last element has the same address) */
|
|
bool mem_test_mm() {
|
|
unsigned int count = 42;
|
|
usize last_addr = 1;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
struct test_struct* test_allocated = (struct test_struct*) malloc(sizeof(struct test_struct));
|
|
if ((usize)test_allocated == 0) {
|
|
printf("test_struct #%d: FAIL: malloc() -> 0", i);
|
|
return false;
|
|
}
|
|
if ((usize)test_allocated == last_addr) {
|
|
printf("test_struct #%d: FAIL: new allocated struct address is the same as the last one", i);
|
|
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);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
struct test_struct* will_be_freed = (struct test_struct*) malloc(sizeof(struct test_struct));
|
|
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;
|
|
}
|
|
|
|
return true;
|
|
}
|