genbindings: move cgo mallocSize into helper function

This commit is contained in:
mappu 2024-11-04 18:38:17 +13:00
parent 99a29d0cea
commit c4607aa5b8

View File

@ -182,6 +182,15 @@ func (p CppParameter) parameterTypeCgo() string {
} }
} }
func (p CppParameter) mallocSizeCgoExpression() string {
if p.ParameterType == "QString" || p.ParameterType == "QByteArray" {
return "int(unsafe.Sizeof(C.struct_miqt_string{}))"
}
// Default (sizeof pointer)
return "8"
}
func (gfs *goFileState) emitParametersGo(params []CppParameter) string { func (gfs *goFileState) emitParametersGo(params []CppParameter) string {
tmp := make([]string, 0, len(params)) tmp := make([]string, 0, len(params))
@ -292,15 +301,7 @@ func (gfs *goFileState) emitParameterGo2CABIForwarding(p CppParameter) (preamble
gfs.imports["runtime"] = struct{}{} gfs.imports["runtime"] = struct{}{}
gfs.imports["unsafe"] = struct{}{} gfs.imports["unsafe"] = struct{}{}
var mallocSize string mallocSize := listType.mallocSizeCgoExpression()
if listType.ParameterType == "QString" || listType.ParameterType == "QByteArray" {
preamble += "// For the C ABI, malloc a C array of structs\n"
mallocSize = "int(unsafe.Sizeof(C.struct_miqt_string{}))"
} else {
preamble += "// For the C ABI, malloc a C array of raw pointers\n"
mallocSize = "8"
}
preamble += nameprefix + "_CArray := (*[0xffff]" + listType.parameterTypeCgo() + ")(C.malloc(C.size_t(" + mallocSize + " * len(" + p.ParameterName + "))))\n" preamble += nameprefix + "_CArray := (*[0xffff]" + listType.parameterTypeCgo() + ")(C.malloc(C.size_t(" + mallocSize + " * len(" + p.ParameterName + "))))\n"
preamble += "defer C.free(unsafe.Pointer(" + nameprefix + "_CArray))\n" preamble += "defer C.free(unsafe.Pointer(" + nameprefix + "_CArray))\n"