forked from Bananymous/banan-os
Kernel: Improve kmalloc error messages
This commit is contained in:
parent
3d3f12bd30
commit
862b32d735
|
@ -1,7 +1,7 @@
|
|||
#include <kernel/multiboot.h>
|
||||
#include <kernel/kmalloc.h>
|
||||
#include <kernel/panic.h>
|
||||
#include <kernel/kprint.h>
|
||||
#include <kernel/Serial.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -29,7 +29,7 @@ static size_t s_kmalloc_allocated = 0;
|
|||
void kmalloc_initialize()
|
||||
{
|
||||
if (!(s_multiboot_info->flags & (1 << 6)))
|
||||
Kernel::panic("Bootloader didn't give a memory map");
|
||||
Kernel::panic("Kmalloc: Bootloader didn't give a memory map");
|
||||
|
||||
// Validate kmalloc memory
|
||||
bool valid = false;
|
||||
|
@ -39,13 +39,9 @@ void kmalloc_initialize()
|
|||
|
||||
if (mmmt->type == 1)
|
||||
{
|
||||
uint8_t* ptr1 = (uint8_t*)mmmt->base_addr;
|
||||
uint8_t* ptr2 = (uint8_t*)s_kmalloc_base;
|
||||
size_t len1 = mmmt->length;
|
||||
size_t len2 = s_kmalloc_size;
|
||||
|
||||
if (ptr1 <= ptr2 && ptr1 + len1 >= ptr2 + len2)
|
||||
if (mmmt->base_addr <= (uint64_t)s_kmalloc_base && (uint64_t)s_kmalloc_end <= mmmt->base_addr + mmmt->length)
|
||||
{
|
||||
dprintln("Total usable RAM: {} MB", (float)mmmt->length / MB);
|
||||
valid = true;
|
||||
break;
|
||||
}
|
||||
|
@ -55,7 +51,7 @@ void kmalloc_initialize()
|
|||
}
|
||||
|
||||
if (!valid)
|
||||
Kernel::panic("Could not find enough space for kmalloc");
|
||||
Kernel::panic("Kmalloc: Could not find 1 MB of memory");
|
||||
|
||||
s_kmalloc_node_count = 1;
|
||||
s_kmalloc_node_head = (kmalloc_node*)s_kmalloc_node_base;
|
||||
|
@ -71,11 +67,14 @@ void kmalloc_initialize()
|
|||
|
||||
void kmalloc_dump_nodes()
|
||||
{
|
||||
dprintln("Kmalloc memory available {} MB", (float)s_kmalloc_available / MB);
|
||||
dprintln("Kmalloc memory allocated {} MB", (float)s_kmalloc_allocated / MB);
|
||||
dprintln("Using {}/{} nodes", s_kmalloc_node_count, s_kmalloc_max_nodes);
|
||||
for (size_t i = 0; i < s_kmalloc_node_count; i++)
|
||||
{
|
||||
kmalloc_node& node = s_kmalloc_node_head[i];
|
||||
if (i < 10) kprint(" ");
|
||||
kprint(" ({}) {}, node at {}, free: {}, size: {}\n", i, (void*)&node, (void*)node.addr, node.free, node.size);
|
||||
if (i < 10) dprint(" ");
|
||||
dprintln(" ({}) {}, node at {}, free: {}, size: {}", i, (void*)&node, (void*)node.addr, node.free, node.size);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,7 +94,7 @@ void* kmalloc(size_t size)
|
|||
|
||||
if (valid_node_index == size_t(-1))
|
||||
{
|
||||
kprint("Could not allocate {} bytes\n", size);
|
||||
dprintln("\e[33mKmalloc: Could not allocate {} bytes\e[0m", size);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
@ -111,7 +110,7 @@ void* kmalloc(size_t size)
|
|||
|
||||
if (s_kmalloc_node_count == s_kmalloc_max_nodes)
|
||||
{
|
||||
kprint("Out of kmalloc nodes\n");
|
||||
dprintln("\e[33mKmalloc: Out of kmalloc nodes\e[0m");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
@ -152,7 +151,7 @@ void kfree(void* addr)
|
|||
|
||||
if (node_index == size_t(-1))
|
||||
{
|
||||
kprint("Attempting to free unallocated pointer {}\n", addr);
|
||||
dprintln("\e[33mKmalloc: Attempting to free unallocated pointer {}\e[0m", addr);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue