From 48a74bfde2f59bdf99de2fc18f0ff3e54f8e2334 Mon Sep 17 00:00:00 2001 From: zxq5 Date: Tue, 3 Feb 2026 15:38:40 +0000 Subject: [PATCH] updated dsc example to reflect current feature set. --- resources/dsc/example.dsc | 112 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 107 insertions(+), 5 deletions(-) diff --git a/resources/dsc/example.dsc b/resources/dsc/example.dsc index cb07da2..6a7738f 100644 --- a/resources/dsc/example.dsc +++ b/resources/dsc/example.dsc @@ -1,8 +1,110 @@ fn main() -> u32 { - let x: u32 = 5; - let stringgg: str = "Hello world"; - let test: str = "test"; - println("hello world 2 electric boogaloo"); - printnum(213); + let x: u32 = 0; + let y: u32 = &x; + + let alloc: u32 = arena_create(512); + let ptr1: u32 = arena_alloc(alloc, 32); + let ptr2: u32 = arena_alloc(alloc, 32); + + print_hex(alloc); + print_newline(); + print_hex(ptr1); + print_newline(); + print_hex(ptr2); + print_newline(); + printnum(*ptr2); + print_newline(); + *ptr2 = 42; + + print_hex(ptr2); + print_newline(); + printnum(*ptr2); + print_newline(); + println("end"); + + return 0; +} + + + + + + +// Arena Allocator +// Supports multiple arenas that can be destroyed independently +// Much more practical than a simple bump allocator + +// Global heap management +static heap_start: u32 = 0x30000; +static heap_end: u32 = 0x40000; +static heap_current: u32 = 0x30000; + +// Arena structure (stored at the start of each arena): +// [0-3]: start_address (u32) +// [4-7]: current_position (u32) +// [8-11]: end_address (u32) +// Total header size: 12 bytes + +// Create a new arena with given size +// Returns pointer to arena handle (or 0 if failed) +fn arena_create(size: u32) -> u32 { + let total_size: u32 = size + 12; + let arena_ptr: u32 = heap_current; + let new_current: u32 = arena_ptr + total_size; + + // Check if we have space + if new_current > heap_end { + return 0; + } + + // Calculate arena data region + let data_start: u32 = arena_ptr + 12; + let data_end: u32 = arena_ptr + total_size; + + // Initialize arena header + // Note: In real implementation, you'd use pointer writes here + // For now, using placeholder comments: + *arena_ptr = data_start; // start_address + *(arena_ptr + 4) = data_start; // current_position + *(arena_ptr + 8) = data_end; // end_address + + heap_current = new_current; + + return arena_ptr; +} + +// Allocate from an arena +// Returns pointer to allocated memory (or 0 if failed) +fn arena_alloc(arena: u32, size: u32) -> u32 { + // Read current position from arena + let current: u32 = *(arena + 4); + let end: u32 = *(arena + 8); + + let new_current: u32 = current + size; + + // Check if arena has space + if new_current > end { + return 0; + } + + // Update current position in arena + *(arena + 4) = new_current; + + return current; +} + +// Destroy an arena (in bump allocator, this is a no-op) +// In a real allocator, you'd mark the memory as free +fn arena_destroy(arena: u32) { + // In a true allocator, mark memory as reusable + // For bump allocator, we can't reclaim memory + // unless we destroy ALL arenas and reset + return 0; +} + +// Reset entire heap (destroys ALL arenas) +fn reset_all() { + heap_current = heap_start; + return 0; }