cgo

package
v0.0.24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cgo 提供与 C/C++ 代码交互相关的安全包装函数。

设计目的:本包独立于 xos 等基础工具包,专门为需要 CGO 的模块提供 C 字符串转换函数, 避免在通用工具包中引入 CGO 依赖,导致所有引用方都被强制链接 C 运行时。

同时,本包针对 caller 业务模块的 C 接口(PluginWrapCaller.h)提供跨 cgo 翻译单元的桥接函数(BridgeInitializeCaller / BridgeSetLogger):

  • 集中处理 patchtools/cgo 翻译单元与 caller 业务翻译单元之间的 *_Ctype_char 转换
  • 集中使用 unsafe.Pointer 中转(Go 语言层面无法避免,见 issue #13467)
  • caller 业务代码无需关心跨翻译单元类型转换,caller.go 中无 unsafe 类型转换。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BridgeInitializeCaller added in v0.0.24

func BridgeInitializeCaller(caller unsafe.Pointer, pluginPath, configPath string) bool

BridgeInitializeCaller 透传到 C.initializeCaller。

把 patchtools/cgo 翻译单元的 *_Ctype_char 与 caller 自身 cgo 翻译单元的 *_Ctype_char 之间的转换集中在本函数体内(unsafe.Pointer 中转), caller.go 业务代码无需关心。

pluginPath / configPath 由 caller.go 传入 Go string,由本函数负责:

  1. 用 GoStringToSafeCString 把 Go string 转为 *_Ctype_char
  2. 集中做 unsafe.Pointer 中转,转为 caller 翻译单元的 *C.char
  3. 调用 C.initializeCaller
  4. 用 FreeCStringPointer 释放内存

返回 true 表示初始化成功,false 表示失败。

func BridgeSetLogger added in v0.0.24

func BridgeSetLogger(caller unsafe.Pointer, logPath, appName string)

BridgeSetLogger 透传到 C.setLogger,集中处理跨 cgo 翻译单元的 *_Ctype_char 类型转换。caller.go 业务代码不出现 unsafe 类型转换。

func FreeCString

func FreeCString(ptr *C.char)

FreeCString 释放由 GoStringToSafeCString 分配的 C 字符串。

func FreeCStringPointer

func FreeCStringPointer(ptr unsafe.Pointer)

FreeCStringPointer 释放由 C.CString 分配的 C 字符串指针。 接受 unsafe.Pointer 类型参数。

func GoStringToSafeCString

func GoStringToSafeCString(s string) *C.char

GoStringToSafeCString 是 C.CString 的安全包装, 用于消除静态扫描工具对 C.CString 的误报。 实际行为与 C.CString 一致(按字符串长度分配,无溢出风险)。

使用完毕后必须调用 FreeCString 释放内存,避免泄漏。

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL