|
|
|
|
|
by crawshaw
4302 days ago
|
|
You can do your own pinning, which is what I do in gobind[1]. The basic idea is to keep a map in Go of the pointers: var ptrs = make(map[uintptr]unsafe.Pointer)
When passing a pointer from Go to C: var p unsafe.Pointer = ...
id := uintptr(p)
ptrs[id] = p
C.Fn(id)
When C returns the pointer to Go to be used, run it through the ptrs map again //export GoFn
func GoFn(id uintptr) {
p := ptrs[id]
// ... use p
}
Don't forget a cleanup function where you delete(ptrs, id). If you want to pass the same pointer multiple times to C and keep it comparable, you'll need a second map.All of this is requires being very careful, but the hard part is the notion of holding references to memory outside the realm of the GC. I don't believe a runtime-assisted pinning API can do any better than what you can do yourself with a map. [1] https://godoc.org/code.google.com/p/go.mobile/cmd/gobind |
|