From ed2ced5caf060da96904d34dd24b3232142ab00b Mon Sep 17 00:00:00 2001 From: Yi-Ting Shih Date: Tue, 18 Mar 2025 10:11:37 +0800 Subject: [PATCH] Feat: add memalloc command --- include/shell.h | 1 + lib/kmalloc.c | 3 +-- lib/shell.c | 23 +++++++++++++++++------ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/shell.h b/include/shell.h index 18739d4..3c6ccc8 100644 --- a/include/shell.h +++ b/include/shell.h @@ -6,6 +6,7 @@ void help(void); void hello(void); void hwinfo(void); +void memalloc(size_t size); void ls(file_node_t *root); void cat(file_node_t *root, const char *filename); void reboot(void); diff --git a/lib/kmalloc.c b/lib/kmalloc.c index 3455b60..d5399ed 100644 --- a/lib/kmalloc.c +++ b/lib/kmalloc.c @@ -16,8 +16,7 @@ void *simple_alloc(size_t size) _heap_top = (void *)&__heap_start; } - if (size & 0xff) - size = (size & ~0xff) + 0x100; + size = (size_t)ALIGN8(size); if ((uint64_t)_heap_top + size >= (uint64_t)&__heap_end) exit(ERR_NO_MEM); diff --git a/lib/shell.c b/lib/shell.c index a008e48..1b6b595 100644 --- a/lib/shell.c +++ b/lib/shell.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -15,12 +16,13 @@ void help (void) { uart_puts( - "help : print this help menu" ENDL - "hello : print Hello World!" ENDL - "hwinfo: print hardware info" ENDL - "ls : list directory contents" ENDL - "cat : concatenate files and print" ENDL - "reboot: reboot the device" ENDL + "help : print this help menu" ENDL + "hello : print Hello World!" ENDL + "hwinfo : print hardware info" ENDL + "memalloc : alloate memory and print" ENDL + "ls : list directory contents" ENDL + "cat : concatenate files and print" ENDL + "reboot : reboot the device" ENDL ); } @@ -49,6 +51,13 @@ void hwinfo (void) uart_puts(ENDL); } +void memalloc(size_t size) +{ + DEBUG(size); + void *addr = kmalloc(size); + DEBUG(addr); +} + void ls_initrd_callback(file_node_t *tr) { uart_puts(tr->filename); @@ -118,6 +127,8 @@ int shell(file_node_t *initrd_root) hello(); } else if (!strcmp(bin, "hwinfo")) { hwinfo(); + } else if (!strcmp(bin, "memalloc")){ + memalloc((size_t)atoi32(param)); } else if (!strcmp(bin, "ls")) { ls(initrd_root); } else if (!strcmp(bin, "cat")) {