lib_unix.odin 1008 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. _load_library :: proc(path: string, global_symbols: bool, allocator: runtime.Allocator) -> (Library, bool) {
  8. flags := posix.RTLD_Flags{.NOW}
  9. if global_symbols {
  10. flags += {.GLOBAL}
  11. }
  12. cpath := strings.clone_to_cstring(path, allocator)
  13. defer delete(cpath, allocator)
  14. lib := posix.dlopen(cpath, flags)
  15. return Library(lib), lib != nil
  16. }
  17. _unload_library :: proc(library: Library) -> bool {
  18. return posix.dlclose(posix.Symbol_Table(library)) == 0
  19. }
  20. _symbol_address :: proc(library: Library, symbol: string, allocator: runtime.Allocator) -> (ptr: rawptr, found: bool) {
  21. csymbol := strings.clone_to_cstring(symbol, allocator)
  22. defer delete(csymbol, allocator)
  23. ptr = posix.dlsym(posix.Symbol_Table(library), csymbol)
  24. found = ptr != nil
  25. return
  26. }
  27. _last_error :: proc() -> string {
  28. err := string(posix.dlerror())
  29. return "unknown" if err == "" else err
  30. }