unsafecgo

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 1 Imported by: 0

README

unsafecgo

MIT License GoDoc Go Report Card Releases

unsafecgo provides Cgo calls via assembly trampoline. Inspired by rustgo.

Installation

go get github.com/octu0/unsafecgo

Example

package main

/*
#include <stdio.h>
void hello() {
  fprintf(stderr, "hello world\n");
}
*/
import "C"

import (
	"fmt"
	"reflect"
	"runtime"
	"unsafe"

	"github.com/octu0/unsafecgo"
)

func cgo_helloworld() {
	fmt.Println("cgo")

	C.hello()
}

func unsafecgo_helloworld() {
	fmt.Println("unsafecgo.Call")

	p := unsafe.Pointer(reflect.ValueOf(C.hello).Pointer())
	unsafecgo.Call(p) // call C.hello()
	runtime.KeepAlive(p)
}

func main() {
	cgo_helloworld()
	unsafecgo_helloworld()
}

Benchmark

goos: darwin
goarch: amd64
pkg: github.com/octu0/unsafecgo/benchmark
cpu: Intel(R) Core(TM) i5-8210Y CPU @ 1.60GHz
BenchmarkUnsafecgo
BenchmarkUnsafecgo/cgo/malloc_free
BenchmarkUnsafecgo/cgo/malloc_free-4              971646              1236 ns/op            1024 B/op          1 allocs/op
BenchmarkUnsafecgo/unsafecgo/malloc_free
BenchmarkUnsafecgo/unsafecgo/malloc_free-4       1340142               910.4 ns/op             0 B/op          0 allocs/op
BenchmarkUnsafecgo/cgo/calc
BenchmarkUnsafecgo/cgo/calc-4                    3667410               311.5 ns/op             0 B/op          0 allocs/op
BenchmarkUnsafecgo/unsafecgo/calc
BenchmarkUnsafecgo/unsafecgo/calc-4             16357767                74.31 ns/op            0 B/op          0 allocs/op
BenchmarkUnsafecgo/cgo/nop_call
BenchmarkUnsafecgo/cgo/nop_call-4               17337052                65.60 ns/op            0 B/op          0 allocs/op
BenchmarkUnsafecgo/unsafecgo/nop_call
BenchmarkUnsafecgo/unsafecgo/nop_call-4         369709251                3.211 ns/op           0 B/op          0 allocs/op
PASS

License

MIT, see LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	AppName string = "unsafecgo"
	Version string = "1.0.2"
)

Variables

This section is empty.

Functions

func Available

func Available() bool

func Call

func Call(unsafe.Pointer)

Call C func return will void:

void C.foo()

func CallCtx

func CallCtx(unsafe.Pointer, unsafe.Pointer)

Call C func with context parameter and return will void:

void C.foo(void *ctx)

func CallCtxRC

func CallCtxRC(unsafe.Pointer, unsafe.Pointer) int32

Call C func with context parameter and return will int32:

int C.foo(void *ctx)

func CallCtxRP

Call C func with context parameter and return will pointer:

void* C.foo(void *ctx)

func CallRC

func CallRC(unsafe.Pointer) int32

Call C func return will int32:

int C.foo()

func CallRP

func CallRP(unsafe.Pointer) unsafe.Pointer

Call C func return will pointer:

void* C.foo()

Types

This section is empty.

Jump to

Keyboard shortcuts

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