Skip to content

Commit

Permalink
Fixed build issues: memory allocation, terminal functions, and type m…
Browse files Browse the repository at this point in the history
…ismatches
  • Loading branch information
Anon23261 committed Dec 5, 2024
1 parent 63962fe commit 6e49438
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
38 changes: 9 additions & 29 deletions kernel/compiler.cpp
Original file line number Diff line number Diff line change
@@ -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 <string.h>

// Simple assembly instruction structure
struct Instruction {
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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;
}
11 changes: 7 additions & 4 deletions kernel/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions kernel/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions kernel/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 6e49438

Please sign in to comment.