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 temp_arena := Arena.{ proc = TempArenaProc };
/// Arena is an interface for an arena-based memory allocation.
Arena :: struct {
proc: ArenaProc;
data: *void;
}
ArenaProc :: #type (
mode: ArenaMode,
event: ArenaEvent,
arena_data: *void,
new_size: int, old_size: int, align: int,
@ -16,7 +17,7 @@ ArenaProc :: #type (
caller: Source_Code_Location
) -> *void;
ArenaMode :: enum {
ArenaEvent :: enum {
Setup;
Teardown;
@ -65,13 +66,13 @@ ArenaReset :: (loc := #caller_location) {
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) {
TrySetupArena(*context.arena, 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 {
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);
if data != null
{ arena.data = data; }
}
} @jc.nodocs
PanicArenaProc :: (
mode: ArenaMode,
event: ArenaEvent,
arena_data: *void,
new_size: int, old_size: int, align: int,
@ -115,7 +116,7 @@ PanicArenaProc :: (
caller: Source_Code_Location
) -> *void {
if mode == {
if event == {
case .Acquire;
if new_size > 0
{ Panic("jc: cannot acquire memory using the PanicArena", loc = caller); }
@ -130,7 +131,7 @@ PanicArenaProc :: (
} @jc.nodocs
BumpArenaProc :: (
mode: ArenaMode,
event: ArenaEvent,
arena_data: *void,
new_size: int, old_size: int, align: int,
@ -139,7 +140,7 @@ BumpArenaProc :: (
caller: Source_Code_Location
) -> *void #no_abc {
bump := arena_data.(*BumpData);
if mode == {
if event == {
case .Setup;
if bump.memory.data == null
{ Panic("jc: BumpArena has no memory", loc = caller); }
@ -176,7 +177,7 @@ BumpArenaProc :: (
} @jc.nodocs
PagingArenaProc :: (
mode: ArenaMode,
event: ArenaEvent,
arena_data: *void,
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.
// This is needed so we can use TempArenaProc without forcing the user to set everything up.
TempArenaProc :: (
mode: ArenaMode,
event: ArenaEvent,
arena_data: *void,
new_size: int, old_size: int, align: int,
@ -204,7 +205,7 @@ TempArenaProc :: (
malloc :: (size: int) -> *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.
case .Setup;
mcount := AlignForward(size_of(BumpData) + 32768).(int);
@ -221,13 +222,13 @@ TempArenaProc :: (
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
#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 {
node := context.stack_trace;
if node.next != null {
@ -237,7 +238,7 @@ JaiAllocatorProc :: (mode: Allocator_Mode, requested_size: s64, old_size: s64, o
}
arena := allocator_data.(*Arena);
if mode == {
if event == {
case .STARTUP;
TrySetupArena(arena);
return null;

View file

@ -106,7 +106,10 @@ NextPowerOfTwo :: (x: int) -> int #no_aoc {
return x + 1;
}
TrySetAllocator :: (thing: *$T, allocator := context.allocator) #modify {
#scope_module
TrySetAllocator :: (thing: *$T) #modify {
info := T.(*Type_Info_Struct);
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";
} #expand {
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 {
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
+ Panic/Unreachable prints stack trace in debug builds
+ type_info names are now pascal case
: not sure how [..]T should interface with arenas
+ fixed +internal's weird behavior in the test runner
+ ArrayAppend/ArrayGrow now wrap the current arena as the attached Allocator
+ ArenaMode -> ArenaEvent
/ 09.06.25