Compare commits
2 Commits
1ed51d11cb
...
f1756e6f58
Author | SHA1 | Date | |
---|---|---|---|
f1756e6f58 | |||
552186ad51 |
@ -6,6 +6,7 @@
|
|||||||
#include "memory/Heap.h"
|
#include "memory/Heap.h"
|
||||||
#include "memory/MemoryManager.h"
|
#include "memory/MemoryManager.h"
|
||||||
#include "video/TextConsole.h"
|
#include "video/TextConsole.h"
|
||||||
|
#include <Units.h>
|
||||||
|
|
||||||
Result<void> init()
|
Result<void> init()
|
||||||
{
|
{
|
||||||
@ -51,6 +52,14 @@ Result<void> init()
|
|||||||
|
|
||||||
TRY(destroy(mem));
|
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 (1)
|
||||||
{
|
{
|
||||||
while ((Timer::ticks_ms() - start) < 20) { CPU::wait_for_interrupt(); }
|
while ((Timer::ticks_ms() - start) < 20) { CPU::wait_for_interrupt(); }
|
||||||
|
@ -2,6 +2,7 @@ set(FREESTANDING_SOURCES
|
|||||||
Format.cpp
|
Format.cpp
|
||||||
NumberParsing.cpp
|
NumberParsing.cpp
|
||||||
String.cpp
|
String.cpp
|
||||||
|
Units.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
|
@ -467,4 +467,43 @@ Result<usize> cstyle_format(const char* format, callback_t callback, void* arg,
|
|||||||
}
|
}
|
||||||
|
|
||||||
return state.count;
|
return state.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct StringFormatInfo
|
||||||
|
{
|
||||||
|
char* buffer;
|
||||||
|
size_t remaining;
|
||||||
|
};
|
||||||
|
|
||||||
|
Result<usize> vstring_format(char* buf, size_t max, const char* format, va_list ap)
|
||||||
|
{
|
||||||
|
StringFormatInfo info = {.buffer = buf, .remaining = max - 1};
|
||||||
|
|
||||||
|
usize result = TRY(cstyle_format(
|
||||||
|
format,
|
||||||
|
[](char c, void* arg) -> Result<void> {
|
||||||
|
StringFormatInfo* info_arg = (StringFormatInfo*)arg;
|
||||||
|
if (!info_arg->remaining) return {};
|
||||||
|
*(info_arg->buffer) = c;
|
||||||
|
info_arg->buffer++;
|
||||||
|
info_arg->remaining--;
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
&info, ap));
|
||||||
|
|
||||||
|
*(info.buffer) = 0;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<usize> string_format(char* buf, size_t max, const char* format, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, format);
|
||||||
|
|
||||||
|
usize result = TRY(vstring_format(buf, max, format, ap));
|
||||||
|
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
@ -5,4 +5,6 @@
|
|||||||
|
|
||||||
typedef Result<void> (*callback_t)(char, void*);
|
typedef Result<void> (*callback_t)(char, void*);
|
||||||
|
|
||||||
Result<usize> cstyle_format(const char* format, callback_t callback, void* arg, va_list ap);
|
Result<usize> cstyle_format(const char* format, callback_t callback, void* arg, va_list ap);
|
||||||
|
Result<usize> vstring_format(char* buf, size_t max, const char* format, va_list ap);
|
||||||
|
Result<usize> string_format(char* buf, size_t max, const char* format, ...);
|
17
luna/Units.cpp
Normal file
17
luna/Units.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include <Format.h>
|
||||||
|
#include <Units.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
Result<usize> 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);
|
||||||
|
}
|
4
luna/Units.h
Normal file
4
luna/Units.h
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Result.h>
|
||||||
|
|
||||||
|
Result<usize> to_dynamic_unit(usize value, char* buffer, usize max);
|
Loading…
Reference in New Issue
Block a user