From 6e49438a182c8682e3063d5bfd745634509c3467 Mon Sep 17 00:00:00 2001 From: Anon2261 <182589283+Anon23261@users.noreply.github.com> Date: Thu, 5 Dec 2024 04:12:55 -0700 Subject: [PATCH] Fixed build issues: memory allocation, terminal functions, and type mismatches --- kernel/compiler.cpp | 38 +++++++++----------------------------- kernel/editor.cpp | 11 +++++++---- kernel/kernel.cpp | 3 +++ kernel/memory.cpp | 11 +++++++++-- 4 files changed, 28 insertions(+), 35 deletions(-) diff --git a/kernel/compiler.cpp b/kernel/compiler.cpp index 35fd613..a4a2a27 100644 --- a/kernel/compiler.cpp +++ b/kernel/compiler.cpp @@ -1,32 +1,6 @@ #include "compiler.h" #include "memory.h" - -// Custom implementations for bare metal environment -extern "C" { - void* memcpy(void* dest, const void* src, size_t n) { - char* d = (char*)dest; - const char* s = (const char*)src; - while (n--) *d++ = *s++; - return dest; - } - - void* memset(void* dest, int val, size_t n) { - unsigned char* ptr = (unsigned char*)dest; - while (n--) *ptr++ = (unsigned char)val; - return dest; - } - - int memcmp(const void* s1, const void* s2, size_t n) { - const unsigned char* p1 = (const unsigned char*)s1; - const unsigned char* p2 = (const unsigned char*)s2; - while (n--) { - if (*p1 != *p2) return *p1 - *p2; - p1++; - p2++; - } - return 0; - } -} +#include // Simple assembly instruction structure struct Instruction { @@ -47,6 +21,12 @@ enum OpCode { OP_INT = 0x08 }; +extern "C" { + void* memcpy(void* dest, const void* src, size_t n); + void* memset(void* dest, int val, size_t n); + int memcmp(const void* s1, const void* s2, size_t n); +} + static unsigned char* output_buffer = nullptr; static size_t output_size = 0; @@ -153,7 +133,7 @@ bool execute_binary(const unsigned char* binary, size_t size) { if (!binary || size == 0) return false; // Create executable memory page - void* exec_mem = malloc(size); + void* exec_mem = memory_allocate(size); if (!exec_mem) return false; // Copy binary to executable memory @@ -165,6 +145,6 @@ bool execute_binary(const unsigned char* binary, size_t size) { func(); // Clean up - free(exec_mem); + memory_free(exec_mem); return true; } diff --git a/kernel/editor.cpp b/kernel/editor.cpp index ee49dcb..bc5c2e9 100644 --- a/kernel/editor.cpp +++ b/kernel/editor.cpp @@ -24,7 +24,10 @@ void editor_init() { E.cursor_x = 0; E.cursor_y = 0; E.filename = nullptr; - terminal_get_size(&E.screen_rows, &E.screen_cols); + size_t rows, cols; + terminal_get_size(&rows, &cols); + E.screen_rows = rows; + E.screen_cols = cols; if (E.screen_rows == 0) E.screen_rows = 24; if (E.screen_cols == 0) E.screen_cols = 80; E.screen_rows--; // Make room for status line @@ -140,11 +143,11 @@ void editor_refresh_screen() { // Status line char status[80]; - int len = snprintf(status, sizeof(status), "%.20s - %zu bytes %d,%d", + int len = snprintf(status, sizeof(status), "%.20s - %zu bytes %zu,%zu", E.filename ? E.filename : "[No Name]", E.buffer_size, - (int)E.cursor_y + 1, - (int)E.cursor_x + 1); + E.cursor_y + 1, + E.cursor_x + 1); if (len > (int)E.screen_cols) len = E.screen_cols; diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index 78053d4..8fa1d5e 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -12,6 +12,9 @@ extern "C" { extern void* memset(void* s, int c, size_t n); } +// External functions +extern void int_to_string(int value, char* str); + // Global kernel state KernelState kernel_state = { .terminal_row = 0, diff --git a/kernel/memory.cpp b/kernel/memory.cpp index 39461f2..2f8449c 100644 --- a/kernel/memory.cpp +++ b/kernel/memory.cpp @@ -123,6 +123,13 @@ size_t memory_get_total() { } size_t memory_get_free() { - // TODO: Implement actual free memory tracking - return 32 * 1024 * 1024; // Return 32MB for now + return HEAP_SIZE - sizeof(struct block_meta); +} + +extern "C" void* memory_allocate(size_t size) { + return malloc(size); +} + +extern "C" void memory_free(void* ptr) { + free(ptr); }