From 450b9ceaec2bf30baa4ffe8acdaa2339a84f3590 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 14 May 2024 18:25:15 +0100 Subject: [PATCH] Add `@(require_results)` everywhere in os2 --- core/os/os2/allocators.odin | 2 ++ core/os/os2/env.odin | 3 ++ core/os/os2/file.odin | 8 +++++ core/os/os2/file_linux.odin | 60 +++++++++++++++------------------- core/os/os2/file_util.odin | 3 ++ core/os/os2/heap.odin | 2 ++ core/os/os2/internal_util.odin | 28 ++++++++++++++-- core/os/os2/path.odin | 2 ++ core/os/os2/path_linux.odin | 3 +- core/os/os2/pipe.odin | 1 + core/os/os2/stat.odin | 4 +++ core/os/os2/temp_file.odin | 5 ++- core/os/os2/user.odin | 3 ++ 13 files changed, 85 insertions(+), 39 deletions(-) diff --git a/core/os/os2/allocators.odin b/core/os/os2/allocators.odin index c044e4522..40672face 100644 --- a/core/os/os2/allocators.odin +++ b/core/os/os2/allocators.odin @@ -3,6 +3,7 @@ package os2 import "base:runtime" +@(require_results) file_allocator :: proc() -> runtime.Allocator { return heap_allocator() } @@ -12,6 +13,7 @@ temp_allocator_proc :: runtime.arena_allocator_proc @(private="file", thread_local) global_default_temp_allocator_arena: runtime.Arena +@(require_results) temp_allocator :: proc() -> runtime.Allocator { return runtime.Allocator{ procedure = temp_allocator_proc, diff --git a/core/os/os2/env.odin b/core/os/os2/env.odin index bed4bebd9..c8d39b270 100644 --- a/core/os/os2/env.odin +++ b/core/os/os2/env.odin @@ -6,6 +6,7 @@ import "base:runtime" // It returns the value, which will be empty if the variable is not present // To distinguish between an empty value and an unset value, use lookup_env // NOTE: the value will be allocated with the supplied allocator +@(require_results) get_env :: proc(key: string, allocator: runtime.Allocator) -> string { value, _ := lookup_env(key, allocator) return value @@ -15,6 +16,7 @@ get_env :: proc(key: string, allocator: runtime.Allocator) -> string { // If the variable is found in the environment the value (which can be empty) is returned and the boolean is true // Otherwise the returned value will be empty and the boolean will be false // NOTE: the value will be allocated with the supplied allocator +@(require_results) lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string, found: bool) { return _lookup_env(key, allocator) } @@ -38,6 +40,7 @@ clear_env :: proc() { // environ returns a copy of strings representing the environment, in the form "key=value" // NOTE: the slice of strings and the strings with be allocated using the supplied allocator +@(require_results) environ :: proc(allocator: runtime.Allocator) -> []string { return _environ(allocator) } diff --git a/core/os/os2/file.odin b/core/os/os2/file.odin index a0fd48c27..b6e9472fb 100644 --- a/core/os/os2/file.odin +++ b/core/os/os2/file.odin @@ -51,22 +51,27 @@ stdout: ^File = nil // OS-Specific stderr: ^File = nil // OS-Specific +@(require_results) create :: proc(name: string) -> (^File, Error) { return open(name, {.Read, .Write, .Create}, File_Mode(0o777)) } +@(require_results) open :: proc(name: string, flags := File_Flags{.Read}, perm := File_Mode(0o777)) -> (^File, Error) { return _open(name, flags, perm) } +@(require_results) new_file :: proc(handle: uintptr, name: string) -> ^File { return _new_file(handle, name) } +@(require_results) fd :: proc(f: ^File) -> uintptr { return _fd(f) } +@(require_results) name :: proc(f: ^File) -> string { return _name(f) } @@ -200,15 +205,18 @@ fchange_times :: proc(f: ^File, atime, mtime: time.Time) -> Error { return _fchtimes(f, atime, mtime) } +@(require_results) exists :: proc(path: string) -> bool { return _exists(path) } +@(require_results) is_file :: proc(path: string) -> bool { return _is_file(path) } is_dir :: is_directory +@(require_results) is_directory :: proc(path: string) -> bool { return _is_dir(path) } diff --git a/core/os/os2/file_linux.odin b/core/os/os2/file_linux.odin index 3f66bbd09..3843c1105 100644 --- a/core/os/os2/file_linux.odin +++ b/core/os/os2/file_linux.odin @@ -1,10 +1,9 @@ //+private package os2 +import "base:runtime" import "core:io" import "core:time" -import "core:strings" -import "base:runtime" import "core:sys/unix" INVALID_HANDLE :: -1 @@ -35,9 +34,9 @@ _File :: struct { allocator: runtime.Allocator, } -_open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (^File, Error) { +_open :: proc(name: string, flags: File_Flags, perm: File_Mode) -> (f: ^File, err: Error) { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr := temp_cstring(name) or_return // Just default to using O_NOCTTY because needing to open a controlling // terminal would be incredibly rare. This has no effect on files while @@ -195,7 +194,7 @@ _truncate :: proc(f: ^File, size: i64) -> Error { _remove :: proc(name: string) -> Error { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr := temp_cstring(name) or_return fd := unix.sys_open(name_cstr, int(File_Flags.Read)) if fd < 0 { @@ -210,22 +209,25 @@ _remove :: proc(name: string) -> Error { } _rename :: proc(old_name, new_name: string) -> Error { - old_name_cstr := strings.clone_to_cstring(old_name, context.temp_allocator) - new_name_cstr := strings.clone_to_cstring(new_name, context.temp_allocator) + TEMP_ALLOCATOR_GUARD() + old_name_cstr := temp_cstring(old_name) or_return + new_name_cstr := temp_cstring(new_name) or_return return _ok_or_error(unix.sys_rename(old_name_cstr, new_name_cstr)) } _link :: proc(old_name, new_name: string) -> Error { - old_name_cstr := strings.clone_to_cstring(old_name, context.temp_allocator) - new_name_cstr := strings.clone_to_cstring(new_name, context.temp_allocator) + TEMP_ALLOCATOR_GUARD() + old_name_cstr := temp_cstring(old_name) or_return + new_name_cstr := temp_cstring(new_name) or_return return _ok_or_error(unix.sys_link(old_name_cstr, new_name_cstr)) } _symlink :: proc(old_name, new_name: string) -> Error { - old_name_cstr := strings.clone_to_cstring(old_name, context.temp_allocator) - new_name_cstr := strings.clone_to_cstring(new_name, context.temp_allocator) + TEMP_ALLOCATOR_GUARD() + old_name_cstr := temp_cstring(old_name) or_return + new_name_cstr := temp_cstring(new_name) or_return return _ok_or_error(unix.sys_symlink(old_name_cstr, new_name_cstr)) } @@ -234,7 +236,7 @@ _read_link_cstr :: proc(name_cstr: cstring, allocator: runtime.Allocator) -> (st bufsz : uint = 256 buf := make([]byte, bufsz, allocator) for { - rc := unix.sys_readlink(name_cstr, &(buf[0]), bufsz) + rc := unix.sys_readlink(name_cstr, &buf[0], bufsz) if rc < 0 { delete(buf) return "", _get_platform_error(rc) @@ -243,26 +245,26 @@ _read_link_cstr :: proc(name_cstr: cstring, allocator: runtime.Allocator) -> (st delete(buf) buf = make([]byte, bufsz, allocator) } else { - return strings.string_from_ptr(&buf[0], rc), nil + return string(buf[:rc]), nil } } } -_read_link :: proc(name: string, allocator: runtime.Allocator) -> (string, Error) { +_read_link :: proc(name: string, allocator: runtime.Allocator) -> (path: string, err: Error) { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr := temp_cstring(name) or_return return _read_link_cstr(name_cstr, allocator) } _unlink :: proc(name: string) -> Error { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr := temp_cstring(name) or_return return _ok_or_error(unix.sys_unlink(name_cstr)) } _chdir :: proc(name: string) -> Error { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr := temp_cstring(name) or_return return _ok_or_error(unix.sys_chdir(name_cstr)) } @@ -272,7 +274,7 @@ _fchdir :: proc(f: ^File) -> Error { _chmod :: proc(name: string, mode: File_Mode) -> Error { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr := temp_cstring(name) or_return return _ok_or_error(unix.sys_chmod(name_cstr, uint(mode))) } @@ -283,14 +285,14 @@ _fchmod :: proc(f: ^File, mode: File_Mode) -> Error { // NOTE: will throw error without super user priviledges _chown :: proc(name: string, uid, gid: int) -> Error { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr := temp_cstring(name) or_return return _ok_or_error(unix.sys_chown(name_cstr, uid, gid)) } // NOTE: will throw error without super user priviledges _lchown :: proc(name: string, uid, gid: int) -> Error { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr := temp_cstring(name) or_return return _ok_or_error(unix.sys_lchown(name_cstr, uid, gid)) } @@ -301,7 +303,7 @@ _fchown :: proc(f: ^File, uid, gid: int) -> Error { _chtimes :: proc(name: string, atime, mtime: time.Time) -> Error { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr := temp_cstring(name) or_return times := [2]Unix_File_Time { { atime._nsec, 0 }, { mtime._nsec, 0 }, @@ -319,13 +321,13 @@ _fchtimes :: proc(f: ^File, atime, mtime: time.Time) -> Error { _exists :: proc(name: string) -> bool { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr, _ := temp_cstring(name) return unix.sys_access(name_cstr, F_OK) == 0 } _is_file :: proc(name: string) -> bool { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr, _ := temp_cstring(name) s: _Stat res := unix.sys_stat(name_cstr, &s) if res < 0 { @@ -345,7 +347,7 @@ _is_file_fd :: proc(fd: int) -> bool { _is_dir :: proc(name: string) -> bool { TEMP_ALLOCATOR_GUARD() - name_cstr := _temp_name_to_cstring(name) + name_cstr, _ := temp_cstring(name) s: _Stat res := unix.sys_stat(name_cstr, &s) if res < 0 { @@ -363,16 +365,6 @@ _is_dir_fd :: proc(fd: int) -> bool { return S_ISDIR(s.mode) } -// Ideally we want to use the temp_allocator. PATH_MAX on Linux is commonly -// defined as 512, however, it is well known that paths can exceed that limit. -// So, in theory you could have a path larger than the entire temp_allocator's -// buffer. Therefor, any large paths will use context.allocator. -@(private="file") -_temp_name_to_cstring :: proc(name: string) -> (cname: cstring) { - return strings.clone_to_cstring(name, temp_allocator()) -} - - @(private="package") _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) { f := (^File)(stream_data) diff --git a/core/os/os2/file_util.odin b/core/os/os2/file_util.odin index 9ec75fc91..977979bae 100644 --- a/core/os/os2/file_util.odin +++ b/core/os/os2/file_util.odin @@ -76,6 +76,7 @@ read_entire_file :: proc{ read_entire_file_from_file, } +@(require_results) read_entire_file_from_path :: proc(name: string, allocator: runtime.Allocator) -> (data: []byte, err: Error) { f, ferr := open(name) if ferr != nil { @@ -85,6 +86,7 @@ read_entire_file_from_path :: proc(name: string, allocator: runtime.Allocator) - return read_entire_file_from_file(f, allocator) } +@(require_results) read_entire_file_from_file :: proc(f: ^File, allocator: runtime.Allocator) -> (data: []byte, err: Error) { size: int has_size := true @@ -135,6 +137,7 @@ read_entire_file_from_file :: proc(f: ^File, allocator: runtime.Allocator) -> (d } } +@(require_results) write_entire_file :: proc(name: string, data: []byte, perm: File_Mode, truncate := true) -> Error { flags := O_WRONLY|O_CREATE if truncate { diff --git a/core/os/os2/heap.odin b/core/os/os2/heap.odin index a07a0d618..e0cffaf0d 100644 --- a/core/os/os2/heap.odin +++ b/core/os/os2/heap.odin @@ -2,6 +2,7 @@ package os2 import "base:runtime" +@(require_results) heap_allocator :: proc() -> runtime.Allocator { return runtime.Allocator{ procedure = heap_allocator_proc, @@ -10,6 +11,7 @@ heap_allocator :: proc() -> runtime.Allocator { } +@(require_results) heap_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode, size, alignment: int, old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, runtime.Allocator_Error) { diff --git a/core/os/os2/internal_util.odin b/core/os/os2/internal_util.odin index 69c3c8941..756343efd 100644 --- a/core/os/os2/internal_util.odin +++ b/core/os/os2/internal_util.odin @@ -7,6 +7,7 @@ import "base:runtime" // Splits pattern by the last wildcard "*", if it exists, and returns the prefix and suffix // parts which are split by the last "*" +@(require_results) _prefix_and_suffix :: proc(pattern: string) -> (prefix, suffix: string, err: Error) { for i in 0.. (prefix, suffix: string, err: Err return } -clone_string :: proc(s: string, allocator: runtime.Allocator) -> string { - buf := make([]byte, len(s), allocator) +@(require_results) +clone_string :: proc(s: string, allocator: runtime.Allocator) -> (res: string, err: runtime.Allocator_Error) { + buf := make([]byte, len(s), allocator) or_return copy(buf, s) - return string(buf) + return string(buf), nil } +@(require_results) +clone_to_cstring :: proc(s: string, allocator: runtime.Allocator) -> (res: cstring, err: runtime.Allocator_Error) { + res = "" // do not use a `nil` cstring + buf := make([]byte, len(s)+1, allocator) or_return + copy(buf, s) + buf[len(s)] = 0 + return cstring(&buf[0]), nil +} + +@(require_results) +temp_cstring :: proc(s: string) -> (cstring, runtime.Allocator_Error) { + return clone_to_cstring(s, temp_allocator()) +} + + + + +@(require_results) concatenate_strings_from_buffer :: proc(buf: []byte, strings: ..string) -> string { n := 0 for s in strings { @@ -57,6 +77,7 @@ init_random_string_seed :: proc() { _ = next_random(s) } +@(require_results) next_random :: proc(r: ^[2]u64) -> u64 { old_state := r[0] r[0] = old_state * 6364136223846793005 + (r[1]|1) @@ -65,6 +86,7 @@ next_random :: proc(r: ^[2]u64) -> u64 { return (xor_shifted >> rot) | (xor_shifted << ((-rot) & 63)) } +@(require_results) random_string :: proc(buf: []byte) -> string { @static digits := "0123456789" diff --git a/core/os/os2/path.odin b/core/os/os2/path.odin index 277da56dd..27c3d6b0b 100644 --- a/core/os/os2/path.odin +++ b/core/os/os2/path.odin @@ -6,6 +6,7 @@ Path_Separator :: _Path_Separator // OS-Specific Path_Separator_String :: _Path_Separator_String // OS-Specific Path_List_Separator :: _Path_List_Separator // OS-Specific +@(require_results) is_path_separator :: proc(c: byte) -> bool { return _is_path_separator(c) } @@ -26,6 +27,7 @@ remove_all :: proc(path: string) -> Error { getwd :: get_working_directory +@(require_results) get_working_directory :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { return _getwd(allocator) } diff --git a/core/os/os2/path_linux.odin b/core/os/os2/path_linux.odin index fde4ba5b1..aaaafa2b4 100644 --- a/core/os/os2/path_linux.odin +++ b/core/os/os2/path_linux.odin @@ -32,7 +32,8 @@ _mkdir :: proc(path: string, perm: File_Mode) -> Error { return .Invalid_Argument } - path_cstr := strings.clone_to_cstring(path, context.temp_allocator) + TEMP_ALLOCATOR_GUARD() + path_cstr := strings.clone_to_cstring(path, temp_allocator()) return _ok_or_error(unix.sys_mkdir(path_cstr, uint(perm & 0o777))) } diff --git a/core/os/os2/pipe.odin b/core/os/os2/pipe.odin index 62f7ddf10..9254d6f8e 100644 --- a/core/os/os2/pipe.odin +++ b/core/os/os2/pipe.odin @@ -1,5 +1,6 @@ package os2 +@(require_results) pipe :: proc() -> (r, w: ^File, err: Error) { return _pipe() } diff --git a/core/os/os2/stat.odin b/core/os/os2/stat.odin index a64522ac1..16f319046 100644 --- a/core/os/os2/stat.odin +++ b/core/os/os2/stat.odin @@ -25,20 +25,24 @@ file_info_delete :: proc(fi: File_Info, allocator: runtime.Allocator) { delete(fi.fullpath, allocator) } +@(require_results) fstat :: proc(f: ^File, allocator: runtime.Allocator) -> (File_Info, Error) { return _fstat(f, allocator) } +@(require_results) stat :: proc(name: string, allocator: runtime.Allocator) -> (File_Info, Error) { return _stat(name, allocator) } lstat :: stat_do_not_follow_links +@(require_results) stat_do_not_follow_links :: proc(name: string, allocator: runtime.Allocator) -> (File_Info, Error) { return _lstat(name, allocator) } +@(require_results) same_file :: proc(fi1, fi2: File_Info) -> bool { return _same_file(fi1, fi2) } diff --git a/core/os/os2/temp_file.odin b/core/os/os2/temp_file.odin index a1be8f690..fa71f44a7 100644 --- a/core/os/os2/temp_file.odin +++ b/core/os/os2/temp_file.odin @@ -13,6 +13,7 @@ MAX_ATTEMPTS :: 1<<13 // Should be enough for everyone, right? // If `dir` is an empty tring, `temp_directory()` will be used. // // The caller must `close` the file once finished with. +@(require_results) 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 @@ -44,6 +45,7 @@ mkdir_temp :: make_directory_temp // The directory name is generated by taking a pattern, and adding a randomized string to the end. // If the pattern includes an "*", the randm string replaces the last "*". // If `dir` is an empty tring, `temp_directory()` will be used. +@(require_results) make_directory_temp :: proc(dir, pattern: string, allocator: runtime.Allocator) -> (temp_path: string, err: Error) { TEMP_ALLOCATOR_GUARD() dir := dir if dir != "" else temp_directory(temp_allocator()) or_return @@ -58,7 +60,7 @@ make_directory_temp :: proc(dir, pattern: string, allocator: runtime.Allocator) name := concatenate_strings_from_buffer(name_buf[:], prefix, random_string(rand_buf[:]), suffix) err = make_directory(name, 0o700) if err == nil { - return clone_string(name, allocator), nil + return clone_string(name, allocator) } if err == .Exist { attempts += 1 @@ -78,6 +80,7 @@ make_directory_temp :: proc(dir, pattern: string, allocator: runtime.Allocator) } temp_dir :: temp_directory +@(require_results) temp_directory :: proc(allocator: runtime.Allocator) -> (string, Error) { return _temp_dir(allocator) } diff --git a/core/os/os2/user.odin b/core/os/os2/user.odin index 0af461bf5..a4ef7c1dd 100644 --- a/core/os/os2/user.odin +++ b/core/os/os2/user.odin @@ -3,6 +3,7 @@ package os2 import "core:strings" import "base:runtime" +@(require_results) user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { #partial switch ODIN_OS { case .Windows: @@ -31,6 +32,7 @@ user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error return } +@(require_results) user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { #partial switch ODIN_OS { case .Windows: @@ -59,6 +61,7 @@ user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Erro return } +@(require_results) user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { env := "HOME" #partial switch ODIN_OS {