[英]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)
為C結構flavor==PROC_PIDTASKINFO
被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()
: 這里是完整的源代碼。
如果我引用C結構,我得到了一個類似的錯誤報告,如我最近關於這個主題的問題: could not determine kind of name for C.proc_taskinfo
,但這次我確定定義是用#include
導入的。
根據文件
要直接訪問struct,union或enum類型,請在其前面加上struct_,union_或enum_,如C.struct_stat中所示。
使用C.struct_proc_taskinfo
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.