dl

package
v1.17.14 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 8 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) that the backends share. The struct-layout test helper is in package layouttest, which only tests import, so that no program links package testing.

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 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 CHeapInUse

func CHeapInUse() (bytes int64, ok bool)

CHeapInUse is the number of bytes that the C allocator has handed out and not taken back, all callers counted: libwayland, libxkbcommon, fontconfig and the graphics drivers, and not only the blocks of this package. The memory tests (step 29.21) compare it after N and after 4N runs of a scenario. ok is false when libc has no mallinfo2 (glibc before 2.33, Bionic).

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 LiveBlocks

func LiveBlocks() int64

LiveBlocks is the number of blocks of C memory that Malloc, Calloc and CString made and that Free has not released. A program whose C memory is flat keeps this number flat; the memory tests (step 29.21) compare it after N and after 4N runs of a scenario. Memory that C allocates itself (a library's own objects) is not counted.

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

func TrackBlocks

func TrackBlocks() (stop func() []LeakedBlock)

TrackBlocks starts to record the stack of every block that Malloc, Calloc and CString make, for a test that looks for the site of a block that is not freed. It returns the function that stops the tracking and returns the blocks that are still alive. Tracking is slow: it is for tests. Only one tracking can be on at a time; a second call stops the first.

Flutter: no counterpart

Types

type LeakedBlock

type LeakedBlock struct {
	// Size is the size of the block in bytes.
	Size uintptr
	// Stack is where it was allocated, innermost function first, as "function file:line".
	Stack []string
}

LeakedBlock is a block of C memory that was allocated while TrackBlocks was on and not freed.

Flutter: no counterpart

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

Directories

Path Synopsis
Package layouttest is the helper for the layout test that every Go mirror of a C struct needs (SPEC §8.1 rule 3).
Package layouttest is the helper for the layout test that every Go mirror of a C struct needs (SPEC §8.1 rule 3).

Jump to

Keyboard shortcuts

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