dl

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func AssertLayout(t testing.TB, name string, size uintptr, offsets map[string]uintptr, got any)

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

func Bytes(addr uintptr, n int) []byte

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

func CString(s string) uintptr

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

func Callback(fn any) uintptr

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

func Calloc(count, size uintptr) uintptr

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

func GoString(ptr uintptr) string

GoString copies the NUL-terminated C string at ptr into a Go string. The address 0 gives "".

Flutter: no counterpart

func Malloc

func Malloc(size uintptr) uintptr

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

func Pointer(addr uintptr) unsafe.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

func Strlen

func Strlen(ptr uintptr) uintptr

Strlen returns the length in bytes of the NUL-terminated C string at ptr, not counting the NUL.

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

func Open(names ...string) (*Lib, error)

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

func OpenOverride(env string, names ...string) (*Lib, error)

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

func (l *Lib) Bind(fptr any, symbol string) error

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

func (*Lib) Has

func (l *Lib) Has(symbol string) bool

Has reports whether the library has the symbol, without failing.

Flutter: no counterpart

func (*Lib) Name

func (l *Lib) Name() string

Name returns the name or path that was loaded.

Flutter: no counterpart

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL