lib_unix.odin 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #+build linux, darwin, freebsd, openbsd, netbsd
  2. #+private
  3. package dynlib
  4. import "base:runtime"
  5. import "core:strings"
  6. import "core:sys/posix"
  7. _LIBRARY_FILE_EXTENSION :: "dylib" when ODIN_OS == .Darwin else "so"
  8. _load_library :: proc(path: string, global_symbols: bool, allocator: runtime.Allocator) -> (Library, bool) {
  9. flags := posix.RTLD_Flags{.NOW}
  10. if global_symbols {
  11. flags += {.GLOBAL}
  12. } else {
  13. flags += posix.RTLD_LOCAL
  14. }
  15. cpath := strings.clone_to_cstring(path, allocator)
  16. defer delete(cpath, allocator)
  17. lib := posix.dlopen(cpath, flags)
  18. return Library(lib), lib != nil
  19. }
  20. _unload_library :: proc(library: Library) -> bool {
  21. return posix.dlclose(posix.Symbol_Table(library)) == 0
  22. }
  23. _symbol_address :: proc(library: Library, symbol: string, allocator: runtime.Allocator) -> (ptr: rawptr, found: bool) {
  24. csymbol := strings.clone_to_cstring(symbol, allocator)
  25. defer delete(csymbol, allocator)
  26. ptr = posix.dlsym(posix.Symbol_Table(library), csymbol)
  27. found = ptr != nil
  28. return
  29. }
  30. _last_error :: proc() -> string {
  31. err := string(posix.dlerror())
  32. return "unknown" if err == "" else err
  33. }