Browse Source

Remove all uses of `context` stuff in os2

gingerBill 1 year ago
parent
commit
453fc5182b

+ 1 - 2
core/os/os2/file_windows.odin

@@ -6,7 +6,6 @@ import "base:runtime"
 import "core:io"
 import "core:mem"
 import "core:sync"
-import "core:strings"
 import "core:time"
 import "core:unicode/utf16"
 import win32 "core:sys/windows"
@@ -137,7 +136,7 @@ _new_file :: proc(handle: uintptr, name: string) -> ^File {
 
 	f.impl.allocator = file_allocator()
 	f.impl.fd = rawptr(handle)
-	f.impl.name = strings.clone(name, f.impl.allocator)
+	f.impl.name, _ = clone_string(name, f.impl.allocator)
 	f.impl.wname = win32.utf8_to_wstring(name, f.impl.allocator)
 
 	handle := _handle(f)

+ 24 - 2
core/os/os2/internal_util.odin

@@ -47,8 +47,16 @@ temp_cstring :: proc(s: string) -> (cstring, runtime.Allocator_Error) {
 	return clone_to_cstring(s, temp_allocator())
 }
 
-
-
+string_from_null_terminated_bytes :: proc(b: []byte) -> (res: string) {
+	s := string(b)
+	i := 0
+	for ; i < len(s); i += 1 {
+		if s[i] == 0 {
+			break
+		}
+	}
+	return s[:i]
+}
 
 @(require_results)
 concatenate_strings_from_buffer :: proc(buf: []byte, strings: ..string) -> string {
@@ -61,6 +69,20 @@ concatenate_strings_from_buffer :: proc(buf: []byte, strings: ..string) -> strin
 	return string(buf[:n])
 }
 
+@(require_results)
+concatenate :: proc(strings: []string, allocator: runtime.Allocator) -> (res: string, err: runtime.Allocator_Error) {
+	n := 0
+	for s in strings {
+		n += len(s)
+	}
+	buf := make([]byte, n) or_return
+	n = 0
+	for s in strings {
+		n += copy(buf[n:], s)
+	}
+	return string(buf), nil
+}
+
 
 
 @(private="file")

+ 8 - 6
core/os/os2/path_linux.odin

@@ -1,7 +1,6 @@
 //+private
 package os2
 
-import "core:strings"
 import "core:strconv"
 import "base:runtime"
 import "core:sys/unix"
@@ -33,7 +32,7 @@ _mkdir :: proc(path: string, perm: File_Mode) -> Error {
 	}
 
 	TEMP_ALLOCATOR_GUARD()
-	path_cstr := strings.clone_to_cstring(path, temp_allocator())
+	path_cstr := temp_cstring(path) or_return
 	return _ok_or_error(unix.sys_mkdir(path_cstr, uint(perm & 0o777)))
 }
 
@@ -73,6 +72,8 @@ _mkdir_all :: proc(path: string, perm: File_Mode) -> Error {
 		return .Invalid_Argument
 	}
 
+	TEMP_ALLOCATOR_GUARD()
+
 	// need something we can edit, and use to generate cstrings
 	allocated: bool
 	path_bytes: []u8
@@ -80,7 +81,7 @@ _mkdir_all :: proc(path: string, perm: File_Mode) -> Error {
 		allocated = true
 		path_bytes = make([]u8, len(path) + 1)
 	} else {
-		path_bytes = make([]u8, len(path) + 1, context.temp_allocator)
+		path_bytes = make([]u8, len(path) + 1, temp_allocator())
 	}
 
 	// NULL terminate the byte slice to make it a valid cstring
@@ -178,7 +179,8 @@ _remove_all :: proc(path: string) -> Error {
 		return nil
 	}
 
