diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 4aa3eacd..24b3bcd0 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -6,6 +6,7 @@ #include "memory/Heap.h" #include "memory/MemoryManager.h" #include "video/TextConsole.h" +#include Result init() { @@ -51,6 +52,14 @@ Result init() TRY(destroy(mem)); + char buffer[64]; + to_dynamic_unit(MemoryManager::free(), buffer, sizeof(buffer)); + Serial::printf("Free memory: %s\n", buffer); + to_dynamic_unit(MemoryManager::used(), buffer, sizeof(buffer)); + Serial::printf("Used memory: %s\n", buffer); + to_dynamic_unit(MemoryManager::reserved(), buffer, sizeof(buffer)); + Serial::printf("Reserved memory: %s\n", buffer); + while (1) { while ((Timer::ticks_ms() - start) < 20) { CPU::wait_for_interrupt(); } diff --git a/luna/CMakeLists.txt b/luna/CMakeLists.txt index 7481c08b..ee20a2ce 100644 --- a/luna/CMakeLists.txt +++ b/luna/CMakeLists.txt @@ -2,6 +2,7 @@ set(FREESTANDING_SOURCES Format.cpp NumberParsing.cpp String.cpp + Units.cpp ) set(SOURCES diff --git a/luna/Units.cpp b/luna/Units.cpp new file mode 100644 index 00000000..98eb4e70 --- /dev/null +++ b/luna/Units.cpp @@ -0,0 +1,17 @@ +#include +#include +#include + +Result to_dynamic_unit(usize value, char* buffer, size_t max) +{ + if (value < 1024) { return string_format(buffer, max, "%u bytes", value); } + + const char* unit_prefixes = "KMGTPE"; + for (int i = 40; i >= 0 && value > (0xfffccccccccccccUL >> i); i -= 10) + { + value >>= 10; + unit_prefixes++; + } + + return string_format(buffer, max, "%u.%u %ciB", value / 1024, (value % 1024) / 103, *unit_prefixes); +} \ No newline at end of file diff --git a/luna/Units.h b/luna/Units.h new file mode 100644 index 00000000..c12aac63 --- /dev/null +++ b/luna/Units.h @@ -0,0 +1,4 @@ +#pragma once +#include + +Result to_dynamic_unit(usize value, char* buffer, usize max); \ No newline at end of file