Updating a field value of a C struct from Julia

ε祈祈猫儿з 提交于 2019-12-05 20:30:33

The fieldoffset function is provided for this use case:

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

But also don't be worried about simply storing the whole immutable, with one field changed; it will be optimized away.

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
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!