-	path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
+	TEMP_ALLOCATOR_GUARD()
+	path_cstr := temp_cstring(path) or_return
 
 	fd := unix.sys_open(path_cstr, _OPENDIR_FLAGS)
 	switch fd {
@@ -204,7 +206,7 @@ _getwd :: proc(allocator: runtime.Allocator) -> (string, Error) {
 		#no_bounds_check res := unix.sys_getcwd(&buf[0], uint(len(buf)))
 
 		if res >= 0 {
-			return strings.string_from_null_terminated_ptr(&buf[0], len(buf)), nil
+			return string_from_null_terminated_bytes(buf[:]), nil
 		}
 		if res != -ERANGE {
 			return "", _get_platform_error(res)
@@ -215,7 +217,7 @@ _getwd :: proc(allocator: runtime.Allocator) -> (string, Error) {
 }
 
 _setwd :: proc(dir: string) -> Error {
-	dir_cstr := strings.clone_to_cstring(dir, context.temp_allocator)
+	dir_cstr := temp_cstring(dir) or_return
 	return _ok_or_error(unix.sys_chdir(dir_cstr))
 }
 

+ 1 - 2
core/os/os2/path_windows.odin

@@ -3,7 +3,6 @@ package os2
 
 import win32 "core:sys/windows"
 import "base:runtime"
-import "core:strings"
 
 _Path_Separator        :: '\\'
 _Path_Separator_String :: "\\"
@@ -24,7 +23,7 @@ _mkdir_all :: proc(path: string, perm: File_Mode) -> Error {
 	fix_root_directory :: proc(p: string) -> (s: string, allocated: bool, err: runtime.Allocator_Error) {
 		if len(p) == len(`\\?\c:`) {
 			if is_path_separator(p[0]) && is_path_separator(p[1]) && p[2] == '?' && is_path_separator(p[3]) && p[5] == ':' {
-				s = strings.concatenate({p, `\`}, file_allocator()) or_return
+				s = concatenate({p, `\`}, file_allocator()) or_return
 				allocated = true
 				return
 			}

+ 12 - 10
core/os/os2/stat_linux.odin

@@ -3,7 +3,6 @@ package os2
 
 import "core:time"
 import "base:runtime"
-import "core:strings"
 import "core:sys/unix"
 import "core:path/filepath"
 
@@ -112,8 +111,9 @@ _fstat_internal :: proc(fd: int, allocator: runtime.Allocator) -> (File_Info, Er
 }
 
 // NOTE: _stat and _lstat are using _fstat to avoid a race condition when populating fullpath
-_stat :: proc(name: string, allocator: runtime.Allocator) -> (File_Info, Error) {
-	name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
+_stat :: proc(name: string, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) {
+	TEMP_ALLOCATOR_GUARD()
+	name_cstr := temp_cstring(name) or_return
 
 	fd := unix.sys_open(name_cstr, _O_RDONLY)
 	if fd < 0 {
@@ -123,8 +123,9 @@ _stat :: proc(name: string, allocator: runtime.Allocator) -> (File_Info, Error)
 	return _fstat_internal(fd, allocator)
 }
 
-_lstat :: proc(name: string, allocator: runtime.Allocator) -> (File_Info, Error) {
-	name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
+_lstat :: proc(name: string, allocator: runtime.Allocator) -> (fi: File_Info, err: Error) {
+	TEMP_ALLOCATOR_GUARD()
+	name_cstr := temp_cstring(name) or_return
 
 	fd := unix.sys_open(name_cstr, _O_RDONLY | _O_PATH | _O_NOFOLLOW)
 	if fd < 0 {
@@ -138,8 +139,9 @@ _same_file :: proc(fi1, fi2: File_Info) -> bool {
 	return fi1.fullpath == fi2.fullpath
 }
 
-_stat_internal :: proc(name: string) -> (s: _Stat, res: int) {
-	name_cstr := strings.clone_to_cstring(name, context.temp_allocator)
-	res = unix.sys_stat(name_cstr, &s)
-	return
-}
+// _stat_internal :: proc(name: string) -> (s: _Stat, res: int) {
+// 	TEMP_ALLOCATOR_GUARD()
+// 	name_cstr := temp_cstring(name) or_return
+// 	res = unix.sys_stat(name_cstr, &s)
+// 	return
+// }

+ 5 - 18
core/os/os2/temp_file.odin

@@ -18,7 +18,7 @@ create_temp_file :: proc(dir, pattern: string) -> (f: ^File, err: Error) {
 	TEMP_ALLOCATOR_GUARD()
 	dir := dir if dir != "" else temp_directory(temp_allocator()) or_return
 	prefix, suffix := _prefix_and_suffix(pattern) or_return
-	prefix = temp_join_path(dir, prefix)
+	prefix = temp_join_path(dir, prefix) or_return
 
 	rand_buf: [32]byte
 	name_buf := make([]byte, len(prefix)+len(rand_buf)+len(suffix), temp_allocator())
@@ -50,7 +50,7 @@ make_directory_temp :: proc(dir, pattern: string, allocator: runtime.Allocator)
 	TEMP_ALLOCATOR_GUARD()
 	dir := dir if dir != "" else temp_directory(temp_allocator()) or_return
 	prefix, suffix := _prefix_and_suffix(pattern) or_return
-	prefix = temp_join_path(dir, prefix)
+	prefix = temp_join_path(dir, prefix) or_return
 
 	rand_buf: [32]byte
 	name_buf := make([]byte, len(prefix)+len(rand_buf)+len(suffix), temp_allocator())
@@ -88,23 +88,10 @@ temp_directory :: proc(allocator: runtime.Allocator) -> (string, Error) {
 
 
 @(private="file")
-temp_join_path :: proc(dir, name: string) -> string {
-	concat :: proc(strings: ..string) -> string {
-		n := 0
-		for s in strings {
-			n += len(s)
-		}
-		buf := make([]byte, n)
-		n = 0
-		for s in strings {
-			n += copy(buf[n:], s)
-		}
-		return string(buf)
-	}
-
+temp_join_path :: proc(dir, name: string) -> (string, runtime.Allocator_Error) {
 	if len(dir) > 0 && is_path_separator(dir[len(dir)-1]) {
-		return concat(dir, name)
+		return concatenate({dir, name}, temp_allocator(),)
 	}
 
-	return concat(dir, Path_Separator_String, name)
+	return concatenate({dir, Path_Separator_String, name}, temp_allocator())
 }

+ 6 - 7
core/os/os2/user.odin

@@ -1,6 +1,5 @@
 package os2
 
-import "core:strings"
 import "base:runtime"
 
 @(require_results)
@@ -9,12 +8,12 @@ user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error
 	case .Windows:
 		dir = get_env("LocalAppData", allocator)
 		if dir != "" {
-			dir = strings.clone(dir, allocator) or_return
+			dir = clone_string(dir, allocator) or_return
 		}
 	case .Darwin:
 		dir = get_env("HOME", allocator)
 		if dir != "" {
-			dir = strings.concatenate({dir, "/Library/Caches"}, allocator) or_return
+			dir = concatenate({dir, "/Library/Caches"}, allocator) or_return
 		}
 	case: // All other UNIX systems
 		dir = get_env("XDG_CACHE_HOME", allocator)
@@ -23,7 +22,7 @@ user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error
 			if dir == "" {
 				return
 			}
-			dir = strings.concatenate({dir, "/.cache"}, allocator) or_return
+			dir = concatenate({dir, "/.cache"}, allocator) or_return
 		}
 	}
 	if dir == "" {
@@ -38,12 +37,12 @@ user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Erro
 	case .Windows:
 		dir = get_env("AppData", allocator)
 		if dir != "" {
-			dir = strings.clone(dir, allocator) or_return
+			dir = clone_string(dir, allocator) or_return
 		}
 	case .Darwin:
 		dir = get_env("HOME", allocator)
 		if dir != "" {
-			dir = strings.concatenate({dir, "/Library/Application Support"}, allocator) or_return
+			dir = concatenate({dir, "/Library/Application Support"}, allocator) or_return
 		}
 	case: // All other UNIX systems
 		dir = get_env("XDG_CACHE_HOME", allocator)
@@ -52,7 +51,7 @@ user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Erro
 			if dir == "" {
 				return
 			}
-			dir = strings.concatenate({dir, "/.config"}, allocator) or_return
+			dir = concatenate({dir, "/.config"}, allocator) or_return
 		}
 	}
 	if dir == "" {