Skip to contents

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" for libm.so.6), or NULL for 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 a zuffi_range_error when not; or "zuint", as the zuint package's int64 and uint64 types (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 versioned libzstd.so.<n> files, taking the highest version unless version pins one. The unversioned name usually exists only where a -dev package is installed, and some (libc.so, often libm.so) are linker scripts the loader refuses, so the versioned name is the common case. The directories are those of path, then LD_LIBRARY_PATH, then the loader's (/etc/ld.so.conf and the system directories).

  • macOS: libzstd.dylib (libzstd.<version>.dylib with version), in the directories of path, then DYLD_LIBRARY_PATH, then DYLD_FALLBACK_LIBRARY_PATH, then /usr/local/lib and /usr/lib. System libraries live in the dyld shared cache and open by these paths though no file exists.

  • Windows: zstd.dll, then libzstd.dll (zstd-<version>.dll and libzstd-<version>.dll with version), in the directories of path and 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).

Examples

lib <- ffi_dlopen(if (.Platform$OS.type == "windows") "ucrtbase" else "m")
lib
#> <zuffi_lib> /usr/lib/x86_64-linux-gnu/libm.so.6
#> int64: double
#> 0 functions
ffi_dlsym(lib, "cos")
#> <zuffi_ptr void * 0x72f84cf393a0>
ffi_close(lib)