Browse Source

clean up dynlib and path/filepath with sys/posix

Laytan 9 months ago
parent
commit
0b4a4212bb

+ 5 - 5
core/dynlib/lib.odin

@@ -37,8 +37,8 @@ Example:
 		fmt.println("The library %q was successfully loaded", LIBRARY_PATH)
 		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.
 // Returns an error message for the last failed procedure call.
 last_error :: proc() -> string {
 last_error :: proc() -> string {
 	return _last_error()
 	return _last_error()
-}
+}

+ 5 - 3
core/dynlib/lib_js.odin

@@ -2,7 +2,9 @@
 #+private
 #+private
 package dynlib
 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
 	return nil, false
 }
 }
 
 
@@ -10,10 +12,10 @@ _unload_library :: proc(library: Library) -> bool {
 	return false
 	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
 	return nil, false
 }
 }
 
 
 _last_error :: proc() -> string {
 _last_error :: proc() -> string {
 	return ""
 	return ""
-}
+}

+ 20 - 10
core/dynlib/lib_unix.odin

@@ -2,28 +2,38 @@
 #+private
 #+private
 package dynlib
 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 {
 	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
 	return Library(lib), lib != nil
 }
 }
 
 
 _unload_library :: proc(library: Library) -> bool {
 _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
 	found = ptr != nil
 	return
 	return
 }
 }
 
 
 _last_error :: proc() -> string {
 _last_error :: proc() -> string {
-	err := os.dlerror()
+	err := string(posix.dlerror())
 	return "unknown" if err == "" else err
 	return "unknown" if err == "" else err
-}
+}

+ 5 - 3
core/dynlib/lib_windows.odin

@@ -2,11 +2,13 @@
 #+private
 #+private
 package dynlib
 package dynlib
 
 
+import "base:runtime"
+
 import win32 "core:sys/windows"
 import win32 "core:sys/windows"
 import "core:strings"
 import "core:strings"
 import "core:reflect"
 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
 	// NOTE(bill): 'global_symbols' is here only for consistency with POSIX which has RTLD_GLOBAL
 	wide_path := win32.utf8_to_wstring(path, allocator)
 	wide_path := win32.utf8_to_wstring(path, allocator)
 	defer free(wide_path, allocator)
 	defer free(wide_path, allocator)
@@ -19,7 +21,7 @@ _unload_library :: proc(library: Library) -> bool {
 	return bool(ok)
 	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)
 	c_str := strings.clone_to_cstring(symbol, allocator)
 	defer delete(c_str, allocator)
 	defer delete(c_str, allocator)
 	ptr = win32.GetProcAddress(cast(win32.HMODULE)library, c_str)
 	ptr = win32.GetProcAddress(cast(win32.HMODULE)library, c_str)
@@ -31,4 +33,4 @@ _last_error :: proc() -> string {
 	err := win32.System_Error(win32.GetLastError())
 	err := win32.System_Error(win32.GetLastError())
 	err_msg := reflect.enum_string(err)
 	err_msg := reflect.enum_string(err)
 	return "unknown" if err_msg == "" else err_msg
 	return "unknown" if err_msg == "" else err_msg
-}
+}

+ 5 - 32
core/path/filepath/path_unix.odin

@@ -1,14 +1,10 @@
 #+build linux, darwin, freebsd, openbsd, netbsd
 #+build linux, darwin, freebsd, openbsd, netbsd
 package filepath
 package filepath
 
 
-when ODIN_OS == .Darwin {
-	foreign import libc "system:System.framework"
-} else {
-	foreign import libc "system:c"
-}
-
 import "base:runtime"
 import "base:runtime"
+
 import "core:strings"
 import "core:strings"
+import "core:sys/posix"
 
 
 SEPARATOR :: '/'
 SEPARATOR :: '/'
 SEPARATOR_STRING :: `/`
 SEPARATOR_STRING :: `/`
@@ -28,11 +24,11 @@ abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
 		rel = "."
 		rel = "."
 	}
 	}
 	rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
 	rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
-	path_ptr := realpath(rel_cstr, nil)
+	path_ptr := posix.realpath(rel_cstr, nil)
 	if path_ptr == nil {
 	if path_ptr == nil {
-		return "", __error()^ == 0
+		return "", posix.errno() == nil
 	}
 	}
-	defer _unix_free(rawptr(path_ptr))
+	defer posix.free(path_ptr)
 
 
 	path_str := strings.clone(string(path_ptr), allocator)
 	path_str := strings.clone(string(path_ptr), allocator)
 	return path_str, true
 	return path_str, true
@@ -48,26 +44,3 @@ join :: proc(elems: []string, allocator := context.allocator) -> (joined: string
 	}
 	}
 	return "", nil
 	return "", nil
 }
 }
-
-@(private)
-foreign libc {
-	realpath :: proc(path: cstring, resolved_path: [^]byte = nil) -> cstring ---
-	@(link_name="free") _unix_free :: proc(ptr: rawptr) ---
-
-}
-when ODIN_OS == .Darwin || ODIN_OS == .FreeBSD {
-	@(private)
-	foreign libc {
-		@(link_name="__error")          __error :: proc() -> ^i32 ---
-	}
-} else when ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD {
-	@(private)
-	foreign libc {
-		@(link_name="__errno")		__error :: proc() -> ^i32 ---
-	}
-} else {
-	@(private)
-	foreign libc {
-		@(link_name="__errno_location") __error :: proc() -> ^i32 ---
-	}
-}