Commit 5665ece

kravantokh <kravantokh@noreply.codeberg.org>
2026-07-17 19:06:35
Add arena test. Implmenet missing arena functionality.
1 parent 6f9ca10
Changed files (4)
memory/arena.h
@@ -33,7 +33,6 @@ Status Arena_align( Arena *a, usize alignment );
 
 
 IMPL(
-
 	Status Arena_create( Arena *out, usize size ) {
 		TRY( u64_round_up( size, 0x1000, &size ) );
 		TRY( mem_claim( size/0x1000, &out->start ) );
@@ -65,6 +64,31 @@ IMPL(
 		return S_SUCCESS;
 	}
 
+	Status Arena_take( Arena *a, usize amount, u8 **out ) {
+		assert( out != 0 );
+		assert( amount != 0 );
+
+		if( a->claimed - a->taken < amount ) { /* no overflow risk this way */
+			return S_FAILURE | ERR_OUT_OF_BOUND;
+		}
+
+		u64 new_taken = a->taken + amount;
+
+		if( new_taken > a->occupied ) {
+			u64 occupy_amount = 0;
+
+			TRY( u64_round_up( new_taken - a->occupied , sizeof(Page), &occupy_amount ) );
+			TRY( mem_occupy( a->start + a->occupied/sizeof(Page), occupy_amount/0x1000 ) );
+			
+			a->occupied += occupy_amount;
+		}
+
+		*out = ((u8*)a->start + a->taken);
+		a->taken = new_taken;
+
+		return S_SUCCESS;
+	}
+
 
 	Status Arena_give( Arena *a, usize amount ) {
 		if( amount > a->taken ) return S_FAILURE | ERR_OUT_OF_BOUND;
platform/platform.h
@@ -46,4 +46,15 @@ ASSERT( sizeof( f64 ) == 8 );
 /* Error handling types and macros. */
 #include "./common/error.h"
 
+
+/* Compiler-specific tricks */
+
+GCC( IMPL(
+	/* This is so that GCC can detect stack smashing via canaries and crash the program when it happens. */
+	void __stack_chk_fail(void) {
+		assert(0);
+	}
+))
+
+
 #endif
testing/tests/002_arena_basic.c
@@ -0,0 +1,25 @@
+#include "memory/arena.h"
+
+Arena a = {0};
+
+void entry(void) {
+	assert( S_OK(
+		Arena_create( &a, KiB(5) )
+	));
+
+
+	for( u64 i = 0; i < KiB(5)/sizeof(u32); ++i ) {
+		u32 *out = 0;
+		assert( S_OK(
+			Arena_take( &a, sizeof(u32), (u8**)&out )
+		));
+		*out = 0xdeadbeef;
+	}
+
+
+	assert( S_OK(
+		Arena_destroy( &a )
+	));
+
+	terminate(0);
+}
testing/test.sh
@@ -22,7 +22,7 @@ echo
 
 for SRC in ./testing/tests/*.c; do
 
-	if ! $CC -nostdlib -I. -g -fno-builtin -O0 -DBUILD_TYPE=1 -DNOTLIBC_IMPLEMENT -fno-stack-protector -static $SRC -o ./build/tests/o0d/$(basename "$SRC" .c); then
+	if ! $CC -nostdlib -I. -g -fno-builtin -O0 -DBUILD_TYPE=1 -DNOTLIBC_IMPLEMENT $SRC -o ./build/tests/o0d/$(basename "$SRC" .c); then
 		echo
 		echo "Compilation failure for $SRC - O0D."
 		FAILURE=1
@@ -40,19 +40,19 @@ for SRC in ./testing/tests/*.c; do
 		FAILURE=1
 	fi
 
-	if ! $CC -nostdlib -I. -g -fno-builtin -O3 -DBUILD_TYPE=1 $SRC -o ./build/tests/o3d/$(basename "$SRC" .c); then
+	if ! $CC -nostdlib -I. -g -fno-builtin -O3 -DBUILD_TYPE=1 -DNOTLIBC_IMPLEMENT $SRC -o ./build/tests/o3d/$(basename "$SRC" .c); then
 		echo
 		echo "Compilation failure for $SRC - O3D."
 		FAILURE=1
 	fi
 
-	if ! $CC -nostdlib -I. -g -fno-builtin -O2 -DBUILD_TYPE=2 $SRC -o ./build/tests/o2r/$(basename "$SRC" .c); then
+	if ! $CC -nostdlib -I. -g -fno-builtin -O2 -DBUILD_TYPE=2 -DNOTLIBC_IMPLEMENT $SRC -o ./build/tests/o2r/$(basename "$SRC" .c); then
 		echo
 		echo "Compilation failure for $SRC - O2R."
 		FAILURE=1
 	fi
 
-	if ! $CC -nostdlib -I. -g -fno-builtin -O3 -DBUILD_TYPE=2 $SRC -o ./build/tests/o3r/$(basename "$SRC" .c); then
+	if ! $CC -nostdlib -I. -g -fno-builtin -O3 -DBUILD_TYPE=2 -DNOTLIBC_IMPLEMENT $SRC -o ./build/tests/o3r/$(basename "$SRC" .c); then
 		echo
 		echo "Compilation failure for $SRC - O3R."
 		FAILURE=1