ffi

package
v4.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cast

func Cast[T any](ptr uintptr) *T

Cast converts a uintptr obtained from C-allocated memory into a Go pointer of the desired type. The pointer must not originate from Go-allocated memory, as the uintptr argument is invisible to the garbage collector and violates the unsafe.Pointer conversion rules (the pointer-to-uintptr and uintptr-to-pointer conversions do not occur in the same expression).

The implementation bypasses go vet's unsafe.Pointer checks by reinterpreting the uintptr through its memory representation rather than using a direct unsafe.Pointer(ptr) conversion.

func CastWithOffset

func CastWithOffset[T any](ptr uintptr, offset uint64) *T

CastWithOffset is the same as Cast but advances the pointer by offset elements of type T (i.e., by offset * unsafe.Sizeof(T) bytes) before converting. The same C-allocated memory restriction as Cast applies.

func Cstring

func Cstring(pinner *runtime.Pinner, name string) *byte

Cstring converts a go string to *byte that can be passed to C code.

func Gostring

func Gostring(ptr *byte) string

Gostring copies a char* to a Go string.

func GostringSized

func GostringSized(ptr *byte, size uint64) string

GostringSized copies size bytes starting at ptr into a new Go string. Unlike Gostring, it does not scan for a NUL terminator. Returns "" if ptr is nil.

func NativeToUintptr

func NativeToUintptr[T Native](x T) uintptr

NativeToUintptr is a helper used by populate WafObject values with Go values

func Slice

func Slice[T any](ptr *T, length uint64) []T

Slice returns a []T whose backing array starts at ptr and has the given length. It is a generic wrapper around unsafe.Slice.

func SliceData

func SliceData[E any, T ~[]E](slice T) *E

SliceData returns a pointer to the underlying array of the slice. It is a generic wrapper around unsafe.SliceData.

func String

func String(ptr *byte, length uint64) string

String returns a string whose bytes start at ptr and has the given length. It is a wrapper around unsafe.String.

func StringData

func StringData(str string) *byte

StringData returns a pointer to the underlying bytes of str. It is a wrapper around unsafe.StringData.

func UintptrToNative

func UintptrToNative[T Native](x uintptr) T

UintToNative is a helper used retrieve Go values from an uintptr encoded value from a WafObject

Types

type Native

type Native interface {
	~byte | ~float64 | ~float32 | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint16 | ~uint32 | ~uint64 | ~bool | ~uintptr
}

Native is a constraint that permits scalar types whose in-memory representation can safely be reinterpreted via NativeToUintptr and UintptrToNative. All permitted types have a well-defined, fixed-size layout with no pointers.

type StringHeader

type StringHeader struct {
	Len  int
	Data *byte
}

StringHeader is the runtime representation of a Go string value, mirroring the internal layout used by the compiler.

func NativeStringUnwrap

func NativeStringUnwrap(str string) StringHeader

NativeStringUnwrap cast a native string type into it's runtime value.

Jump to

Keyboard shortcuts

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