cptr

package
v0.0.0-...-1e60831 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: NIST-PD-fallback Imports: 10 Imported by: 3

Documentation

Overview

Package cptr handles C void* pointers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsByteSlice

func AsByteSlice[T ~uint8 | ~int8, A ~[]T](value A) (b []byte)

AsByteSlice converts []C.uint8_t or []C.char to []byte.

func Call

func Call(post func(fn Function), f any) any

Call wraps a Go function as Function and immediately uses it. post is a function that asynchronously invokes fn (this must be asynchronous). f must be a function with zero parameters and zero or one return values. Returns f's return value, or nil if f does not have a return value.

func CaptureFileDump

func CaptureFileDump(f func(fp unsafe.Pointer)) (data []byte, e error)

CaptureFileDump invokes a function that writes to *C.FILE, and returns what's been written.

func FirstPtr

func FirstPtr[R, T any, A ~[]T](value A) *R

FirstPtr returns a pointer to the first element of a slice, or nil if the slice is empty.

T must be a pointer or unsafe.Pointer. *R should be equivalent to T.

func GetString

func GetString[T ~uint8 | ~int8, A ~[]T](value A) string

GetString interprets []byte as nil-terminated string.

Types

type CArgs

type CArgs struct {
	Argc int            // argc for C code (cast to C.int)
	Argv unsafe.Pointer // argv for C code (cast to **C.char)
	// contains filtered or unexported fields
}

CArgs rearranges args so that they can be provided to C code.

func NewCArgs

func NewCArgs(args []string) (a *CArgs)

NewCArgs constructs CArgs.

func (*CArgs) Close

func (a *CArgs) Close() error

Close releases C memory in CArgs.

type FilePipeCGo

type FilePipeCGo struct {
	Reader *os.File
	Writer unsafe.Pointer // *C.FILE
}

FilePipeCGo is a pipe from *C.FILE writer to *os.File reader.

func NewFilePipeCGo

func NewFilePipeCGo(cfg FilePipeConfig) (p *FilePipeCGo, e error)

NewFilePipeCGo creates a FilePipeCGo.

func (*FilePipeCGo) Close

func (p *FilePipeCGo) Close() error

Close closes both reader and writer.

func (*FilePipeCGo) CloseReader

func (p *FilePipeCGo) CloseReader() (e error)

CloseReader closes the reader.

func (*FilePipeCGo) CloseWriter

func (p *FilePipeCGo) CloseWriter() error

CloseWriter closes the writer.

func (*FilePipeCGo) ReadAll

func (p *FilePipeCGo) ReadAll() (data []byte, e error)

ReadAll reads from the pipe until EOF.

type FilePipeConfig

type FilePipeConfig struct {
	// NonBlock sets O_NONBLOCK on the writer file descriptor.
	NonBlock bool
}

FilePipeConfig configures FilePipe*.

type Function

type Function interface {
	// contains filtered or unexported methods
}

Function provides a C function with void* argument.

type FunctionType

type FunctionType []string

FunctionType identifies the type of a C function. The zero FunctionType identifies `int f(uintptr_t ctx)`. FunctionType{"T1"} identifies `int f(T1* item1, uintptr_t ctx)`.

func (FunctionType) Assert

func (ft FunctionType) Assert(fn Function)

Assert panics if fn is not created from ft.

func (FunctionType) C

func (ft FunctionType) C(f unsafe.Pointer, arg any) Function

C wraps a C function as Function. f must be a C function consistent with ft.

func (FunctionType) CallbackOnce

func (ft FunctionType) CallbackOnce(fn Function) (f unsafe.Pointer, ctx uintptr)

CallbackOnce returns C callback function and arg that can be invoked only once.

func (FunctionType) CallbackReuse

func (ft FunctionType) CallbackReuse(fn Function) (f unsafe.Pointer, ctx uintptr, revoke func())

CallbackReuse returns C callback function and arg that can be invoked repeatedly. Use revoke() to avoid memory leak.

func (FunctionType) Invoke

func (ft FunctionType) Invoke(fn Function, param ...unsafe.Pointer) int

Invoke invokes the function. fn and param must be consistent with ft.

type ZeroFunctionType

type ZeroFunctionType struct {
	FunctionType
}

ZeroFunctionType is the `int f(uintptr_t ctx)` type.

var Func0 ZeroFunctionType

Func0 is the `int f(uintptr_t ctx)` type.

func (ZeroFunctionType) Int

func (ZeroFunctionType) Int(f func() int) Function

Int wraps a Go function as Function.

func (ZeroFunctionType) Void

func (ft ZeroFunctionType) Void(f func()) Function

Void wraps a Go function as Function.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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