-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffi_darwin.go
64 lines (57 loc) · 1.35 KB
/
ffi_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package ffi
// #cgo CFLAGS: -I/usr/include/ffi
// #cgo LDFLAGS: -lffi
//
// #include <ffi.h>
// #include <unistd.h>
// #include <sys/mman.h>
//
// static int ffi_closure_alloc__(void **mem) {
// void *ptr;
//
// ptr = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
//
// if (MAP_FAILED == ptr) {
// return -1;
// }
//
// *mem = ptr;
// return 0;
// }
//
// static int ffi_closure_make_executable__(void *ptr) {
// return mprotect(ptr, sizeof(ptr), PROT_READ | PROT_EXEC);
// }
//
// static void ffi_closure_free__(void *ptr) {
// munmap(ptr, sizeof(ffi_closure));
// }
//
// extern void GoClosureCallback(ffi_cif *, void *, void **, void *);
//
// typedef void (*closure)(ffi_cif*, void*, void**, void*);
//
import "C"
import "unsafe"
func constructClosure(fn *function) (err error) {
var ptr unsafe.Pointer
var rc C.int
if rc, err = C.ffi_closure_alloc__(&ptr); rc != 0 {
return
}
if status := Status(C.ffi_prep_closure((*C.ffi_closure)(ptr), &fn.Interface.ffi_cif, C.closure(C.GoClosureCallback), unsafe.Pointer(fn))); status != OK {
C.ffi_closure_free__(ptr)
err = status
return
}
if rc, err = C.ffi_closure_make_executable__(ptr); rc != 0 {
C.ffi_closure_free__(ptr)
return
}
fn.fptr = ptr
fn.mptr = ptr
return nil
}
func destroyClosure(fn *function) {
C.ffi_closure_free__(fn.mptr)
}