diff --git a/Makefile b/Makefile index cb3a85b..2e58dd1 100644 --- a/Makefile +++ b/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 diff --git a/include/lib.h b/include/lib.h index 3be1f8b..d808833 100644 --- a/include/lib.h +++ b/include/lib.h @@ -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. diff --git a/src/lib.c b/src/lib.c index 7f3d79f..e298b00 100644 --- a/src/lib.c +++ b/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) { \ diff --git a/src/memory/physalloc.c b/src/memory/physalloc.c index bcbc66d..71df848 100644 --- a/src/memory/physalloc.c +++ b/src/memory/physalloc.c @@ -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();