https://github.com/octu0/unsafecgo
cgo calls via assembly trampoline
https://github.com/octu0/unsafecgo
assembly cgo goasm golang trampoline
Last synced: about 1 month ago
JSON representation
cgo calls via assembly trampoline
- Host: GitHub
- URL: https://github.com/octu0/unsafecgo
- Owner: octu0
- License: mit
- Created: 2023-03-01T14:01:20.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-03-23T11:25:59.000Z (over 3 years ago)
- Last Synced: 2025-07-27T17:55:28.811Z (11 months ago)
- Topics: assembly, cgo, goasm, golang, trampoline
- Language: Go
- Homepage: https://pkg.go.dev/github.com/octu0/unsafecgo
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `unsafecgo`
[](https://github.com/octu0/unsafecgo/blob/master/LICENSE)
[](https://godoc.org/github.com/octu0/unsafecgo)
[](https://goreportcard.com/report/github.com/octu0/unsafecgo)
[](https://github.com/octu0/unsafecgo/releases)
`unsafecgo` provides Cgo calls via assembly trampoline. Inspired by [rustgo](https://blog.filippo.io/rustgo/).
## Installation
```bash
go get github.com/octu0/unsafecgo
```
## Example
```go
package main
/*
#include
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.