被迫定义Go结构体以将unsafe.Pointer()转换为C结构体。

huangapple go评论89阅读模式
英文:

Forced to define Go struct for casting an unsafe.Pointer() to a C struct

问题

与C代码进行交互时,我无法直接转换一个结构体,而是被迫在Go中定义一个等效的结构体。

libproc.h中的C函数是:

int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize)

flavor==PROC_PIDTASKINFO时的C结构体是在sys/proc_info.h中定义的proc_taskinfo(由libproc.h包含):

struct proc_taskinfo {
    uint64_t    pti_virtual_size;    /* 虚拟内存大小(字节) */
    uint64_t    pti_resident_size;    /* 驻留内存大小(字节) */
    uint64_t    pti_total_user;    /* 总用户时间 */
    uint64_t    pti_total_system;
    uint64_t    pti_threads_user;    /* 仅现有线程 */
    uint64_t    pti_threads_system;
    int32_t    pti_policy;    /* 新线程的默认策略 */
    int32_t    pti_faults;    /* 页面错误数 */
    int32_t    pti_pageins;    /* 实际页面插入数 */
    int32_t    pti_cow_faults;    /* 写时复制错误数 */
    int32_t    pti_messages_sent;    /* 发送的消息数 */
    int32_t    pti_messages_received;    /* 接收的消息数 */
    int32_t    pti_syscalls_mach;    /* mach系统调用数 */
    int32_t    pti_syscalls_unix;    /* unix系统调用数 */
    int32_t    pti_csw;    /* 上下文切换数 */
    int32_t    pti_threadnum;    /* 任务中的线程数 */
    int32_t    pti_numrunning;    /* 运行中的线程数 */
    int32_t    pti_priority;    /* 任务优先级 */
};

即使Go代码实际上可以工作,我仍然无法直接使用C.proc_taskinfo。Go函数是propertiesOf():完整源代码在这里

如果我引用C结构体,我会得到一个类似于我在最新问题中报告的错误:could not determine kind of name for C.proc_taskinfo,但这次我确定已经使用#include导入了定义。

英文:

Interoperating with C code, I was not able to directly cast a structure and I was forced to define an equivalent one in Go.
The C function from libproc.h is

<pre><code><!-- language: lang-c -->
int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize)
</code></pre>

The C structure for flavor==PROC_PIDTASKINFO is proc_taskinfo as defined in sys/proc_info.h (included by libproc.h):

<pre><code><!-- language: lang-c -->
struct proc_taskinfo {
uint64_t pti_virtual_size; /* virtual memory size (bytes) /
uint64_t pti_resident_size; /
resident memory size (bytes) /
uint64_t pti_total_user; /
total time /
uint64_t pti_total_system;
uint64_t pti_threads_user; /
existing threads only /
uint64_t pti_threads_system;
int32_t pti_policy; /
default policy for new threads /
int32_t pti_faults; /
number of page faults /
int32_t pti_pageins; /
number of actual pageins /
int32_t pti_cow_faults; /
number of copy-on-write faults /
int32_t pti_messages_sent; /
number of messages sent /
int32_t pti_messages_received; /
number of messages received /
int32_t pti_syscalls_mach; /
number of mach system calls /
int32_t pti_syscalls_unix; /
number of unix system calls /
int32_t pti_csw; /
number of context switches /
int32_t pti_threadnum; /
number of threads in the task /
int32_t pti_numrunning; /
number of running threads /
int32_t pti_priority; /
task priority*/
};
</code></pre>

Even if Go code actually works, I was not able to use C.proc_taskinfo directly. The Go function is propertiesOf(): complete source here.

If I reference the C structure, I got a similar error reported as in my latest question on the subject: could not determine kind of name for C.proc_taskinfo, but this time I'm sure the definition is imported with #include.

答案1

得分: 7

根据文档

>要直接访问结构体、联合体或枚举类型,请在其前面加上struct_、union_或enum_,例如C.struct_stat。

使用C.struct_proc_taskinfo

英文:

As per documentation

>To access a struct, union, or enum type directly, prefix it with struct_, union_, or enum_, as in C.struct_stat.

Use C.struct_proc_taskinfo.

huangapple
  • 本文由 发表于 2015年5月29日 22:06:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/30531721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定