Memory fixes userspace

This commit is contained in:
boreddevnl
2026-02-26 21:09:18 +01:00
parent 786eac0345
commit 2801dbc21f
57 changed files with 215 additions and 183 deletions

View File

@@ -86,12 +86,12 @@ static void update_display(void) {
static void calculator_paint(void) {
int w = 180;
int h = 230;
ui_draw_rect(win_calculator, 4, 30, w - 8, h - 34, COLOR_DARK_BG);
ui_draw_rounded_rect_filled(win_calculator, 10, 36, w - 20, 25, 6, COLOR_DARK_PANEL);
ui_draw_rect(win_calculator, 4, 4, w - 8, h - 34, COLOR_DARK_BG);
ui_draw_rounded_rect_filled(win_calculator, 10, 10, w - 20, 25, 6, COLOR_DARK_PANEL);
int text_w = display_buf_len * 8;
int text_x = w - 15 - text_w;
ui_draw_string(win_calculator, text_x, 44, display_buffer, COLOR_DARK_TEXT);
ui_draw_string(win_calculator, text_x, 18, display_buffer, COLOR_DARK_TEXT);
const char *labels[] = {
"C", "sqr", "rt", "/",
@@ -105,7 +105,7 @@ static void calculator_paint(void) {
int bh = 25;
int gap = 5;
int start_x = 10;
int start_y = 70;
int start_y = 40;
for (int i = 0; i < 20; i++) {
int r = i / 4;
@@ -136,7 +136,7 @@ static void calculator_click(int x, int y) {
int bh = 25;
int gap = 5;
int start_x = 10;
int start_y = 65; // Matches the hitboxes
int start_y = 35; // Matches the hitboxes
for (int i = 0; i < 20; i++) {
int r = i / 4;

Binary file not shown.

View File

@@ -4,7 +4,6 @@ int main() {
const char* msg = "Attempting to crash via null dereference...\n";
sys_write(1, msg, 45);
// Null pointer dereference (should not crash the system and instead get this process killed)
volatile int* p = (int*)0;
*p = 123;

Binary file not shown.

Binary file not shown.

View File

@@ -39,3 +39,8 @@ void ui_mark_dirty(ui_window_t win, int x, int y, int w, int h) {
uint64_t params[4] = { (uint64_t)x, (uint64_t)y, (uint64_t)w, (uint64_t)h };
syscall3(SYS_GUI, GUI_CMD_MARK_DIRTY, (uint64_t)win, (uint64_t)params);
}
void ui_draw_image(ui_window_t win, int x, int y, int w, int h, uint32_t *image_data) {
uint64_t params[4] = { (uint64_t)x, (uint64_t)y, (uint64_t)w, (uint64_t)h };
syscall4(SYS_GUI, GUI_CMD_DRAW_IMAGE, (uint64_t)win, (uint64_t)params, (uint64_t)image_data);
}

View File

@@ -11,6 +11,7 @@
#define GUI_CMD_MARK_DIRTY 4
#define GUI_CMD_GET_EVENT 5
#define GUI_CMD_DRAW_ROUNDED_RECT_FILLED 6
#define GUI_CMD_DRAW_IMAGE 7
// Event Types
#define GUI_EVENT_NONE 0
@@ -42,5 +43,6 @@ void ui_draw_rect(ui_window_t win, int x, int y, int w, int h, uint32_t color);
void ui_draw_rounded_rect_filled(ui_window_t win, int x, int y, int w, int h, int radius, uint32_t color);
void ui_draw_string(ui_window_t win, int x, int y, const char *str, uint32_t color);
void ui_mark_dirty(ui_window_t win, int x, int y, int w, int h);
void ui_draw_image(ui_window_t win, int x, int y, int w, int h, uint32_t *image_data);
#endif

Binary file not shown.

View File

@@ -24,9 +24,6 @@ static BlockMeta *find_free_block(BlockMeta **last, size_t size) {
static BlockMeta *request_space(BlockMeta* last, size_t size) {
BlockMeta *block;
block = (BlockMeta *)sys_sbrk(0);
// Ask for space, ensuring everything stays 8-byte aligned if sizes are odd.
// For simplicity, we just request exactly what is needed,
// but typically `size` should be aligned.
size_t align = 8;
if (size % align != 0) {
size += align - (size % align);
@@ -70,7 +67,6 @@ void *malloc(size_t size) {
if (!block) return NULL;
} else { // Found free block
block->free = 0;
// We could split the block here if block->size is much larger than size...
}
}

View File

@@ -59,11 +59,10 @@ uint64_t syscall5(uint64_t sys_num, uint64_t arg1, uint64_t arg2, uint64_t arg3,
return ret;
}
// C-Friendly Wrappers
void sys_exit(int status) {
syscall1(SYS_EXIT, (uint64_t)status);
while (1); // Halt
while (1);
}
int sys_write(int fd, const char *buf, int len) {

View File

@@ -235,7 +235,7 @@ static void md_draw_text_bold(ui_window_t win, int x, int y, const char *text, u
static void md_paint(ui_window_t win) {
int offset_x = 4;
int offset_y = 24;
int offset_y = 0;
int content_width = win_w - 8;
int content_height = win_h - 28;
@@ -392,7 +392,7 @@ static void md_handle_key(char c) {
static void md_handle_click(int x, int y) {
int content_width = win_w - 8;
int btn_x_up = 4 + content_width - 50;
int btn_y = 24 + 2;
int btn_y = 2;
if (x >= btn_x_up && x < btn_x_up + 20 && y >= btn_y && y < btn_y + 16) {
scroll_top -= 3;
if (scroll_top < 0) scroll_top = 0;
@@ -423,15 +423,15 @@ int main(int argc, char **argv) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
md_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
ui_mark_dirty(win, 0, 0, win_w, win_h - 20);
} else if (ev.type == GUI_EVENT_CLICK) {
md_handle_click(ev.arg1, ev.arg2);
md_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
ui_mark_dirty(win, 0, 0, win_w, win_h - 20);
} else if (ev.type == GUI_EVENT_KEY) {
md_handle_key((char)ev.arg1);
md_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
ui_mark_dirty(win, 0, 0, win_w, win_h - 20);
} else if (ev.type == GUI_EVENT_CLOSE) {
sys_exit(0);
}

Binary file not shown.

View File

@@ -26,14 +26,13 @@ static void debug_print(const char *msg) {
#define CELL_SIZE 20
// Game state
static int grid[GRID_HEIGHT][GRID_WIDTH]; // -1 = mine, 0-8 = adjacent mine count
static int grid[GRID_HEIGHT][GRID_WIDTH];
static bool revealed[GRID_HEIGHT][GRID_WIDTH];
static bool flagged[GRID_HEIGHT][GRID_WIDTH];
static bool game_over = false;
static bool game_won = false;
static int revealed_count = 0;
// Helper: Random number generator (simple LCG)
static uint32_t random_seed = 12345;
static uint32_t random_next(void) {
random_seed = random_seed * 1103515245 + 12345;
@@ -120,10 +119,8 @@ static void reveal_cell(int x, int y) {
}
}
} else if (grid[y][x] == 0) {
// Empty cell - flood fill
flood_fill(x, y);
} else {
// Numbered cell
revealed[y][x] = true;
revealed_count++;
}
@@ -144,21 +141,19 @@ static void flag_cell(int x, int y) {
static void minesweeper_paint(ui_window_t win) {
int win_w = 240, win_h = 340;
// Background - dark mode
ui_draw_rect(win, 4, 30, win_w - 8, win_h - 34, COLOR_DARK_BG);
ui_draw_rect(win, 4, 0, win_w - 8, win_h - 34, COLOR_DARK_BG);
// Game status
if (game_over) {
ui_draw_string(win, 10, 36, "Game Over!", COLOR_TRAFFIC_RED);
ui_draw_string(win, 10, 6, "Game Over!", COLOR_TRAFFIC_RED);
} else if (game_won) {
ui_draw_string(win, 10, 36, "You Won!", 0xFF00FF00); // Bright green
ui_draw_string(win, 10, 6, "You Won!", 0xFF00FF00); // Bright green
} else {
ui_draw_string(win, 10, 36, "", COLOR_DARK_TEXT);
ui_draw_string(win, 10, 6, "", COLOR_DARK_TEXT);
}
// Draw grid
int grid_start_x = 10;
int grid_start_y = 56;
int grid_start_y = 26;
for (int y = 0; y < GRID_HEIGHT; y++) {
for (int x = 0; x < GRID_WIDTH; x++) {
@@ -196,7 +191,7 @@ static void minesweeper_paint(ui_window_t win) {
static void minesweeper_handle_click(ui_window_t win, int x, int y, int button) {
int grid_start_x = 10;
int grid_start_y = 56;
int grid_start_y = 26;
int btn_y = grid_start_y + GRID_HEIGHT * CELL_SIZE + 10;
// Check "New Game" button
@@ -231,7 +226,6 @@ int main(int argc, char **argv) {
ui_window_t win = ui_window_create("Minesweeper", 250, 100, 240, 340);
if (!win) return 1;
// Use current time or something for seed? No syscall for time right now.
random_seed = 987654321;
init_game();
@@ -240,17 +234,17 @@ int main(int argc, char **argv) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
minesweeper_paint(win);
ui_mark_dirty(win, 0, 0, 240, 340);
ui_mark_dirty(win, 0, 0, 240, 320);
} else if (ev.type == GUI_EVENT_CLICK) {
debug_print("[MINESWEEPER] LEFT CLICK");
minesweeper_handle_click(win, ev.arg1, ev.arg2, ev.type);
minesweeper_paint(win);
ui_mark_dirty(win, 0, 0, 240, 340);
ui_mark_dirty(win, 0, 0, 240, 320);
} else if (ev.type == GUI_EVENT_RIGHT_CLICK) {
debug_print("[MINESWEEPER] RIGHT CLICK DETECTED");
minesweeper_handle_click(win, ev.arg1, ev.arg2, ev.type);
minesweeper_paint(win);
ui_mark_dirty(win, 0, 0, 240, 340);
ui_mark_dirty(win, 0, 0, 240, 320);
} else if (ev.type == GUI_EVENT_CLOSE) {
sys_exit(0);
}

Binary file not shown.

View File

@@ -241,8 +241,7 @@ int main(int argc, char **argv) {
sys_exit(0);
}
} else {
// Optional: sys_yield() or similar to avoid high CPU
// For now, just keep looping but it's better than nothing
for(volatile int i=0; i<10000; i++);
}
}

Binary file not shown.

View File

@@ -50,24 +50,17 @@ static void paint_reset(void) {
static void paint_paint(ui_window_t win) {
int canvas_x = 60;
int canvas_y = 30;
int canvas_y = 0;
// Canvas Area with dark background and rounded corners (draw first so it's behind everything)
ui_draw_rounded_rect_filled(win, canvas_x - 2, canvas_y - 2, CANVAS_W + 4, CANVAS_H + 4, 4, COLOR_DARK_BG);
// Toolbar area - dark mode
ui_draw_rounded_rect_filled(win, 10, 30, 40, 260 - 40, 6, COLOR_DARK_PANEL);
ui_draw_rounded_rect_filled(win, 10, 0, 40, 230, 6, COLOR_DARK_PANEL);
// Color Palette with rounded corners
uint32_t colors[] = {COLOR_BLACK, COLOR_RED, COLOR_APPLE_GREEN, COLOR_APPLE_BLUE, COLOR_APPLE_YELLOW, COLOR_WHITE};
for (int i = 0; i < 6; i++) {
int cy = 40 + (i * 25);
int cy = 10 + (i * 25);
ui_draw_rounded_rect_filled(win, 15, cy, 30, 20, 3, colors[i]);
// Highlight selected color with border
if (current_color == colors[i]) {
// Note: libui might not have draw_rounded_rect (hollow), so we just draw four lines simulating it
// or we use ui_draw_rect for hollow border
ui_draw_rect(win, 13, cy - 2, 34, 1, COLOR_DARK_TEXT);
ui_draw_rect(win, 13, cy - 2 + 24, 34, 1, COLOR_DARK_TEXT);
ui_draw_rect(win, 13, cy - 2, 1, 24, COLOR_DARK_TEXT);
@@ -75,12 +68,11 @@ static void paint_paint(ui_window_t win) {
}
}
// Toolbar Buttons - dark mode with rounded corners
ui_draw_rounded_rect_filled(win, 12, 260 - 65, 36, 20, 4, COLOR_DARK_BORDER);
ui_draw_string(win, 18, 260 - 58, "CLR", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, 12, 230 - 65, 36, 20, 4, COLOR_DARK_BORDER);
ui_draw_string(win, 18, 230 - 58, "CLR", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, 12, 260 - 40, 36, 20, 4, COLOR_DARK_BORDER);
ui_draw_string(win, 18, 260 - 33, "SAV", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, 12, 230 - 40, 36, 20, 4, COLOR_DARK_BORDER);
ui_draw_string(win, 18, 230 - 33, "SAV", COLOR_DARK_TEXT);
// Draw canvas content
if (canvas_buffer) {
@@ -93,7 +85,7 @@ static void paint_paint(ui_window_t win) {
}
}
static void paint_put_brush(ui_window_t win, int cx, int cy) {
static void paint_put_brush(ui_window_t win, int cx, int cy, int *min_x, int *min_y, int *max_x, int *max_y) {
if (!canvas_buffer) return;
for (int dy = 0; dy < 2; dy++) {
for (int dx = 0; dx < 2; dx++) {
@@ -101,26 +93,32 @@ static void paint_put_brush(ui_window_t win, int cx, int cy) {
int py = cy + dy;
if (px >= 0 && px < CANVAS_W && py >= 0 && py < CANVAS_H) {
canvas_buffer[py * CANVAS_W + px] = current_color;
ui_draw_rect(win, 60 + px, 30 + py, 1, 1, current_color);
ui_draw_rect(win, 60 + px, 0 + py, 1, 1, current_color);
if (px < *min_x) *min_x = px;
if (py < *min_y) *min_y = py;
if (px > *max_x) *max_x = px;
if (py > *max_y) *max_y = py;
}
}
}
ui_mark_dirty(win, 60 + cx, 30 + cy, 2, 2);
}
void paint_handle_mouse(ui_window_t win, int x, int y) {
int cx = x - 60;
int cy = y - 30;
int cy = y;
if (cx < 0 || cx >= CANVAS_W || cy < 0 || cy >= CANVAS_H) {
last_mx = -1;
return;
}
int min_x = CANVAS_W, min_y = CANVAS_H;
int max_x = -1, max_y = -1;
if (last_mx == -1) {
paint_put_brush(win, cx, cy);
paint_put_brush(win, cx, cy, &min_x, &min_y, &max_x, &max_y);
} else {
// Bresenham's line algorithm to fill gaps between points
int x0 = last_mx, y0 = last_my;
int x1 = cx, y1 = cy;
int dx = (x1 - x0 > 0) ? (x1 - x0) : (x0 - x1);
@@ -130,13 +128,18 @@ void paint_handle_mouse(ui_window_t win, int x, int y) {
int err = dx - dy;
while (1) {
paint_put_brush(win, x0, y0);
paint_put_brush(win, x0, y0, &min_x, &min_y, &max_x, &max_y);
if (x0 == x1 && y0 == y1) break;
int e2 = 2 * err;
if (e2 > -dy) { err -= dy; x0 += sx; }
if (e2 < dx) { err += dx; y0 += sy; }
}
}
if (min_x <= max_x && min_y <= max_y) {
ui_mark_dirty(win, 60 + min_x, 0 + min_y, (max_x - min_x) + 1, (max_y - min_y) + 1);
}
last_mx = cx;
last_my = cy;
}
@@ -146,11 +149,9 @@ void paint_reset_last_pos(void) {
last_my = -1;
}
// Simple window message dialog wrapper using syscall
static void wm_show_message(const char *title, const char *msg) {
// Wait, userland doesn't have wm_show_message syscall available yet, or maybe it does?
// We didn't add it or GUI_EVENT doesn't support it directly.
// For now we do nothing, or just open a small window.
}
static void paint_save(const char *path) {
@@ -181,13 +182,13 @@ void paint_load(const char *path) {
static void paint_click(ui_window_t win, int x, int y) {
// Check Buttons
if (x >= 12 && x < 48) {
if (y >= 260 - 65 && y < 260 - 45) {
if (y >= 230 - 65 && y < 230 - 45) {
paint_reset();
paint_paint(win);
ui_mark_dirty(win, 0, 0, 380, 260);
ui_mark_dirty(win, 0, 0, 380, 230);
return;
}
if (y >= 260 - 40 && y < 260 - 20) {
if (y >= 230 - 40 && y < 230 - 20) {
paint_save(current_file_path);
return;
}
@@ -196,12 +197,12 @@ static void paint_click(ui_window_t win, int x, int y) {
// Check Palette
if (x >= 15 && x < 45) {
for (int i = 0; i < 6; i++) {
int cy = 40 + (i * 25);
int cy = 10 + (i * 25);
if (y >= cy && y < cy + 20) {
uint32_t colors[] = {COLOR_BLACK, COLOR_RED, COLOR_APPLE_GREEN, COLOR_APPLE_BLUE, COLOR_APPLE_YELLOW, COLOR_WHITE};
current_color = colors[i];
paint_paint(win);
ui_mark_dirty(win, 0, 0, 380, 260);
ui_mark_dirty(win, 0, 0, 380, 230);
return;
}
}
@@ -227,11 +228,9 @@ int main(int argc, char **argv) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
paint_paint(win);
ui_mark_dirty(win, 0, 0, 380, 260);
ui_mark_dirty(win, 0, 0, 380, 240);
} else if (ev.type == GUI_EVENT_CLICK) {
paint_click(win, ev.arg1, ev.arg2);
paint_paint(win);
ui_mark_dirty(win, 0, 0, 380, 260);
} else if (ev.type == GUI_EVENT_MOUSE_DOWN) {
paint_handle_mouse(win, ev.arg1, ev.arg2);
} else if (ev.type == GUI_EVENT_MOUSE_UP) {

Binary file not shown.

View File

@@ -689,3 +689,4 @@ int main(int argc, char **argv) {
}
return 0;
}

Binary file not shown.

View File

@@ -197,7 +197,7 @@ static void editor_insert_char(char ch) {
static void editor_paint(ui_window_t win) {
int offset_x = 4;
int offset_y = 24;
int offset_y = 0;
int content_width = win_w - 8;
int content_height = win_h - 28;
@@ -390,7 +390,7 @@ static void editor_handle_key(char c) {
static void editor_handle_click(int x, int y) {
int content_width = win_w - 8;
int button_x = 4 + content_width - 80;
int button_y = 24 + 3;
int button_y = 3;
if (x >= button_x && x < button_x + 70 && y >= button_y && y < button_y + 20) {
editor_save_file();
@@ -413,15 +413,15 @@ int main(int argc, char **argv) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
editor_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
ui_mark_dirty(win, 0, 0, win_w, win_h - 20);
} else if (ev.type == GUI_EVENT_CLICK) {
editor_handle_click(ev.arg1, ev.arg2);
editor_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
ui_mark_dirty(win, 0, 0, win_w, win_h - 20);
} else if (ev.type == GUI_EVENT_KEY) {
editor_handle_key((char)ev.arg1);
editor_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
ui_mark_dirty(win, 0, 0, win_w, win_h - 20);
} else if (ev.type == GUI_EVENT_CLOSE) {
sys_exit(0);
}

Binary file not shown.

View File

@@ -49,7 +49,7 @@ static void viewer_scale_rgb_to_argb(const unsigned char *rgb, int src_w, int sr
static void viewer_paint(ui_window_t win) {
int cx = 4;
int cy = 24;
int cy = 0;
int cw = win_w - 8;
int ch = win_h - 28;
@@ -75,21 +75,25 @@ static void viewer_paint(ui_window_t win) {
int ox = cx + (cw - disp_w) / 2;
int oy = cy + (ch - disp_h - 30) / 2;
for (int y = 0; y < disp_h; y++) {
int src_y = y * viewer_img_h / disp_h;
if (src_y >= viewer_img_h) src_y = viewer_img_h - 1;
for (int x = 0; x < disp_w; x++) {
int src_x = x * viewer_img_w / disp_w;
if (src_x >= viewer_img_w) src_x = viewer_img_w - 1;
uint32_t pixel = viewer_pixels[src_y * viewer_img_w + src_x];
ui_draw_rect(win, ox + x, oy + y, 1, 1, pixel);
uint32_t *temp_buf = malloc(disp_w * disp_h * sizeof(uint32_t));
if (temp_buf) {
for (int y = 0; y < disp_h; y++) {
int src_y = y * viewer_img_h / disp_h;
if (src_y >= viewer_img_h) src_y = viewer_img_h - 1;
for (int x = 0; x < disp_w; x++) {
int src_x = x * viewer_img_w / disp_w;
if (src_x >= viewer_img_w) src_x = viewer_img_w - 1;
temp_buf[y * disp_w + x] = viewer_pixels[src_y * viewer_img_w + src_x];
}
}
ui_draw_image(win, ox, oy, disp_w, disp_h, temp_buf);
free(temp_buf);
}
int btn_w = 160;
int btn_h = 22;
int btn_x = cx + (cw - btn_w) / 2;
int btn_y = win_h - 30;
int btn_y = (win_h - 20) - 30;
ui_draw_rounded_rect_filled(win, btn_x, btn_y, btn_w, btn_h, 6, 0xFF2D2D2D);
ui_draw_string(win, btn_x + 10, btn_y + 6, "Set as Wallpaper", 0xFFF0F0F0);
}
@@ -101,7 +105,7 @@ static void viewer_handle_click(ui_window_t win, int x, int y) {
int cw = win_w - 8;
int btn_w = 160;
int btn_x = cx + (cw - btn_w) / 2;
int btn_y = win_h - 30;
int btn_y = (win_h - 20) - 30;
if (x >= btn_x && x < btn_x + btn_w && y >= btn_y && y < btn_y + 22) {
// SYSTEM_CMD_SET_WALLPAPER is 3 based on syscall.c code
@@ -214,7 +218,7 @@ int main(int argc, char **argv) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
viewer_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
ui_mark_dirty(win, 0, 0, win_w, win_h - 20);
} else if (ev.type == GUI_EVENT_CLICK) {
viewer_handle_click(win, ev.arg1, ev.arg2);
} else if (ev.type == GUI_EVENT_CLOSE) {

Binary file not shown.

Binary file not shown.