Remove implicit allocator usage in core:dynlib

This commit is contained in:
Jeroen van Rijn
2024-07-04 16:53:00 +02:00
parent 1eb0bc1408
commit 8bee73b08e
4 changed files with 14 additions and 24 deletions
+5 -13
View File
@@ -16,15 +16,12 @@ Library :: distinct rawptr
Loads a dynamic library from the filesystem. The paramater `global_symbols` makes the symbols in the loaded Loads a dynamic library from the filesystem. The paramater `global_symbols` makes the symbols in the loaded
library available to resolve references in subsequently loaded libraries. library available to resolve references in subsequently loaded libraries.
The paramater `global_symbols` is only used for the platforms `linux`, `darwin`, `freebsd` and `openbsd`. The parameter `global_symbols` is only used for the platforms `linux`, `darwin`, `freebsd` and `openbsd`.
On `windows` this paramater is ignored. On `windows` this paramater is ignored.
The underlying behaviour is platform specific. The underlying behaviour is platform specific.
On `linux`, `darwin`, `freebsd` and `openbsd` refer to `dlopen`. On `linux`, `darwin`, `freebsd` and `openbsd` refer to `dlopen`.
On `windows` refer to `LoadLibraryW`. On `windows` refer to `LoadLibraryW`. Also temporarily needs an allocator to convert a string.
**Implicit Allocators**
`context.temp_allocator`
Example: Example:
import "core:dynlib" import "core:dynlib"
@@ -79,10 +76,7 @@ Loads the address of a procedure/variable from a dynamic library.
The underlying behaviour is platform specific. The underlying behaviour is platform specific.
On `linux`, `darwin`, `freebsd` and `openbsd` refer to `dlsym`. On `linux`, `darwin`, `freebsd` and `openbsd` refer to `dlsym`.
On `windows` refer to `GetProcAddress`. On `windows` refer to `GetProcAddress`. Also temporarily needs an allocator to convert a string.
**Implicit Allocators**
`context.temp_allocator`
Example: Example:
import "core:dynlib" import "core:dynlib"
@@ -177,9 +171,7 @@ initialize_symbols :: proc(
return count, count > 0 return count, count > 0
} }
/* // 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()
} }
+1 -1
View File
@@ -16,4 +16,4 @@ _symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found
_last_error :: proc() -> string { _last_error :: proc() -> string {
return "" return ""
} }
+1 -1
View File
@@ -26,4 +26,4 @@ _symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found
_last_error :: proc() -> string { _last_error :: proc() -> string {
err := os.dlerror() err := os.dlerror()
return "unknown" if err == "" else err return "unknown" if err == "" else err
} }
+7 -9
View File
@@ -4,14 +4,12 @@ package dynlib
import win32 "core:sys/windows" import win32 "core:sys/windows"
import "core:strings" import "core:strings"
import "base:runtime"
import "core:reflect" import "core:reflect"
_load_library :: proc(path: string, global_symbols := false) -> (Library, bool) { _load_library :: proc(path: string, global_symbols := false, allocator := context.temp_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)
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() defer free(wide_path, allocator)
wide_path := win32.utf8_to_wstring(path, context.temp_allocator)
handle := cast(Library)win32.LoadLibraryW(wide_path) handle := cast(Library)win32.LoadLibraryW(wide_path)
return handle, handle != nil return handle, handle != nil
} }
@@ -21,9 +19,9 @@ _unload_library :: proc(library: Library) -> bool {
return bool(ok) return bool(ok)
} }
_symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) { _symbol_address :: proc(library: Library, symbol: string, allocator := context.temp_allocator) -> (ptr: rawptr, found: bool) {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() c_str := strings.clone_to_cstring(symbol, allocator)
c_str := strings.clone_to_cstring(symbol, context.temp_allocator) defer delete(c_str, allocator)
ptr = win32.GetProcAddress(cast(win32.HMODULE)library, c_str) ptr = win32.GetProcAddress(cast(win32.HMODULE)library, c_str)
found = ptr != nil found = ptr != nil
return return
@@ -33,4 +31,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
} }