英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论