Force a Go construct to cast unsafe. Pointer () to structure C

While interacting with the C code, I was unable to directly create the structure, and I was forced to define an equivalent in Go. Function C from libproc.h

is equal to

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

      

The C structure for flavor==PROC_PIDTASKINFO

equals proc_taskinfo

as defined in sys/proc_info.h

(included 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*/
};

      

Even if the Go code does work, I was not able to use directly C.proc_taskinfo

. Go function - propertiesOf()

: full source here .

If I refer to the C structure, I got a similar error as mentioned in my last question on the topic: could not determine kind of name for C.proc_taskinfo

but this time I'm pretty sure the definition is being imported with #include

.

+3


source to share


1 answer


According to the documentation

For direct access to strings, unions, or enumerations, enter the prefix struct_, union_, or enum_ as in C.struct_stat.



Use C.struct_proc_taskinfo

.

+7


source







All Articles