强制定义用于将 unsafe.Pointer() 转换为 C 结构的 Go 结构
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 结构是 proc_taskinfo
,如 sys/proc_info.h
中定义(包含在 libproc.h
中):
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()
:完整源代码 here。
如果我引用 C 结构,我得到了与我在 中关于主题的类似错误报告:could not determine kind of name for C.proc_taskinfo
,但这次我确定定义是使用 #include
.
To access a struct, union, or enum type directly, prefix it with struct_, union_, or enum_, as in C.struct_stat.
使用C.struct_proc_taskinfo
.
与 C 代码互操作时,我无法直接转换结构,我被迫在 Go 中定义一个等效的结构。
来自 libproc.h
的 C 函数是
int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize)
flavor==PROC_PIDTASKINFO
的 C 结构是 proc_taskinfo
,如 sys/proc_info.h
中定义(包含在 libproc.h
中):
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()
:完整源代码 here。
如果我引用 C 结构,我得到了与我在 could not determine kind of name for C.proc_taskinfo
,但这次我确定定义是使用 #include
.
To access a struct, union, or enum type directly, prefix it with struct_, union_, or enum_, as in C.struct_stat.
使用C.struct_proc_taskinfo
.