Documentation
¶
Overview ¶
Package dl is the one safe way to load C libraries and call into them without cgo: it wraps purego's Dlopen, Dlsym and callbacks, tags every failure, and offers the libc helpers (malloc, free, C strings) and the struct-layout test helper that the backends share.
The rules for calling C through purego are in docs/SPEC.md §8.1. Only the UI thread calls into the libraries opened here.
Flutter: no counterpart.
Index ¶
- Constants
- func AssertLayout(t testing.TB, name string, size uintptr, offsets map[string]uintptr, got any)
- func Bytes(addr uintptr, n int) []byte
- func CString(s string) uintptr
- func Callback(fn any) uintptr
- func CallbackCount() int
- func Calloc(count, size uintptr) uintptr
- func Free(ptr uintptr)
- func GoString(ptr uintptr) string
- func Malloc(size uintptr) uintptr
- func Pointer(addr uintptr) unsafe.Pointer
- func Strlen(ptr uintptr) uintptr
- type Lib
Constants ¶
const MaxCallbacks = 1500
MaxCallbacks is how many C→Go callbacks Callback creates before it panics with CallbackLimit. purego allows about 2,000 per process and never frees them, so this leaves a safety margin (SPEC §8.1 rule 2).
Flutter: no counterpart
Variables ¶
This section is empty.
Functions ¶
func AssertLayout ¶
AssertLayout is a test helper for the layout test that every Go mirror of a C struct needs (SPEC §8.1 rule 3). It checks that the struct value got has the given size and the given field offsets, as taken from the C headers and recorded in the test. Every named field of got must be listed in offsets, so that a field added later cannot escape the test. Fields named "_" are padding and are skipped. It reports every difference with t.Errorf.
Flutter: no counterpart
func Bytes ¶
Bytes returns a slice that views n bytes of C memory at addr, without copying. It is valid until the C memory is freed. Address 0 or n 0 gives nil.
Flutter: no counterpart
func CString ¶
CString copies s into new C memory as a NUL-terminated string and returns its address. The caller releases it with Free. An s with a NUL byte inside reads as truncated on the C side. It panics with CStringAllocFailed when malloc fails.
Flutter: no counterpart
func Callback ¶
Callback converts a Go function into a C function pointer. Create callbacks once, at startup, and dispatch by user data: they are never freed, and a process can have only about 2,000. After MaxCallbacks it panics with CallbackLimit. The arguments of fn must fit in a pointer (SPEC §8.1 rule 2).
Flutter: no counterpart
func CallbackCount ¶
func CallbackCount() int
CallbackCount returns how many callbacks Callback has created.
Flutter: no counterpart
func Calloc ¶
Calloc allocates count*size bytes of zeroed C memory and returns its address, or 0 when the allocation fails. Release it with Free.
Flutter: no counterpart
func Free ¶
func Free(ptr uintptr)
Free releases C memory from Malloc, Calloc or CString. Freeing 0 does nothing.
Flutter: no counterpart
func GoString ¶
GoString copies the NUL-terminated C string at ptr into a Go string. The address 0 gives "".
Flutter: no counterpart
func Malloc ¶
Malloc allocates size bytes of C memory and returns its address, or 0 when the allocation fails. Release it with Free. C memory is where data that C keeps must live (SPEC §8.1 rule 3).
Flutter: no counterpart
func Pointer ¶
Pointer converts the address of C memory to an unsafe.Pointer. Use it only for addresses of memory that C owns or that came from Malloc, Calloc or CString, never for Go memory, which the garbage collector may move. It goes through a pointer to the address, so go vet's unsafeptr check, which targets uintptr-to-Pointer conversions of Go memory, does not apply.
Flutter: no counterpart
Types ¶
type Lib ¶
type Lib struct {
// contains filtered or unexported fields
}
Lib is an open C library.
Flutter: no counterpart
func Open ¶
Open loads the first of names that dlopen accepts, with RTLD_NOW|RTLD_LOCAL. Give the versioned name first (such as "libEGL.so.1"), because the unversioned symlink exists only when the -devel package is installed. If no name loads, Open fails with LibraryLoadFailed and the metadata "names" and "dlerror" (the message of the last failed attempt).
Flutter: no counterpart
func OpenOverride ¶
OpenOverride is like Open, except that when the environment variable env holds an absolute path, only that path is tried. This is for unusual setups, such as GLUTTER_EGL_LIBRARY on NixOS. A set but failing override is an error (it does not fall back to names), and a value that is not an absolute path fails with LibraryOverrideInvalid.
Flutter: no counterpart
func (*Lib) Bind ¶
Bind sets fptr, a pointer to a Go func variable, to call the C function symbol. The func's type describes the C signature as purego defines it. It fails with SymbolMissing (metadata "library", "symbol") when the library has no such symbol. A fptr that is not a pointer to a func is a programmer error and panics.
Flutter: no counterpart