Browse Source

Minor changes to `os2`

gingerBill 1 year ago
parent
commit
fa1875a8f1

+ 4 - 0
core/os/os2/env_windows.odin

@@ -18,6 +18,9 @@ _lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string
 		}
 		return "", true
 	}
+
+	_TEMP_ALLOCATOR_GUARD()
+
 	b := make([]u16, n+1, _temp_allocator())
 
 	n = win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b)))
@@ -47,6 +50,7 @@ _unset_env :: proc(key: string) -> bool {
 }
 
 _clear_env :: proc() {
+	_TEMP_ALLOCATOR_GUARD()
 	envs := environ(_temp_allocator())
 	for env in envs {
 		for j in 1..<len(env) {

+ 41 - 3
core/os/os2/file_windows.odin

@@ -1,10 +1,11 @@
 //+private
 package os2
 
+import "base:runtime"
+
 import "core:io"
 import "core:mem"
 import "core:sync"
-import "base:runtime"
 import "core:strings"
 import "core:time"
 import "core:unicode/utf16"
@@ -20,11 +21,45 @@ _file_allocator :: proc() -> runtime.Allocator {
 	return heap_allocator()
 }
 
+_temp_allocator_proc :: runtime.arena_allocator_proc
+
+@(private="file", thread_local)
+_global_default_temp_allocator_arena: runtime.Arena
+
 _temp_allocator :: proc() -> runtime.Allocator {
-	// TODO(bill): make this not depend on the context allocator
-	return context.temp_allocator
+	return runtime.Allocator{
+		procedure = _temp_allocator_proc,
+		data      = &_global_default_temp_allocator_arena,
+	}
 }
 
+@(require_results)
+_temp_allocator_temp_begin :: proc(loc := #caller_location) -> (temp: runtime.Arena_Temp) {
+	temp = runtime.arena_temp_begin(&_global_default_temp_allocator_arena, loc)
+	return
+}
+
+_temp_allocator_temp_end :: proc(temp: runtime.Arena_Temp, loc := #caller_location) {
+	runtime.arena_temp_end(temp, loc)
+}
+
+@(fini, private)
+_destroy_temp_allocator_fini :: proc() {
+	runtime.arena_destroy(&_global_default_temp_allocator_arena)
+	_global_default_temp_allocator_arena = {}
+}
+
+@(deferred_out=_temp_allocator_temp_end)
+_TEMP_ALLOCATOR_GUARD :: #force_inline proc(ignore := false, loc := #caller_location) -> (runtime.Arena_Temp, runtime.Source_Code_Location) {
+	if ignore {
+		return {}, loc
+	} else {
+		return _temp_allocator_temp_begin(loc), loc
+	}
+}
+
+
+
 
 _File_Kind :: enum u8 {
 	File,
@@ -546,6 +581,9 @@ _normalize_link_path :: proc(p: []u16, allocator: runtime.Allocator) -> (str: st
 	if n == 0 {
 		return "", _get_platform_error()
 	}
+
+	_TEMP_ALLOCATOR_GUARD()
+
 	buf := make([]u16, n+1, _temp_allocator())
 	n = win32.GetFinalPathNameByHandleW(handle, raw_data(buf), u32(len(buf)), win32.VOLUME_NAME_DOS)
 	if n == 0 {

+ 4 - 0
core/os/os2/path_windows.odin

@@ -31,6 +31,8 @@ _mkdir_all :: proc(path: string, perm: File_Mode) -> Error {
 		return p, false, nil
 	}
 
+	_TEMP_ALLOCATOR_GUARD()
+
 	dir, err := stat(path, _temp_allocator())
 	if err == nil {
 		if dir.is_directory {
@@ -125,6 +127,8 @@ _fix_long_path_internal :: proc(path: string) -> string {
 		return path
 	}
 
+	_TEMP_ALLOCATOR_GUARD()
+
 	PREFIX :: `\\?`
 	path_buf := make([]byte, len(PREFIX)+len(path)+1, _temp_allocator())
 	copy(path_buf, PREFIX)

+ 4 - 0
core/os/os2/stat_windows.odin

@@ -46,6 +46,8 @@ full_path_from_name :: proc(name: string, allocator: runtime.Allocator) -> (path
 	if name == "" {
 		name = "."
 	}
+	_TEMP_ALLOCATOR_GUARD()
+
 	p := win32.utf8_to_utf16(name, _temp_allocator())
 
 	n := win32.GetFullPathNameW(raw_data(p), 0, nil, nil)
@@ -129,6 +131,7 @@ _cleanpath_from_handle :: proc(f: ^File, allocator: runtime.Allocator) -> (strin
 	if n == 0 {
 		return "", _get_platform_error()
 	}
+	_TEMP_ALLOCATOR_GUARD()
 	buf := make([]u16, max(n, 260)+1, _temp_allocator())
 	n = win32.GetFinalPathNameByHandleW(h, raw_data(buf), u32(len(buf)), 0)
 	return _cleanpath_from_buf(buf[:n], allocator)
@@ -144,6 +147,7 @@ _cleanpath_from_handle_u16 :: proc(f: ^File) -> ([]u16, Error) {
 	if n == 0 {
 		return nil, _get_platform_error()
 	}
+	_TEMP_ALLOCATOR_GUARD()
 	buf := make([]u16, max(n, 260)+1, _temp_allocator())
 	n = win32.GetFinalPathNameByHandleW(h, raw_data(buf), u32(len(buf)), 0)
 	return _cleanpath_strip_prefix(buf[:n]), nil

+ 2 - 0
core/os/os2/temp_file_windows.odin

@@ -17,6 +17,8 @@ _temp_dir :: proc(allocator: runtime.Allocator) -> (string, runtime.Allocator_Er
 	if n == 0 {
 		return "", nil
 	}
+	_TEMP_ALLOCATOR_GUARD()
+
 	b := make([]u16, max(win32.MAX_PATH, n), _temp_allocator())
 	n = win32.GetTempPathW(u32(len(b)), raw_data(b))