mirror of
https://github.com/usatiuk/ficus.git
synced 2025-10-29 00:27:52 +01:00
format bytes prettily in free_printer
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
//
|
||||
#include <cstddef>
|
||||
|
||||
#include "BytesFormatter.hpp"
|
||||
#include "LockGuard.hpp"
|
||||
#include "MemFs.hpp"
|
||||
#include "MountTable.hpp"
|
||||
@@ -72,19 +73,19 @@ void freeprinter() {
|
||||
String buf;
|
||||
buf += "=====\n";
|
||||
buf += "Free mem: ";
|
||||
buf += get_free() * 1024;
|
||||
buf += BytesFormatter::formatStr(get_free() * 1024);
|
||||
buf += "\n";
|
||||
GlobalTtyManager.all_tty_putstr(buf.c_str());
|
||||
buf = "";
|
||||
|
||||
buf += "Heap allocated: ";
|
||||
buf += get_heap_allocated();
|
||||
buf += BytesFormatter::formatStr(get_heap_allocated());
|
||||
buf += "\n";
|
||||
GlobalTtyManager.all_tty_putstr(buf.c_str());
|
||||
buf = "";
|
||||
|
||||
buf += "Heap used: ";
|
||||
buf += get_heap_used();
|
||||
buf += BytesFormatter::formatStr(get_heap_used());
|
||||
buf += "\n";
|
||||
buf += "=====\n";
|
||||
GlobalTtyManager.all_tty_putstr(buf.c_str());
|
||||
|
||||
@@ -28,7 +28,7 @@ void setup_syscalls() {
|
||||
union {
|
||||
STAR star;
|
||||
uint64_t bytes;
|
||||
} newstar{};
|
||||
} __attribute__((__packed__)) newstar{};
|
||||
|
||||
newstar.star.ret_cs_ss = (GDTSEL(gdt_data_user) - 8) | 0x3;
|
||||
assert(newstar.star.ret_cs_ss + 8 == (GDTSEL(gdt_data_user) | 0x3));
|
||||
|
||||
45
src/kernel/BytesFormatter.cpp
Normal file
45
src/kernel/BytesFormatter.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 22.03.2024.
|
||||
//
|
||||
|
||||
#include "BytesFormatter.hpp"
|
||||
|
||||
static void print2dec(String &out, uint64_t what) {
|
||||
uint64_t after_dot = what % 100;
|
||||
what /= 100;
|
||||
out += what;
|
||||
out += ".";
|
||||
out += after_dot;
|
||||
}
|
||||
|
||||
BytesFormatter::BytesFormat BytesFormatter::format(unsigned long long int bytes) {
|
||||
String outNum;
|
||||
|
||||
if (bytes > 1024ULL * 1024 * 1024 * 1024) {
|
||||
print2dec(outNum, bytes / (1024ULL * 1024ULL * 1024ULL * 10ULL));
|
||||
return {std::move(outNum), "TiB"};
|
||||
}
|
||||
if (bytes > 1024ULL * 1024 * 1024) {
|
||||
print2dec(outNum, bytes / (1024ULL * 1024ULL * 10ULL));
|
||||
return {std::move(outNum), "GiB"};
|
||||
}
|
||||
if (bytes > 1024ULL * 1024) {
|
||||
print2dec(outNum, bytes / (1024ULL * 10ULL));
|
||||
return {std::move(outNum), "MiB"};
|
||||
}
|
||||
if (bytes > 1024ULL) {
|
||||
print2dec(outNum, bytes / (10ULL));
|
||||
return {std::move(outNum), "KiB"};
|
||||
}
|
||||
outNum += bytes;
|
||||
return {std::move(outNum), "Bytes"};
|
||||
}
|
||||
|
||||
String BytesFormatter::formatStr(unsigned long long int bytes) {
|
||||
auto fmt = format(bytes);
|
||||
String out;
|
||||
out += fmt.number;
|
||||
out += " ";
|
||||
out += fmt.prefix;
|
||||
return out;
|
||||
}
|
||||
31
src/kernel/BytesFormatter.hpp
Normal file
31
src/kernel/BytesFormatter.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 22.03.2024.
|
||||
//
|
||||
|
||||
#ifndef OS2_BYTESFORMATTER_HPP
|
||||
#define OS2_BYTESFORMATTER_HPP
|
||||
|
||||
#include "String.hpp"
|
||||
|
||||
/// Utility class to format byte values according to their magnitude
|
||||
class BytesFormatter {
|
||||
public:
|
||||
/// Structure for returning the processed byte value
|
||||
struct BytesFormat {
|
||||
String number;///< Number part of the value
|
||||
String prefix;///< Unit of measure
|
||||
};
|
||||
|
||||
/// Formats the bytes in BytesFormat format
|
||||
/// \param bytes Number of bytes
|
||||
/// \return BytesFormat value
|
||||
static BytesFormat format(unsigned long long bytes);
|
||||
|
||||
/// Formats the bytes into a string
|
||||
/// \param bytes Number of bytes
|
||||
/// \return String, consisting of the scaled number and the unit of measure separated by a space
|
||||
static String formatStr(unsigned long long bytes);
|
||||
};
|
||||
|
||||
|
||||
#endif//OS2_BYTESFORMATTER_HPP
|
||||
@@ -1,6 +1,18 @@
|
||||
target_include_directories(kernel.elf PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_sources(kernel.elf PRIVATE mutex.cpp cppsupport.cpp Spinlock.cpp LockGuard.cpp rand.cpp VMA.cpp asserts.cpp TtyManager.cpp Tty.cpp cv.cpp)
|
||||
target_sources(kernel.elf PRIVATE
|
||||
mutex.cpp
|
||||
cppsupport.cpp
|
||||
Spinlock.cpp
|
||||
LockGuard.cpp
|
||||
rand.cpp
|
||||
VMA.cpp
|
||||
asserts.cpp
|
||||
TtyManager.cpp
|
||||
Tty.cpp
|
||||
cv.cpp
|
||||
BytesFormatter.cpp
|
||||
)
|
||||
|
||||
add_subdirectory(templates)
|
||||
add_subdirectory(vfs)
|
||||
Reference in New Issue
Block a user