diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 80c7accc..6947c075 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -4,6 +4,7 @@ #include "arch/Timer.h" #include "boot/Init.h" #include "config.h" +#include "memory/Heap.h" #include "memory/MemoryManager.h" #include "thread/Scheduler.h" #include @@ -23,6 +24,13 @@ void async_thread() } } +void heap_thread() +{ + CPU::disable_interrupts(); + dump_heap_usage(); + while (true) kernel_sleep(UINT64_MAX); +} + Result init() { kinfoln("Starting Moon %s", MOON_VERSION); @@ -33,19 +41,15 @@ Result init() Timer::init(); - char buffer[64]; - to_dynamic_unit(MemoryManager::total(), buffer, sizeof(buffer)); - kinfoln("Total memory: %s", buffer); - to_dynamic_unit(MemoryManager::free(), buffer, sizeof(buffer)); - kinfoln("Free memory: %s", buffer); - to_dynamic_unit(MemoryManager::used(), buffer, sizeof(buffer)); - kinfoln("Used memory: %s", buffer); - to_dynamic_unit(MemoryManager::reserved(), buffer, sizeof(buffer)); - kinfoln("Reserved memory: %s", buffer); + kinfoln("Total memory: %s", to_dynamic_unit(MemoryManager::total()).release_value().chars()); + kinfoln("Free memory: %s", to_dynamic_unit(MemoryManager::free()).release_value().chars()); + kinfoln("Used memory: %s", to_dynamic_unit(MemoryManager::used()).release_value().chars()); + kinfoln("Reserved memory: %s", to_dynamic_unit(MemoryManager::reserved()).release_value().chars()); Scheduler::init(); TRY(Scheduler::new_kernel_thread(async_thread)); + TRY(Scheduler::new_kernel_thread(heap_thread)); CPU::platform_finish_init(); diff --git a/luna/include/luna/Units.h b/luna/include/luna/Units.h index 854c18b8..d20b4e0e 100644 --- a/luna/include/luna/Units.h +++ b/luna/include/luna/Units.h @@ -1,4 +1,6 @@ #pragma once +#include #include -Result to_dynamic_unit(usize value, char* buffer, usize max); \ No newline at end of file +Result to_dynamic_unit_cstr(usize value, char* buffer, usize max); +Result to_dynamic_unit(usize value); \ No newline at end of file diff --git a/luna/src/Units.cpp b/luna/src/Units.cpp index 0b64dd9e..e8002d1c 100644 --- a/luna/src/Units.cpp +++ b/luna/src/Units.cpp @@ -1,8 +1,10 @@ +#include #include #include +#include #include -Result to_dynamic_unit(usize value, char* buffer, usize max) +Result to_dynamic_unit_cstr(usize value, char* buffer, usize max) { if (value < 1024) { return string_format(buffer, max, "%u bytes", value); } @@ -14,4 +16,17 @@ Result to_dynamic_unit(usize value, char* buffer, usize max) } return string_format(buffer, max, "%u.%u %ciB", value / 1024, (value % 1024) / 103, *unit_prefixes); +} + +Result to_dynamic_unit(usize value) +{ + char* buf = TRY(make_array(64)); + + auto guard = make_scope_guard([&] { destroy_array(buf); }); + + TRY(to_dynamic_unit_cstr(value, buf, 64)); + + guard.deactivate(); + + return OwnedStringView{buf}; } \ No newline at end of file