从Julia更新C结构的字段值

从Julia更新C结构的字段值,c,julia,ffi,C,Julia,Ffi,我的问题很简单,但我不知道最好的方法是什么(或者Julia目前没有提供这种方法):如何从Julia设置C结构的字段值 假设您有一个结构类型来表示C库中树的节点: typedef struct node_s { int type; node_t* next; node_t* children; node_t* parent; } node_t; 然后在Julia中复制它: immutable node_t typ::Cint next::Ptr{n

我的问题很简单,但我不知道最好的方法是什么(或者Julia目前没有提供这种方法):如何从Julia设置C结构的字段值

假设您有一个结构类型来表示C库中树的节点:

typedef struct node_s
{
    int type;
    node_t* next;
    node_t* children;
    node_t* parent;
} node_t;
然后在Julia中复制它:

immutable node_t
    typ::Cint
    next::Ptr{node_t}
    children::Ptr{node_t}
    parent::Ptr{node_t}
end

现在假设您在C中分配了一个指向
node\u t
的指针,并希望更新Julia中的
parent
字段。我知道我们有
不安全的商店
更新指针指向的值,但计算
父项
字段的指针偏移量很麻烦(在本例中,在我的64位计算机上,它将是
sizeof(Int)+sizeof(Ptr{node\u t})*2
)。有没有更简单的方法来做同样的事情?

此用例提供了
fieldoffset
功能:

julia> immutable node_t
           typ::Cint
           next::Ptr{node_t}
           children::Ptr{node_t}
           parent::Ptr{node_t}
       end

julia> fieldoffset(node_t, 1)
0x0000000000000000

julia> fieldoffset(node_t, 2)
0x0000000000000008

julia> fieldoffset(node_t, 3)
0x0000000000000010

julia> fieldoffset(node_t, 4)
0x0000000000000018
但也不要担心只存储整个
不可变的
,只更改一个字段;它将被优化掉

julia> k = Ptr{node_t}(Libc.malloc(sizeof(node_t)))
Ptr{node_t} @0x00000000036194c0

julia> unsafe_load(k)
node_t(29544064,Ptr{node_t} @0x3038662d34363a34,Ptr{node_t} @0x3a386e2d3832313a,Ptr{node_t} @0x34363a32333a3631)

julia> update_parent(x::node_t, n::Ptr{node_t}) =
           node_t(x.typ, x.next, x.children, n)
update_parent (generic function with 1 method)

julia> mutate!(k) = unsafe_store!(k, update_parent(unsafe_load(k), Ptr{node_t}(0)))
mutate! (generic function with 1 method)

julia> @code_llvm mutate!(k)

define %node_t* @"julia_mutate!_70963"(%node_t*) #0 {
top:
  %1 = load %node_t, %node_t* %0, align 1
  %.fca.3.insert = insertvalue %node_t %1, %node_t* null, 3
  store %node_t %.fca.3.insert, %node_t* %0, align 1
  ret %node_t* %0
}

完美答案!我不知道
fieldoffset
,这样的优化效果非常好。非常感谢!。