docs, stuff

This commit is contained in:
Judah Caruso 2025-09-07 16:44:22 -06:00
parent dc8b8e4f26
commit 5f27b720ea
3 changed files with 28 additions and 24 deletions

View file

@ -1,13 +1,14 @@
#add_context arena := Arena.{ proc = PagingArenaProc }; #add_context arena := Arena.{ proc = PagingArenaProc };
#add_context temp_arena := Arena.{ proc = TempArenaProc }; #add_context temp_arena := Arena.{ proc = TempArenaProc };
/// Arena is an interface for an arena-based memory allocation.
Arena :: struct { Arena :: struct {
proc: ArenaProc; proc: ArenaProc;
data: *void; data: *void;
} }
ArenaProc :: #type ( ArenaProc :: #type (
mode: ArenaMode, event: ArenaEvent,
arena_data: *void, arena_data: *void,
new_size: int, old_size: int, align: int, new_size: int, old_size: int, align: int,
@ -16,7 +17,7 @@ ArenaProc :: #type (
caller: Source_Code_Location caller: Source_Code_Location
) -> *void; ) -> *void;
ArenaMode :: enum { ArenaEvent :: enum {
Setup; Setup;
Teardown; Teardown;
@ -65,13 +66,13 @@ ArenaReset :: (loc := #caller_location) {
context.arena.proc(.Reset, context.arena.data, 0, 0, 0, null, null, loc); context.arena.proc(.Reset, context.arena.data, 0, 0, 0, null, null, loc);
} }
/// ArenaRelease frees an arena's memory. /// ArenaRelease frees the current context.arena's memory.
ArenaRelease :: (loc := #caller_location) { ArenaRelease :: (loc := #caller_location) {
TrySetupArena(*context.arena, loc); TrySetupArena(*context.arena, loc);
context.arena.proc(.Teardown, context.arena.data, 0, 0, 0, null, null, loc); context.arena.proc(.Teardown, context.arena.data, 0, 0, 0, null, null, loc);
} }
/// ArenaToAllocator wraps an arena, allowing it to be used with Jai's builtin Allocator system. /// ArenaToAllocator wraps the given arena, allowing it to be used with Jai's builtin Allocator system.
ArenaToAllocator :: (arena: *Arena) -> Allocator { ArenaToAllocator :: (arena: *Arena) -> Allocator {
return .{ proc = JaiAllocatorProc, data = arena }; return .{ proc = JaiAllocatorProc, data = arena };
} }
@ -104,10 +105,10 @@ TrySetupArena :: (arena: *Arena, loc := #caller_location) #expand {
data := arena.proc(.Setup, null, 0, 0, 0, null, null, loc); data := arena.proc(.Setup, null, 0, 0, 0, null, null, loc);
if data != null if data != null
{ arena.data = data; } { arena.data = data; }
} } @jc.nodocs
PanicArenaProc :: ( PanicArenaProc :: (
mode: ArenaMode, event: ArenaEvent,
arena_data: *void, arena_data: *void,
new_size: int, old_size: int, align: int, new_size: int, old_size: int, align: int,
@ -115,7 +116,7 @@ PanicArenaProc :: (
caller: Source_Code_Location caller: Source_Code_Location
) -> *void { ) -> *void {
if mode == { if event == {
case .Acquire; case .Acquire;
if new_size > 0 if new_size > 0
{ Panic("jc: cannot acquire memory using the PanicArena", loc = caller); } { Panic("jc: cannot acquire memory using the PanicArena", loc = caller); }
@ -130,7 +131,7 @@ PanicArenaProc :: (
} @jc.nodocs } @jc.nodocs
BumpArenaProc :: ( BumpArenaProc :: (
mode: ArenaMode, event: ArenaEvent,
arena_data: *void, arena_data: *void,
new_size: int, old_size: int, align: int, new_size: int, old_size: int, align: int,
@ -139,7 +140,7 @@ BumpArenaProc :: (
caller: Source_Code_Location caller: Source_Code_Location
) -> *void #no_abc { ) -> *void #no_abc {
bump := arena_data.(*BumpData); bump := arena_data.(*BumpData);
if mode == { if event == {
case .Setup; case .Setup;
if bump.memory.data == null if bump.memory.data == null
{ Panic("jc: BumpArena has no memory", loc = caller); } { Panic("jc: BumpArena has no memory", loc = caller); }
@ -176,7 +177,7 @@ BumpArenaProc :: (
} @jc.nodocs } @jc.nodocs
PagingArenaProc :: ( PagingArenaProc :: (
mode: ArenaMode, event: ArenaEvent,
arena_data: *void, arena_data: *void,
new_size: int, old_size: int, align: int, new_size: int, old_size: int, align: int,
@ -191,7 +192,7 @@ PagingArenaProc :: (
// @note(judah): TempArenaProc just wraps BumpArenaProc, but provides its own backing memory. // @note(judah): TempArenaProc just wraps BumpArenaProc, but provides its own backing memory.
// This is needed so we can use TempArenaProc without forcing the user to set everything up. // This is needed so we can use TempArenaProc without forcing the user to set everything up.
TempArenaProc :: ( TempArenaProc :: (
mode: ArenaMode, event: ArenaEvent,
arena_data: *void, arena_data: *void,
new_size: int, old_size: int, align: int, new_size: int, old_size: int, align: int,
@ -204,7 +205,7 @@ TempArenaProc :: (
malloc :: (size: int) -> *void #c_call #foreign libc; malloc :: (size: int) -> *void #c_call #foreign libc;
free :: (ptr: *void) #c_call #foreign libc; free :: (ptr: *void) #c_call #foreign libc;
if mode == { if event == {
// @note(judah): allows the temp arena to initialize itself without relying on another arena to provide its memory. // @note(judah): allows the temp arena to initialize itself without relying on another arena to provide its memory.
case .Setup; case .Setup;
mcount := AlignForward(size_of(BumpData) + 32768).(int); mcount := AlignForward(size_of(BumpData) + 32768).(int);
@ -221,13 +222,13 @@ TempArenaProc :: (
free(arena_data); free(arena_data);
} }
return BumpArenaProc(mode, arena_data, new_size, old_size, align, new_ptr, old_ptr, caller); return BumpArenaProc(event, arena_data, new_size, old_size, align, new_ptr, old_ptr, caller);
} @jc.nodocs } @jc.nodocs
#scope_file #scope_file
JaiAllocatorProc :: (mode: Allocator_Mode, requested_size: s64, old_size: s64, old_memory: *void, allocator_data: *void) -> *void { JaiAllocatorProc :: (event: Allocator_Mode, requested_size: s64, old_size: s64, old_memory: *void, allocator_data: *void) -> *void {
CallerFromStackTrace :: () -> Source_Code_Location #expand { CallerFromStackTrace :: () -> Source_Code_Location #expand {
node := context.stack_trace; node := context.stack_trace;
if node.next != null { if node.next != null {
@ -237,7 +238,7 @@ JaiAllocatorProc :: (mode: Allocator_Mode, requested_size: s64, old_size: s64, o
} }
arena := allocator_data.(*Arena); arena := allocator_data.(*Arena);
if mode == { if event == {
case .STARTUP; case .STARTUP;
TrySetupArena(arena); TrySetupArena(arena);
return null; return null;

View file

@ -106,7 +106,10 @@ NextPowerOfTwo :: (x: int) -> int #no_aoc {
return x + 1; return x + 1;
} }
TrySetAllocator :: (thing: *$T, allocator := context.allocator) #modify {
#scope_module
TrySetAllocator :: (thing: *$T) #modify {
info := T.(*Type_Info_Struct); info := T.(*Type_Info_Struct);
ok := false; ok := false;
@ -121,12 +124,12 @@ TrySetAllocator :: (thing: *$T, allocator := context.allocator) #modify {
return ok, "can only set allocator on struct with an allocator field or dynamic array"; return ok, "can only set allocator on struct with an allocator field or dynamic array";
} #expand { } #expand {
if thing.allocator.proc == null { if thing.allocator.proc == null {
thing.allocator = allocator; thing.allocator = context.allocator;
}
} }
} @jc.nodocs
TrySetAllocator :: (array: *[..]$T, allocator := context.allocator) #expand { TrySetAllocator :: (array: *[..]$T) #expand {
if array.allocator.proc == null { if array.allocator.proc == null {
array.allocator = allocator; array.allocator = context.allocator;
}
} }
} @jc.nodocs

View file

@ -14,9 +14,9 @@ This is what I'm doing day-to-day.
+ replaced allocators with custom arenas + replaced allocators with custom arenas
+ Panic/Unreachable prints stack trace in debug builds + Panic/Unreachable prints stack trace in debug builds
+ type_info names are now pascal case + type_info names are now pascal case
+ fixed +internal's weird behavior in the test runner
: not sure how [..]T should interface with arenas + ArrayAppend/ArrayGrow now wrap the current arena as the attached Allocator
+ ArenaMode -> ArenaEvent
/ 09.06.25 / 09.06.25