purego

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2022 License: Apache-2.0 Imports: 1 Imported by: 208

README

purego

Go Reference

A library for calling C functions from Go without Cgo.

External Code

Purego uses code that originates from the Go runtime. These files are under the BSD-3 License that can be found in the Go Source. This is a list of the copied files:

  • zcallback_darwin_*.s from package runtime
  • internal/abi/abi_*.h from package runtime/cgo
  • internal/fakecgo/callbacks.go from package runtime/cgo
  • internal/fakecgo/iscgo.go from package runtime/cgo
  • internal/fakecgo/setenv.go from package runtime/cgo

Documentation

Index

Constants

View Source
const RTLD_DEFAULT = ^uintptr(1)
View Source
const RTLD_GLOBAL = 0x8

Variables

This section is empty.

Functions

func Dlclose

func Dlclose(handle uintptr) bool

Dlclose decrements the reference count on the dynamic library handle. If the reference count drops to zero and no other loaded libraries use symbols in it, then the dynamic library is unloaded. Dlclose returns false on success, and true on error.

func Dlerror

func Dlerror() string

Dlerror returns a human-readable string describing the most recent error that occurred from Dlopen, Dlsym or Dlclose since the last call to Dlerror. It returns an empty string if no errors have occurred since initialization or since it was last called.

func Dlopen

func Dlopen(path string, mode int) uintptr

Dlopen examines the dynamic library or bundle file specified by path. If the file is compatible with the current process and has not already been loaded into the current process, it is loaded and linked. After being linked, if it contains any initializer functions, they are called, before Dlopen returns. It returns a handle that can be used with Dlsym and Dlclose. A second call to Dlopen with the same path will return the same handle, but the internal reference count for the handle will be incremented. Therefore, all Dlopen calls should be balanced with a Dlclose call.

func Dlsym

func Dlsym(handle uintptr, name string) uintptr

Dlsym takes a "handle" of a dynamic library returned by Dlopen and the symbol name. It returns the address where that symbol is loaded into memory. If the symbol is not found, in the specified library or any of the libraries that were automatically loaded by Dlopen when that library was loaded, Dlsym returns zero.

func NewCallback

func NewCallback(fn interface{}) uintptr

NewCallback converts a Go function to a function pointer conforming to the C calling convention. This is useful when interoperating with C code requiring callbacks. The argument is expected to be a function with zero or one uintptr-sized result. The function must not have arguments with size larger than the size of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory allocated for these callbacks is never released. At least 2000 callbacks can always be created. Although this function provides similar functionality to windows.NewCallback it is distinct.

func SyscallN

func SyscallN(fn uintptr, args ...uintptr) (r1, r2, err uintptr)

SyscallN takes fn, a C function pointer and a list of arguments as uintptr. There is an internal maximum number of arguments that SyscallN can take. It panics when the maximum is exceeded. It returns the result and the libc error code if there is one.

NOTE: SyscallN does not properly call functions that have both integer and float parameters. See discussion comment https://github.com/ebiten/purego/pull/1#issuecomment-1128057607 for an explanation of why that is.

On amd64, if there are more than 8 floats the 9th and so on will be placed incorrectly on the stack.

The pragma go:nosplit is not needed at this function declaration because it uses go:uintptrescapes which forces all the objects that the uintptrs point to onto the heap where a stack split won't affect their memory location.

Types

This section is empty.

Directories

Path Synopsis
internal
fakecgo
Package fakecgo implements the Cgo runtime (runtime/cgo) entirely in Go.
Package fakecgo implements the Cgo runtime (runtime/cgo) entirely in Go.
Package objc is a low-level pure Go objective-c runtime.
Package objc is a low-level pure Go objective-c runtime.

Jump to

Keyboard shortcuts

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