feat: Add freestanding C++17 OOP kernel subsystem and interactive Snake Game

- Add freestanding C++17 runtime with operator new/delete, pure virtual handlers, and global constructor execution in kernel/cpp/cxx_runtime.cpp
- Implement C++ Object-Oriented telemetry and hardware services with polymorphism in kernel/cpp/cpp_module.cpp and include/kernel/cpp_kernel.h
- Add interactive ANSI Snake Game in userland/lazybox/snake.c with real-time keyboard control and automated simulation mode
- Initialize C++ subsystem during kernel boot step [8/14] in kernel/main.c
- Add LazyBox applets: 'snake', 'cppinfo', and 'cpptest'
This commit is contained in:
subhobhai943 committed 2026-08-18 21:14:16 +05:30
1 parent f3b47f9d22
commit 6eaf9397a2
10 files changed
+636 -6

No files matched your search

+81
View File
@@ -0,0 +1,81 @@
// Freestanding C++ Runtime for SUB-OS Kernel
// Provides operator new/delete, pure virtual error handling, and global constructor calling
#include <kernel/cpp_kernel.h>
extern "C" {
#include <mm/kmalloc.h>
#include <kernel/printk.h>
#include <kernel/panic.h>
#include <lib/string.h>
void* __dso_handle = nullptr;
typedef void (*ctor_fn_t)(void);
extern ctor_fn_t __init_array_start[];
extern ctor_fn_t __init_array_end[];
void cpp_call_global_constructors(void) {
if (__init_array_end <= __init_array_start) return;
size_t count = __init_array_end - __init_array_start;
for (size_t i = 0; i < count; i++) {
if (__init_array_start[i]) {
__init_array_start[i]();
}
}
}
int __cxa_atexit(void (*/*destructor*/)(void*), void* /*arg*/, void* /*dso*/) {
return 0; // Kernel runs indefinitely until shutdown
}
void __cxa_pure_virtual(void) {
panic("C++ Pure Virtual Function Called");
}
}
// Freestanding Dynamic Memory Allocation Operators
void* operator new(size_t size) {
if (size == 0) size = 1;
void* ptr = kmalloc(size);
return ptr;
}
void* operator new[](size_t size) {
if (size == 0) size = 1;
void* ptr = kmalloc(size);
return ptr;
}
// Placement new
void* operator new(size_t /*size*/, void* ptr) noexcept {
return ptr;
}
void* operator new[](size_t /*size*/, void* ptr) noexcept {
return ptr;
}
void operator delete(void* ptr) noexcept {
if (ptr) {
kfree(ptr);
}
}
void operator delete[](void* ptr) noexcept {
if (ptr) {
kfree(ptr);
}
}
void operator delete(void* ptr, size_t /*size*/) noexcept {
if (ptr) {
kfree(ptr);
}
}
void operator delete[](void* ptr, size_t /*size*/) noexcept {
if (ptr) {
kfree(ptr);
}
}