aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/reflect/makefunc_ffi.go
blob: 40c1ea80fbec7184d97a80cfbe635300cee0fd8c (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2014 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package reflect

import (
	"runtime"
	"unsafe"
)

// The ffi function, written in C, allocates an FFI closure.  It
// returns the code and data pointers.  When the code pointer is
// called, it will call callback.  CIF is an FFI data structure
// allocated as part of the closure, and is returned to ensure that
// the GC retains it.
func ffi(ftyp *funcType, callback func(unsafe.Pointer, unsafe.Pointer)) (code uintptr, data uintptr, cif unsafe.Pointer)

// The ffiFree function, written in C, releases the FFI closure.
func ffiFree(uintptr)

// An ffiData holds the information needed to preserve an FFI closure
// for the garbage collector.
type ffiData struct {
	code     uintptr
	data     uintptr
	cif      unsafe.Pointer
	callback func(unsafe.Pointer, unsafe.Pointer)
}

// The makeFuncFFI function uses libffi closures to implement
// reflect.MakeFunc.  This is used for processors for which we don't
// have more efficient support.
func makeFuncFFI(ftyp *funcType, fn func(args []Value) (results []Value)) (uintptr, *ffiData) {
	callback := func(params, results unsafe.Pointer) {
		ffiCall(ftyp, fn, params, results)
	}

	code, data, cif := ffi(ftyp, callback)

	c := &ffiData{code: code, data: data, cif: cif, callback: callback}

	runtime.SetFinalizer(c,
		func(p *ffiData) {
			ffiFree(p.data)
		})

	return code, c
}

// ffiCall takes pointers to the parameters, calls the function, and
// stores the results back into memory.
func ffiCall(ftyp *funcType, fn func([]Value) []Value, params unsafe.Pointer, results unsafe.Pointer) {
	in := make([]Value, 0, len(ftyp.in))
	ap := params
	for _, rt := range ftyp.in {
		p := unsafe_New(rt)
		memmove(p, *(*unsafe.Pointer)(ap), rt.size)
		v := Value{rt, p, flag(rt.Kind()) | flagIndir}
		in = append(in, v)
		ap = (unsafe.Pointer)(uintptr(ap) + ptrSize)
	}

	out := fn(in)

	off := uintptr(0)
	for i, typ := range ftyp.out {
		v := out[i]
		if v.typ != typ {
			panic("reflect: function created by MakeFunc using " + funcName(fn) +
				" returned wrong type: have " +
				out[i].typ.String() + " for " + typ.String())
		}
		if v.flag&flagRO != 0 {
			panic("reflect: function created by MakeFunc using " + funcName(fn) +
				" returned value obtained from unexported field")
		}

		off = align(off, uintptr(typ.fieldAlign))
		addr := unsafe.Pointer(uintptr(results) + off)
		if v.flag&flagIndir == 0 && (v.kind() == Ptr || v.kind() == UnsafePointer) {
			*(*unsafe.Pointer)(addr) = v.ptr
		} else {
			memmove(addr, v.ptr, typ.size)
		}
		off += typ.size
	}
}