Allow for nested temp_allocator() calls to flip between arenas on TEMP_ALLOCATOR_GUARDs

This commit is contained in:
gingerBill
2024-08-04 17:14:24 +01:00
parent f427f040fd
commit d0709a7de2
3 changed files with 30 additions and 12 deletions
+25 -7
View File
@@ -10,20 +10,29 @@ file_allocator :: proc() -> runtime.Allocator {
temp_allocator_proc :: runtime.arena_allocator_proc temp_allocator_proc :: runtime.arena_allocator_proc
@(private="file")
MAX_TEMP_ARENA_COUNT :: 2
@(private="file", thread_local) @(private="file", thread_local)
global_default_temp_allocator_arena: runtime.Arena global_default_temp_allocator_arenas: [MAX_TEMP_ARENA_COUNT]runtime.Arena
@(private="file", thread_local)
global_default_temp_allocator_index: uint
@(require_results) @(require_results)
temp_allocator :: proc() -> runtime.Allocator { temp_allocator :: proc() -> runtime.Allocator {
return runtime.Allocator{ return runtime.Allocator{
procedure = temp_allocator_proc, procedure = temp_allocator_proc,
data = &global_default_temp_allocator_arena, data = &global_default_temp_allocator_arenas[global_default_temp_allocator_index],
} }
} }
@(require_results) @(require_results)
temp_allocator_temp_begin :: proc(loc := #caller_location) -> (temp: runtime.Arena_Temp) { temp_allocator_temp_begin :: proc(loc := #caller_location) -> (temp: runtime.Arena_Temp) {
temp = runtime.arena_temp_begin(&global_default_temp_allocator_arena, loc) temp = runtime.arena_temp_begin(&global_default_temp_allocator_arenas[global_default_temp_allocator_index], loc)
return return
} }
@@ -33,16 +42,25 @@ temp_allocator_temp_end :: proc(temp: runtime.Arena_Temp, loc := #caller_locatio
@(fini, private) @(fini, private)
temp_allocator_fini :: proc() { temp_allocator_fini :: proc() {
runtime.arena_destroy(&global_default_temp_allocator_arena) for &arena in global_default_temp_allocator_arenas {
global_default_temp_allocator_arena = {} runtime.arena_destroy(&arena)
}
global_default_temp_allocator_arenas = {}
} }
@(deferred_out=temp_allocator_temp_end) TEMP_ALLOCATOR_GUARD_END :: proc(temp: runtime.Arena_Temp loc := #caller_location) {
runtime.arena_temp_end(temp, loc)
global_default_temp_allocator_index = (global_default_temp_allocator_index-1)%MAX_TEMP_ARENA_COUNT
}
@(deferred_out=TEMP_ALLOCATOR_GUARD_END)
TEMP_ALLOCATOR_GUARD :: #force_inline proc(ignore := false, loc := #caller_location) -> (runtime.Arena_Temp, runtime.Source_Code_Location) { TEMP_ALLOCATOR_GUARD :: #force_inline proc(ignore := false, loc := #caller_location) -> (runtime.Arena_Temp, runtime.Source_Code_Location) {
if ignore { if ignore {
return {}, loc return {}, loc
} else { } else {
return temp_allocator_temp_begin(loc), loc tmp := temp_allocator_temp_begin(loc)
global_default_temp_allocator_index = (global_default_temp_allocator_index+1)%MAX_TEMP_ARENA_COUNT
return tmp, loc
} }
} }
+4 -4
View File
@@ -4,7 +4,7 @@ package os2
import "core:sys/linux" import "core:sys/linux"
@(rodata) @(rodata)
_errno_strings := [linux.Error]string{ _errno_strings := [linux.Errno]string{
.NONE = "", .NONE = "",
.EPERM = "Operation not permitted", .EPERM = "Operation not permitted",
.ENOENT = "No such file or directory", .ENOENT = "No such file or directory",
@@ -142,7 +142,7 @@ _errno_strings := [linux.Error]string{
} }
_get_platform_error :: proc(errno: linux.Error) -> Error { _get_platform_error :: proc(errno: linux.Errno) -> Error {
#partial switch errno { #partial switch errno {
case .NONE: case .NONE:
return nil return nil
@@ -158,8 +158,8 @@ _get_platform_error :: proc(errno: linux.Error) -> Error {
} }
_error_string :: proc(errno: i32) -> string { _error_string :: proc(errno: i32) -> string {
if errno >= 0 && errno <= i32(max(linux.Error)) { if errno >= 0 && errno <= i32(max(linux.Errno)) {
return _errno_strings[linux.Error(errno)] return _errno_strings[linux.Errno(errno)]
} }
return "Unknown Error" return "Unknown Error"
} }
+1 -1
View File
@@ -59,7 +59,7 @@ _mkdir_all :: proc(path: string, perm: int) -> Error {
path_bytes[len(path)] = 0 path_bytes[len(path)] = 0
dfd: linux.Fd dfd: linux.Fd
errno: linux.Error errno: linux.Errno
if path_bytes[0] == '/' { if path_bytes[0] == '/' {
dfd, errno = linux.open("/", _OPENDIR_FLAGS) dfd, errno = linux.open("/", _OPENDIR_FLAGS)
path_bytes = path_bytes[1:] path_bytes = path_bytes[1:]