diff options
| author | Laytan <laytanlaats@hotmail.com> | 2024-10-28 19:21:16 +0100 |
|---|---|---|
| committer | Laytan <laytanlaats@hotmail.com> | 2024-10-28 19:21:16 +0100 |
| commit | 0b4a4212bbd156ab8c2ce3345feb1d9ef859b629 (patch) | |
| tree | 570841d05aee9acb7969524d79c32fd768f04c5b /core/dynlib | |
| parent | afed3ce6b5ae00d12e46f2799f16e286369ef3b3 (diff) | |
clean up dynlib and path/filepath with sys/posix
Diffstat (limited to 'core/dynlib')
| -rw-r--r-- | core/dynlib/lib.odin | 10 | ||||
| -rw-r--r-- | core/dynlib/lib_js.odin | 8 | ||||
| -rw-r--r-- | core/dynlib/lib_unix.odin | 30 | ||||
| -rw-r--r-- | core/dynlib/lib_windows.odin | 8 |
4 files changed, 35 insertions, 21 deletions
diff --git a/core/dynlib/lib.odin b/core/dynlib/lib.odin index 09e16002d..84675a560 100644 --- a/core/dynlib/lib.odin +++ b/core/dynlib/lib.odin @@ -37,8 +37,8 @@ Example: fmt.println("The library %q was successfully loaded", LIBRARY_PATH) } */ -load_library :: proc(path: string, global_symbols := false) -> (library: Library, did_load: bool) { - return _load_library(path, global_symbols) +load_library :: proc(path: string, global_symbols := false, allocator := context.temp_allocator) -> (library: Library, did_load: bool) { + return _load_library(path, global_symbols, allocator) } /* @@ -98,8 +98,8 @@ Example: } } */ -symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) #optional_ok { - return _symbol_address(library, symbol) +symbol_address :: proc(library: Library, symbol: string, allocator := context.temp_allocator) -> (ptr: rawptr, found: bool) #optional_ok { + return _symbol_address(library, symbol, allocator) } /* @@ -174,4 +174,4 @@ initialize_symbols :: proc( // Returns an error message for the last failed procedure call. last_error :: proc() -> string { return _last_error() -}
\ No newline at end of file +} diff --git a/core/dynlib/lib_js.odin b/core/dynlib/lib_js.odin index 698cfee9c..b99143ba0 100644 --- a/core/dynlib/lib_js.odin +++ b/core/dynlib/lib_js.odin @@ -2,7 +2,9 @@ #+private package dynlib -_load_library :: proc(path: string, global_symbols := false) -> (Library, bool) { +import "base:runtime" + +_load_library :: proc(path: string, global_symbols: bool, allocator: runtime.Allocator) -> (Library, bool) { return nil, false } @@ -10,10 +12,10 @@ _unload_library :: proc(library: Library) -> bool { return false } -_symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) { +_symbol_address :: proc(library: Library, symbol: string, allocator: runtime.Allocator) -> (ptr: rawptr, found: bool) { return nil, false } _last_error :: proc() -> string { return "" -}
\ No newline at end of file +} diff --git a/core/dynlib/lib_unix.odin b/core/dynlib/lib_unix.odin index f467d730d..337bf496d 100644 --- a/core/dynlib/lib_unix.odin +++ b/core/dynlib/lib_unix.odin @@ -2,28 +2,38 @@ #+private package dynlib -import "core:os" +import "base:runtime" -_load_library :: proc(path: string, global_symbols := false) -> (Library, bool) { - flags := os.RTLD_NOW +import "core:strings" +import "core:sys/posix" + +_load_library :: proc(path: string, global_symbols: bool, allocator: runtime.Allocator) -> (Library, bool) { + flags := posix.RTLD_Flags{.NOW} if global_symbols { - flags |= os.RTLD_GLOBAL + flags += {.GLOBAL} } - lib := os.dlopen(path, flags) + + cpath := strings.clone_to_cstring(path, allocator) + defer delete(cpath, allocator) + + lib := posix.dlopen(cpath, flags) return Library(lib), lib != nil } _unload_library :: proc(library: Library) -> bool { - return os.dlclose(rawptr(library)) + return posix.dlclose(posix.Symbol_Table(library)) == 0 } -_symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) { - ptr = os.dlsym(rawptr(library), symbol) +_symbol_address :: proc(library: Library, symbol: string, allocator: runtime.Allocator) -> (ptr: rawptr, found: bool) { + csymbol := strings.clone_to_cstring(symbol, allocator) + defer delete(csymbol, allocator) + + ptr = posix.dlsym(posix.Symbol_Table(library), csymbol) found = ptr != nil return } _last_error :: proc() -> string { - err := os.dlerror() + err := string(posix.dlerror()) return "unknown" if err == "" else err -}
\ No newline at end of file +} diff --git a/core/dynlib/lib_windows.odin b/core/dynlib/lib_windows.odin index 6c41a1a75..928a1510d 100644 --- a/core/dynlib/lib_windows.odin +++ b/core/dynlib/lib_windows.odin @@ -2,11 +2,13 @@ #+private package dynlib +import "base:runtime" + import win32 "core:sys/windows" import "core:strings" import "core:reflect" -_load_library :: proc(path: string, global_symbols := false, allocator := context.temp_allocator) -> (Library, bool) { +_load_library :: proc(path: string, global_symbols: bool, allocator: runtime.Allocator) -> (Library, bool) { // NOTE(bill): 'global_symbols' is here only for consistency with POSIX which has RTLD_GLOBAL wide_path := win32.utf8_to_wstring(path, allocator) defer free(wide_path, allocator) @@ -19,7 +21,7 @@ _unload_library :: proc(library: Library) -> bool { return bool(ok) } -_symbol_address :: proc(library: Library, symbol: string, allocator := context.temp_allocator) -> (ptr: rawptr, found: bool) { +_symbol_address :: proc(library: Library, symbol: string, allocator: runtime.Allocator) -> (ptr: rawptr, found: bool) { c_str := strings.clone_to_cstring(symbol, allocator) defer delete(c_str, allocator) ptr = win32.GetProcAddress(cast(win32.HMODULE)library, c_str) @@ -31,4 +33,4 @@ _last_error :: proc() -> string { err := win32.System_Error(win32.GetLastError()) err_msg := reflect.enum_string(err) return "unknown" if err_msg == "" else err_msg -}
\ No newline at end of file +} |