aboutsummaryrefslogtreecommitdiff
path: root/core/dynlib
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2023-03-02 12:07:32 +0000
committerGitHub <noreply@github.com>2023-03-02 12:07:32 +0000
commit553f338f6ffa60ead4931ce83b302c822caf8f0a (patch)
tree0d802dc8fe73eb5897906ac52e81c3a55c5064d7 /core/dynlib
parent3567c006e6683d989805c078db48a95a901d9e72 (diff)
parentbb72c804fb6256caf074f6e1c9aa1d42c231b4d6 (diff)
Merge pull request #2360 from Lperlind/documentation/dynlib
Document core:dynlib
Diffstat (limited to 'core/dynlib')
-rw-r--r--core/dynlib/doc.odin7
-rw-r--r--core/dynlib/lib.odin83
2 files changed, 88 insertions, 2 deletions
diff --git a/core/dynlib/doc.odin b/core/dynlib/doc.odin
new file mode 100644
index 000000000..812fb02d5
--- /dev/null
+++ b/core/dynlib/doc.odin
@@ -0,0 +1,7 @@
+/*
+Package core:dynlib implements loading of shared libraries/DLLs and their symbols.
+
+The behaviour of dynamically loaded libraries is specific to the target platform of the program.
+For in depth detail on the underlying behaviour please refer to your target platform's documentation.
+*/
+package dynlib
diff --git a/core/dynlib/lib.odin b/core/dynlib/lib.odin
index a6c857ee4..b5cb16e3c 100644
--- a/core/dynlib/lib.odin
+++ b/core/dynlib/lib.odin
@@ -1,15 +1,94 @@
package dynlib
+/*
+A handle to a dynamically loaded library.
+*/
Library :: distinct rawptr
-load_library :: proc(path: string, global_symbols := false) -> (Library, bool) {
+/*
+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.
+
+The paramater `global_symbols` is only used for the platforms `linux`, `darwin`, `freebsd` and `openbsd`.
+On `windows` this paramater is ignored.
+
+The underlying behaviour is platform specific.
+On `linux`, `darwin`, `freebsd` and `openbsd` refer to `dlopen`.
+On `windows` refer to `LoadLibraryW`.
+
+**Implicit Allocators**
+`context.temp_allocator`
+
+Example:
+ import "core:dynlib"
+ import "core:fmt"
+
+ load_my_library :: proc() {
+ LIBRARY_PATH :: "my_library.dll"
+ library, ok := dynlib.load_library(LIBRARY_PATH)
+ if ! ok {
+ return
+ }
+ 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)
}
-unload_library :: proc(library: Library) -> bool {
+/*
+Unloads a dynamic library.
+
+The underlying behaviour is platform specific.
+On `linux`, `darwin`, `freebsd` and `openbsd` refer to `dlclose`.
+On `windows` refer to `FreeLibrary`.
+
+Example:
+ import "core:dynlib"
+ import "core:fmt"
+
+ load_then_unload_my_library :: proc() {
+ LIBRARY_PATH :: "my_library.dll"
+ library, ok := dynlib.load_library(LIBRARY_PATH)
+ if ! ok {
+ return
+ }
+ did_unload := dynlib.unload_library(library)
+ if ! did_unload {
+ return
+ }
+ fmt.println("The library %q was successfully unloaded", LIBRARY_PATH)
+ }
+*/
+unload_library :: proc(library: Library) -> (did_unload: bool) {
return _unload_library(library)
}
+/*
+Loads the address of a procedure/variable from a dynamic library.
+
+The underlying behaviour is platform specific.
+On `linux`, `darwin`, `freebsd` and `openbsd` refer to `dlsym`.
+On `windows` refer to `GetProcAddress`.
+
+**Implicit Allocators**
+`context.temp_allocator`
+
+Example:
+ import "core:dynlib"
+ import "core:fmt"
+
+ find_a_in_my_library :: proc() {
+ LIBRARY_PATH :: "my_library.dll"
+ library, ok := dynlib.load_library(LIBRARY_PATH)
+ if ! ok {
+ return
+ }
+
+ a, found_a := dynlib.symbol_address(library, "a")
+ if found_a do fmt.printf("The symbol %q was found at the address %v", "a", a)
+ }
+*/
symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) #optional_ok {
return _symbol_address(library, symbol)
}