wip: memory is almost fixed and working fine (infinite loop on zones linked lists)
This commit is contained in:
parent
55037b75fa
commit
73b8ffb3b8
@ -11,7 +11,7 @@
|
||||
|
||||
// BPZ = Blocks Per Zone, which is the max
|
||||
// number of blocks for a new zone
|
||||
enum { BPZ = 128, PAGES_TINY = 16, PAGES_SMALL = 64, MEM_ALIGN = 4 };
|
||||
enum { BPZ = 128, PAGES_TINY = 16, PAGES_SMALL = 64, MEM_ALIGN = 8 };
|
||||
|
||||
typedef enum { TINY, SMALL, LARGE } block_type_t;
|
||||
|
||||
|
@ -4,8 +4,10 @@
|
||||
#include "kprintf.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#define PRINT_PTR(X) kprintf("%s: %p\n", #X, X)
|
||||
#define PRINT_INT(X) kprintf("%s: %d\n", #X, X)
|
||||
#define PRINT_PTR(X) kprintf("%s:%u %s: %p\n", __FILE__, __LINE__, #X, X)
|
||||
#define PRINT_STR(X) kprintf("%s:%u %s: %s\n", __FILE__, __LINE__, #X, X)
|
||||
#define PRINT_UINT(X) kprintf("%s:%u %s: %u\n", __FILE__, __LINE__, #X, X)
|
||||
#define PRINT_INT(X) kprintf("%s:%u %s: %d\n", __FILE__, __LINE__, #X, X)
|
||||
#define assert(X) \
|
||||
do { \
|
||||
if (!(X)) { \
|
||||
|
@ -13,4 +13,4 @@
|
||||
#define KERN_DEBUG "8"
|
||||
|
||||
int kprintf(const char *restrict format, ...);
|
||||
int kvprintf(const char *restrict format, va_list ap);
|
||||
int kvprintf(const char *restrict format, va_list *ap);
|
||||
|
@ -38,6 +38,7 @@ void kernel_main(void)
|
||||
/* kprintf("%p\n", vmalloc(10)); */
|
||||
int i = 0;
|
||||
while (vmalloc(10))
|
||||
if (i++ > 11000)
|
||||
kprintf("%d\n", i++);
|
||||
shell_init();
|
||||
}
|
||||
|
@ -15,14 +15,14 @@ void kpanic(const char *format, ...)
|
||||
/* terminal_set_bg_color(VGA_COLOR_BLUE); */
|
||||
/* terminal_clear(); */
|
||||
va_start(va, format);
|
||||
kvprintf(format, va);
|
||||
kvprintf(format, &va);
|
||||
va_end(va);
|
||||
uint32_t faulting_address;
|
||||
__asm__ __volatile__("mov %%cr2, %0" : "=r"(faulting_address));
|
||||
kprintf("fault at address: %p\n", faulting_address);
|
||||
/* for (int i = 16; i < 32; i++) */
|
||||
/* kprintf("%p\n", page_table1[i]); */
|
||||
/* show_valloc_mem(); */
|
||||
show_valloc_mem();
|
||||
/* kprintf("\n\n"); */
|
||||
/* print_stack(); */
|
||||
/* kprintf("\n\n"); */
|
||||
|
@ -8,7 +8,7 @@ int kprintf(const char *restrict format, ...)
|
||||
int i;
|
||||
|
||||
va_start(va, format);
|
||||
i = kvprintf(format, va);
|
||||
i = kvprintf(format, &va);
|
||||
va_end(va);
|
||||
return i;
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ static int print_flag(char flag, va_list *ap)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int kvprintf(const char *restrict format, va_list ap)
|
||||
int kvprintf(const char *restrict format, va_list *ap)
|
||||
{
|
||||
const char *start = format;
|
||||
int ret = 0;
|
||||
@ -48,7 +48,7 @@ int kvprintf(const char *restrict format, va_list ap)
|
||||
start++;
|
||||
while (*start != '\0') {
|
||||
if (*start == '%' && *(start + 1) != '\0') {
|
||||
ret += print_flag(*(start + 1), &ap);
|
||||
ret += print_flag(*(start + 1), ap);
|
||||
start++;
|
||||
} else {
|
||||
ret += terminal_putchar(*start);
|
||||
|
@ -9,7 +9,7 @@ Zone *vzones[3];
|
||||
static void add_zone(Zone *zone, block_type_t type)
|
||||
{
|
||||
// We put the zone at the beginning of the list
|
||||
if (vzones[type]) {
|
||||
if (vzones[type] && vzones[type] != zone) {
|
||||
zone->next = vzones[type];
|
||||
vzones[type]->prev = zone;
|
||||
}
|
||||
@ -40,12 +40,10 @@ 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);
|
||||
}
|
||||
/* kprintf("before zone: %p zone->free: %p new_block: %p\n", zone, */
|
||||
/* zone->free, new_block); */
|
||||
zone->free = new_block;
|
||||
/* kprintf("after zone: %p zone->free: %p new_block: %p\n", zone, */
|
||||
/* zone->free, new_block); */
|
||||
assert(zone->free == new_block);
|
||||
}
|
||||
|
||||
|
@ -14,22 +14,6 @@ void show_valloc_mem(void)
|
||||
int count = 0;
|
||||
for (Zone *zone_it = vzones[type]; zone_it != NULL;
|
||||
zone_it = zone_it->next) {
|
||||
#if FULL_INFO
|
||||
if (zone_it->free)
|
||||
kprintf("---------- AVAILABLE %s [n°%d - %p] "
|
||||
"----------\n",
|
||||
vzones_name[type], count, zone_it);
|
||||
int i = 0;
|
||||
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,
|
||||
(uint32_t)block_it->ptr +
|
||||
block_it->sub_size + sizeof(Block),
|
||||
block_it->sub_size);
|
||||
}
|
||||
if (zone_it->free)
|
||||
kprintf("\n");
|
||||
#endif
|
||||
if (zone_it->used)
|
||||
kprintf("---------- IN_USE %s [n°%d - %p] "
|
||||
"----------\n",
|
||||
@ -47,6 +31,21 @@ void show_valloc_mem(void)
|
||||
if (zone_it->used)
|
||||
kprintf("\n");
|
||||
count++;
|
||||
#if FULL_INFO
|
||||
if (zone_it->free)
|
||||
kprintf("---------- AVAILABLE %s [n°%d - %p] "
|
||||
"----------\n",
|
||||
vzones_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,
|
||||
(uint32_t)block_it->ptr +
|
||||
block_it->sub_size + sizeof(Block),
|
||||
block_it->sub_size);
|
||||
}
|
||||
if (zone_it->free)
|
||||
kprintf("\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
kprintf("Total: %u\n", total_size);
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "alloc.h"
|
||||
#include "debug.h"
|
||||
#include "kprintf.h"
|
||||
#include "terminal.h"
|
||||
#include <stdint.h>
|
||||
|
||||
int new_zone(block_type_t type, uint32_t size);
|
||||
@ -12,10 +13,13 @@ int new_zone(block_type_t type, uint32_t size);
|
||||
static Block *find_block(Zone *head, uint32_t size)
|
||||
{
|
||||
for (Zone *zone_it = head; zone_it != NULL; zone_it = zone_it->next) {
|
||||
assert(zone_it->free);
|
||||
for (Block *block_it = zone_it->free; block_it != NULL;
|
||||
block_it = block_it->next_free) {
|
||||
/* PRINT_PTR(block_it); */
|
||||
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);
|
||||
@ -39,6 +43,16 @@ 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:
|
||||
@ -51,11 +65,16 @@ 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);
|
||||
assert(!(new_block >=
|
||||
(Block *)((uint32_t)zone + get_zone_size(zone->type))));
|
||||
/* 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)));
|
||||
|
||||
// Newly created block metadata
|
||||
new_block->size = old_block->size - size;
|
||||
new_block->size = old_block->size - align_mem(size);
|
||||
new_block->sub_size = new_block->size;
|
||||
new_block->in_use = false;
|
||||
new_block->ptr = (void *)((uint32_t)new_block + sizeof(Block));
|
||||
@ -69,6 +88,8 @@ static void frag_block(Zone *zone, Block *old_block, uint32_t size)
|
||||
new_block->next_used = NULL;
|
||||
|
||||
new_block->prev_free = old_block->prev_free;
|
||||
new_block->next_free = NULL;
|
||||
if (new_block != old_block->next_free)
|
||||
new_block->next_free = old_block->next_free;
|
||||
|
||||
if (zone->free == old_block)
|
||||
@ -147,6 +168,9 @@ void *vmalloc(uint32_t size)
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user