From d8eaa13f2efc1eeca4e5f230d5a2d535aaf27338 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Fri, 2 Jan 2026 00:47:16 +0800 Subject: dev(file-system): establish VFS and Read-only FAT32 File System --- src/kernel/cxx.cc | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/kernel/cxx.cc (limited to 'src/kernel/cxx.cc') diff --git a/src/kernel/cxx.cc b/src/kernel/cxx.cc new file mode 100644 index 0000000..f9541b5 --- /dev/null +++ b/src/kernel/cxx.cc @@ -0,0 +1,68 @@ +#include "common/types.h" +#include "kernel/pmm.h" +#include "drivers/uart.h" + +/* + * C++ Kernel Runtime Support + * + * * Strategy: Map directly to PMM::alloc_page(). + * Pros: Simple, works immediately for VFS. + * Cons: Allocates 4KB for every object (wasteful for small objects), + * but acceptable for current stage. + */ + +void *operator new(unsigned long size) +{ + // Safety check: PMM allocates 4096 bytes max per call + if (size > 4096) + { + Drivers::uart_puts("panic: new allocation too big (max 4096)\n"); + while (1) + ; + } + + void *p = PMM::alloc_page(); + if (!p) + { + Drivers::uart_puts("panic: new out of memory\n"); + while (1) + ; + } + // PMM::alloc_page returns zeroed memory usually, + // but standard 'new' doesn't guarantee it. + // The constructor will handle initialization. + return p; +} + +void *operator new[](unsigned long size) +{ + return operator new(size); +} + +void operator delete(void *p) +{ + if (p) + PMM::free_page(p); +} + +void operator delete[](void *p) +{ + if (p) + PMM::free_page(p); +} + +// C++14 Sized Deallocation (Required by modern compilers) +// The compiler optimizes 'delete' calls to pass the size of the object +void operator delete(void *p, unsigned long size) +{ + (void)size; // We ignore size because PMM always frees a full page + if (p) + PMM::free_page(p); +} + +void operator delete[](void *p, unsigned long size) +{ + (void)size; + if (p) + PMM::free_page(p); +} \ No newline at end of file -- cgit v1.2.3