ffi_dlopen() opens a shared library and returns its handle, an
environment into which declarations bind one R function per C function,
so that lib$f(x) calls f. ffi_dlsym() returns the address of a
symbol. ffi_close() unloads the library.
Usage
ffi_dlopen(name, version = NULL, path = NULL, int64 = c("double", "zuint"))
ffi_dlsym(lib, symbol)
ffi_close(lib)Arguments
- name
The library: a stem such as
"m", a file name, or a path.- version
For a stem, the version to open (
"6"forlibm.so.6), orNULLfor the highest found.- path
Directories to search before the system's, or
NULL.- int64
How 64-bit integer results arrive:
"double", as doubles when exact and azuffi_range_errorwhen not; or"zuint", as the zuint package'sint64anduint64types (design D5). The choice belongs to the handle, so a script returns the same types on every machine.- lib
A handle from
ffi_dlopen().- symbol
The symbol's name.
Value
ffi_dlopen(): the handle, of class zuffi_lib.
ffi_dlsym(): a borrowed void * zuffi_ptr to the symbol, which
keeps the library reachable. ffi_close(): lib, invisibly.
Details
A name with a path separator, or with a library extension (.so,
.so.6, .dylib, .dll), is opened as given. Anything else is a stem,
and ffi_dlopen("zstd") looks for the platform's names:
Linux:
libzstd.so, then the versionedlibzstd.so.<n>files, taking the highest version unlessversionpins one. The unversioned name usually exists only where a-devpackage is installed, and some (libc.so, oftenlibm.so) are linker scripts the loader refuses, so the versioned name is the common case. The directories are those ofpath, thenLD_LIBRARY_PATH, then the loader's (/etc/ld.so.confand the system directories).macOS:
libzstd.dylib(libzstd.<version>.dylibwithversion), in the directories ofpath, thenDYLD_LIBRARY_PATH, thenDYLD_FALLBACK_LIBRARY_PATH, then/usr/local/liband/usr/lib. System libraries live in the dyld shared cache and open by these paths though no file exists.Windows:
zstd.dll, thenlibzstd.dll(zstd-<version>.dllandlibzstd-<version>.dllwithversion), in the directories ofpathand then the system directories.
The working directory is never searched. The handle records the path of the file that was opened and prints it.
The garbage collector never unloads a library, because one with running
threads or exit handlers can crash the process when unloaded; only
ffi_close() does. After it, every function of the handle raises a
zuffi_closed_error. ffi_close() refuses, with a zuffi_busy_error,
while a pointer whose free function is in the library is still owned
(see ffi_own()): it collects garbage once first, so a pointer nothing
refers to any more is freed rather than counted (design D11).