Add garbage collection memory mangement mode (gogc). Lots of fixes

related to pointer handling and memory management.
This commit is contained in:
gmp
2019-06-18 09:15:46 -04:00
parent 79bb282188
commit e5c8e114bf
8 changed files with 342 additions and 109 deletions
+21 -7
View File
@@ -246,10 +246,10 @@ func (t *Type) _CType(attrib bool) string {
return ct
}
func (t *Type) GoTypeDecl() string {
func (t *Type) GoTypeDecl(fin bool) string {
gt := t.GoType()
if wrapped[gt] {
return t.GoInterfaceDecl()
return t.GoInterfaceDecl(fin)
}
if t.Node.IsId() {
return ""
@@ -286,7 +286,7 @@ type %s %s
}
}
func (t *Type) GoInterfaceDecl() string {
func (t *Type) GoInterfaceDecl(fin bool) string {
ct := t.CType()
gt := t.GoType()
if Debug {
@@ -384,7 +384,7 @@ func (t *Type) CToGo(cval string) string {
}
// Call a C function from Go with a given return type and parameter types
func GoToC(name string, pnames, snames []string, rtype *Type, ptypes []*Type, fun, fin bool) string {
func GoToC(name string, pnames, snames []string, rtype *Type, ptypes []*Type, fun, fin bool, cm bool) string {
if rtype == nil {
//fmt.Println("nil sent to GoToC")
return ""
@@ -495,10 +495,24 @@ func GoToC(name string, pnames, snames []string, rtype *Type, ptypes []*Type, fu
}
if rt != "void" {
if fin {
cmp := ""
if !cm {
cmp = fmt.Sprintf(`if ret.ptr == o.ptr { return (%s)(unsafe.Pointer(o)) }
`,rtgt)
}
dbg := ""
dbg2 := ""
if Debug {
dbg = fmt.Sprintf(`fmt.Printf("Setting finalizer (%s): %%p -> %%p\n", ret, ret.ptr)
`, rtgt)
dbg2 = fmt.Sprintf(`fmt.Printf("Finalizer (%s): release %%p -> %%p\n", o, o.ptr)
`, rtgt)
}
ret.WriteString(fmt.Sprintf(`
runtime.SetFinalizer(ret, func(o %s) {
o.Release()
})`,rtgt))
if ret.ptr == nil { return ret }
%s%sruntime.SetFinalizer(ret, func(o %s) {
%so.Release()
})`, cmp, dbg, rtgt, dbg2))
}
ret.WriteString(`
return ret`)
+6 -7
View File
@@ -177,14 +177,14 @@ func TestType(t *testing.T) {
chk(tp.PointsTo(), tp2)
chk(tp2.PointsTo(), nil)
chk(tp.GoTypeDecl(), `
chk(tp.GoTypeDecl(false), `
type MyTypedef **C.NSObject
`)
str = "void"
n = &Node{"TypeName", "", []*Node{
&Node{"TypeSpecifier", "void", []*Node{}}}}
chk_newtype()
chk(tp.GoTypeDecl(), "")
chk(tp.GoTypeDecl(false), "")
void := tp
str = "BOOL"
@@ -206,14 +206,14 @@ type MyTypedef **C.NSObject
chk(tp.GoType(), "*NSObject")
Wrap("NSString")
chk(nso.GoTypeDecl(), `
chk(nso.GoTypeDecl(false), `
type NSObject interface {
Ptr() unsafe.Pointer
}
`)
chk(nso.GoType(), "NSObject")
chk(nst.GoTypeDecl(), `
chk(nst.GoTypeDecl(false), `
type NSString struct { Id }
func (o *NSString) Ptr() unsafe.Pointer { if o == nil { return nil }; return o.ptr }
func (o *Id) NSString() *NSString {
@@ -276,8 +276,7 @@ func (o *Id) NSString() *NSString {
snames := []string{"", "", "", ""}
chk_gotoc := func(expected string) {
fmt.Printf("chk_gotoc\n")
chk(GoToC("myFun", pnames, snames, rtype, ptypes, false, false), expected)
chk(GoToC("myFun", pnames, snames, rtype, ptypes, false, false, false), expected)
}
chk_gotoc("")
@@ -352,7 +351,7 @@ func (o *Id) NSString() *NSString {
}
return ret`)
chk(GoToC("myFun", pnames, snames, rtype, ptypes, true, false),
chk(GoToC("myFun", pnames, snames, rtype, ptypes, true, false, false),
`ret := &Id{}
ret.ptr = unsafe.Pointer(C.myFun(p1.Ptr(), p2.Ptr(), unsafe.Pointer(&p3p[0]), p4))
(*p3) = (*p3)[:cap(*p3)]