mirror of
https://git.stationery.faith/corn/corn.git
synced 2024-11-21 23:52:20 +00:00
display mem formatted
This commit is contained in:
parent
341c95f535
commit
ef384ad9a7
4 changed files with 90 additions and 4 deletions
2
Makefile
2
Makefile
|
@ -59,6 +59,6 @@ run: all
|
|||
-cdrom $(BUILD_DIR)/$(ISO_NAME) \
|
||||
-serial stdio \
|
||||
-display gtk,show-menubar=off,zoom-to-fit=on \
|
||||
-m 4G \
|
||||
-m 1G \
|
||||
-enable-kvm \
|
||||
-name corn
|
||||
|
|
|
@ -156,6 +156,14 @@ char *ultoa(unsigned long int n, char *buffer, int radix);
|
|||
*/
|
||||
char *ulltoa(unsigned long long int n, char *buffer, int radix);
|
||||
|
||||
/**
|
||||
* Converts a byte count to a human readable file size
|
||||
* @param bytes - the bytes to convert
|
||||
* @param buf - the buffer to store it in
|
||||
* @preturns - bus
|
||||
*/
|
||||
char *btoa(size_t bytes, char *buf);
|
||||
|
||||
/**
|
||||
* Converts the string in str to an int value based on the given base.
|
||||
* The endptr is updated to where the string was no longer valid.
|
||||
|
|
77
src/lib.c
77
src/lib.c
|
@ -128,6 +128,83 @@ int ctoi(char c) {
|
|||
}
|
||||
}
|
||||
|
||||
static char fs_endings[] = {'K', 'M', 'G', 'T', 'P'};
|
||||
|
||||
static size_t nth_pow(size_t i, size_t e) {
|
||||
size_t a = i;
|
||||
while(--e)
|
||||
a *= i;
|
||||
return a;
|
||||
}
|
||||
|
||||
static char nth_digit(size_t bytes, size_t place) {
|
||||
while (1) {
|
||||
if (bytes < nth_pow(10, place))
|
||||
break;
|
||||
bytes /= 10;
|
||||
}
|
||||
return bytes %= 10;
|
||||
}
|
||||
|
||||
char *btoa(const size_t bytes, char *buf) {
|
||||
|
||||
char *start = buf;
|
||||
|
||||
int idx = -1;
|
||||
|
||||
size_t b = bytes;
|
||||
size_t s = b;
|
||||
while (1) {
|
||||
if (b < 1000)
|
||||
break;
|
||||
s = b;
|
||||
b /= 1024;
|
||||
idx++;
|
||||
}
|
||||
|
||||
if (idx == -1) {
|
||||
ultoa(bytes, buf, 10);
|
||||
return start;
|
||||
}
|
||||
|
||||
s = s * 1000 / 1024;
|
||||
|
||||
if (s < 1024) {
|
||||
b = 1024;
|
||||
idx--;
|
||||
}
|
||||
|
||||
char fd = nth_digit(s, 1);
|
||||
char sd = nth_digit(s, 2);
|
||||
char td = nth_digit(s, 3);
|
||||
|
||||
*buf = fd + '0';
|
||||
buf++;
|
||||
|
||||
if (b < 10) {
|
||||
*buf = '.';
|
||||
buf++;
|
||||
}
|
||||
|
||||
*buf = sd + '0';
|
||||
buf++;
|
||||
|
||||
if (b < 10)
|
||||
goto end;
|
||||
|
||||
if (b > 99) {
|
||||
*buf = td + '0';
|
||||
buf++;
|
||||
}
|
||||
|
||||
end:
|
||||
|
||||
*buf = fs_endings[idx];
|
||||
buf++;
|
||||
*buf = '\0';
|
||||
return start;
|
||||
}
|
||||
|
||||
#define UXTOA(type, name) \
|
||||
char *name(unsigned type n, char *buffer, int radix) { \
|
||||
if (n == 0) { \
|
||||
|
|
|
@ -218,10 +218,11 @@ void memory_init(struct memory_map *map) {
|
|||
page_count -= bitmap_pages;
|
||||
free_memory = page_count * PAGE_SIZE;
|
||||
|
||||
char buf[20];
|
||||
kprintf("\nMEMORY USAGE\n");
|
||||
kprintf("mem total: %ld\n", memory_total());
|
||||
kprintf("mem free: %ld\n", memory_free());
|
||||
kprintf("mem used: %ld\n\n", memory_used());
|
||||
kprintf("mem total: %s\n", btoa(memory_total(), buf));
|
||||
kprintf("mem free: %s\n", btoa(memory_free(), buf));
|
||||
kprintf("mem used: %s\n\n", btoa(memory_used(), buf));
|
||||
|
||||
memory_unlock();
|
||||
|
||||
|
|
Loading…
Reference in a new issue