cabi

package
v0.0.0-...-7e35ac9 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2022 License: Unlicense Imports: 5 Imported by: 0

Documentation

Rendered for darwin/amd64

Overview

Package cabi provides utilities for calling C ABI functions without Cgo.

It is a foreign function library in pure Go and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Go.

Supported Platforms

Currently the implementation supports the following GOOS/GOARCH combinations:

darwin/amd64

Garbage Collector

While currently Go has a non-moving GC, it’s possible that a copying GC will be implemented in the future. This package was written with that in mind and, after minor changes, should be compatible with copying GC, assuming that appropriate argument types were used for function calls in user’s code.

Data Types

This package defines a number of primitive C-compatible data types. The following table shows how Go types map to C types.

┌───────────────┬────────────────┬────────────────────────┬─────────┐
│ Type          │ Go             │ C                      │ Usage   │
├───────────────┼────────────────┼────────────────────────┼─────────┤
│ Void          │                │ void                   │     Out │
│ UnsafePointer │ unsafe.Pointer │ T* / uintptr_t         │ Arg     │
│ String        │ string         │ char*                  │ Arg     │
│ Bytes         │ []byte         │ char*                  │ Arg     │
│ Uintptr       │ uintptr        │ T* / uintptr_t         │ Arg     │
│ Bool          │ bool           │ _Bool                  │ Arg Out │
│ Int           │ int            │ long / ssize_t         │ Arg Out │
│ Int8          │ int8           │ char                   │ Arg Out │
│ Int16         │ int16          │ short                  │ Arg Out │
│ Int32         │ int32          │ int                    │ Arg Out │
│ Int64         │ int64          │ long long              │ Arg Out │
│ Uint          │ uint           │ unsigned long / size_t │ Arg Out │
│ Uint8         │ uint8          │ unsigned char          │ Arg Out │
│ Uint16        │ uint16         │ unsigned short         │ Arg Out │
│ Uint32        │ uint32         │ unsigned int           │ Arg Out │
│ Uint64        │ uint64         │ unsigned long long     │ Arg Out │
│ Float32       │ float32        │ float                  │ Arg     │
│ Float64       │ float64        │ double                 │ Arg     │
└───────────────┴────────────────┴────────────────────────┴─────────┘

Note that, due to the variety of C compiler implementations, this may not apply to all platforms. In particular, long type is platform-specific, i.e. Windows uses LLP64 scheme while Darwin and Linux are LP64.

Not all types can be used as a function outputs. For example, while it may be nice to get String and Bytes return values, both types would have to assume a certain ownership model and null-terminated memory. This assumption does not apply to all C functions and is likely confusing in many cases.

Uintptr should used for pointers to unmanaged memory, while UnsafePointer must be used for pointers to Go values managed by GC. If the value escapes function call, caller must ensure that GC will not move the object or assume that GC is non-moving by importing go4.org/unsafe/assume-no-moving-gc package to avoid issues with future Go releases.

String and Bytes arguments are passed as a pointer to the underlying data. Prefer using them to manually casting string or slice to runtime header.

String passes a pointer to Go string representation. That is, null terminator is not appended and no copying is performed. It is invalid to pass String to functions that modify string data. If a function expects a null-terminated C string, use cstr.CString and pass the result as an UnsafePointer argument.

Prior Art

While the implementation is not a derivative of any other project, it shares the goal with libffi library and should have a similar interface. That’s not intentional though and there may be some subtle differences in the behavior.

https://github.com/libffi/libffi

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

func Call(fn uintptr, out Out, args ...Arg)

Call invokes fn with the given arguments and expected output value.

Types

type Arg

type Arg struct {
	// contains filtered or unexported fields
}

Arg is a function call argument value.

func Bool

func Bool(v bool) Arg

Bool returns a function call argument value for bool type.

func Bytes

func Bytes(v []byte) Arg

Bytes returns a function call argument value for []byte type.

func Float32

func Float32(v float32) Arg

Float32 returns a function call argument value for float32 type.

func Float64

func Float64(v float64) Arg

Float64 returns a function call argument value for float64 type.

func Int

func Int(v int) Arg

Int returns a function call argument value for int type.

func Int16

func Int16(v int16) Arg

Int16 returns a function call argument value for int32 type.

func Int32

func Int32(v int32) Arg

Int32 returns a function call argument value for int32 type.

func Int64

func Int64(v int64) Arg

Int64 returns a function call argument value for int64 type.

func Int8

func Int8(v int8) Arg

Int8 returns a function call argument value for int8 type.

func String

func String(v string) Arg

String returns a function call argument value for string type.

func Uint

func Uint(v uint) Arg

Uint returns a function call argument value for uint type.

func Uint16

func Uint16(v uint16) Arg

Uint16 returns a function call argument value for uint32 type.

func Uint32

func Uint32(v uint32) Arg

Uint32 returns a function call argument value for uint32 type.

func Uint64

func Uint64(v uint64) Arg

Uint64 returns a function call argument value for uint64 type.

func Uint8

func Uint8(v uint8) Arg

Uint8 returns a function call argument value for uint8 type.

func Uintptr

func Uintptr(v uintptr) Arg

Uintptr returns a function call argument value for uintptr type.

func UnsafePointer

func UnsafePointer(v unsafe.Pointer) Arg

UnsafePointer returns a function call argument value for unsafe.Pointer type.

type Out

type Out struct {
	// contains filtered or unexported fields
}

Out is a function call output value.

func OutBool

func OutBool(p *bool) Out

OutBool returns a function call output value for bool type.

func OutInt

func OutInt(p *int) Out

OutInt returns a function call output value for int type.

func OutInt16

func OutInt16(p *int16) Out

OutInt16 returns a function call output value for int16 type.

func OutInt32

func OutInt32(p *int32) Out

OutInt32 returns a function call output value for int32 type.

func OutInt64

func OutInt64(p *int64) Out

OutInt64 returns a function call output value for int64 type.

func OutInt8

func OutInt8(p *int8) Out

OutInt8 returns a function call output value for int8 type.

func OutUint

func OutUint(p *uint) Out

OutUint returns a function call output value for uint type.

func OutUint16

func OutUint16(p *uint16) Out

OutUint16 returns a function call output value for uint16 type.

func OutUint32

func OutUint32(p *uint32) Out

OutUint32 returns a function call output value for uint32 type.

func OutUint64

func OutUint64(p *uint64) Out

OutUint64 returns a function call output value for uint64 type.

func OutUint8

func OutUint8(p *uint8) Out

OutUint8 returns a function call output value for uint8 type.

func OutUintptr

func OutUintptr(p *uintptr) Out

OutUintptr returns a function call output value for uintptr type.

func Void

func Void() Out

Void returns a function call output value for void type.

Jump to

Keyboard shortcuts

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