c/clang/symg:use unsafe.String to avoid memory copy

This commit is contained in:
luoliwoshang
2024-07-29 22:10:30 +08:00
parent aca3a05222
commit 87382aad4d

View File

@@ -28,6 +28,7 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"unsafe"
"github.com/goplus/llgo/c" "github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/c/cjson"
@@ -91,18 +92,24 @@ func check(err error) {
} }
} }
func getConf(data []byte) (config types.Config, err error) { func getConf(data []byte) (*types.Config, error) {
conf := cjson.ParseBytes(data) conf := cjson.ParseBytes(data)
defer conf.Delete()
if conf == nil { if conf == nil {
return config, errors.New("failed to parse config") return nil, errors.New("failed to parse config")
} }
config.Name = getStringItem(conf, "name", "") config := &types.Config{
config.CFlags = getStringItem(conf, "cflags", "") Name: getStringItem(conf, "name", ""),
config.Libs = getStringItem(conf, "libs", "") CFlags: getStringItem(conf, "cflags", ""),
config.Include = getStringArrayItem(conf, "include") Libs: getStringItem(conf, "libs", ""),
config.TrimPrefixes = getStringArrayItem(conf, "trimPrefixes") Include: getStringArrayItem(conf, "include"),
return TrimPrefixes: getStringArrayItem(conf, "trimPrefixes"),
}
return config, nil
}
func getString(obj *cjson.JSON) (value string) {
str := obj.GetStringValue()
return unsafe.String((*byte)(unsafe.Pointer(str)), c.Strlen(str))
} }
func getStringItem(obj *cjson.JSON, key string, defval string) (value string) { func getStringItem(obj *cjson.JSON, key string, defval string) (value string) {
@@ -110,7 +117,7 @@ func getStringItem(obj *cjson.JSON, key string, defval string) (value string) {
if item == nil { if item == nil {
return defval return defval
} }
return c.GoString(item.GetStringValue()) return getString(item)
} }
func getStringArrayItem(obj *cjson.JSON, key string) (value []string) { func getStringArrayItem(obj *cjson.JSON, key string) (value []string) {
@@ -120,7 +127,7 @@ func getStringArrayItem(obj *cjson.JSON, key string) (value []string) {
} }
value = make([]string, item.GetArraySize()) value = make([]string, item.GetArraySize())
for i := range value { for i := range value {
value[i] = c.GoString(item.GetArrayItem(c.Int(i)).GetStringValue()) value[i] = getString(item.GetArrayItem(c.Int(i)))
} }
return return
} }