首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >强制定义Go结构,以便将unsafe.Pointer()转换为C结构

强制定义Go结构,以便将unsafe.Pointer()转换为C结构
EN

Stack Overflow用户
提问于 2015-05-29 14:06:55
回答 1查看 552关注 0票数 4

与C代码交互操作时,我无法直接转换一个结构,我被迫在Go中定义一个等价的结构。来自libproc.h的C函数是

代码语言:javascript
复制
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包含):

代码语言:javascript
复制
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*/
};

即使Go代码确实有效,我也无法直接使用C.proc_taskinfo。Go函数是propertiesOf():完全源这里

如果我引用C结构,就会发现一个类似于我的最新问题中关于主题的错误:could not determine kind of name for C.proc_taskinfo,但这一次我确信定义是用#include导入的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-29 14:52:34

根据文档

若要直接访问结构、联合或枚举类型,请将其前缀为struct_、union_或enum_,如在C.struct_stat中。

使用C.struct_proc_taskinfo

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30531721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档