-
Notifications
You must be signed in to change notification settings - Fork 9
/
jl_ref.jl
46 lines (36 loc) · 1.04 KB
/
jl_ref.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Test Julia's Ref mechanism and compare it to Ptr.
#
if !isdefined(:libn)
const refc = Pkg.dir("CSDP", "test", "ref.c")
const libn = Pkg.dir("CSDP", "deps", "usr", "lib", "ref.$(Libdl.dlext)")
isdir(dirname(libn)) || mkdir(dirname(libn))
end
run(`gcc -fPIC -shared -o $libn -std=c99 $refc`)
const OP1 = UInt32(0)
const OP2 = UInt32(1)
"""Simple type containing an enum and Ptr"""
mutable struct S
c::UInt32
n::Cint
e::Ptr{Cdouble}
end
"""Use Ref instead of Ptr"""
mutable struct R
c::UInt32
n::Cint
e::Ref{Cdouble}
end
# Say hello, print sizeof(int)
println(ccall((:hello,libn), Cdouble, ()))
vec = Cdouble[1.0, 2.0]
println("&vec = $(pointer(vec))")
# Call with S --> should work
s = S(OP1, length(vec), pointer(vec))
ccall((:sum,libn), Cdouble, (S,), s)
# Call with R --> does not work
r = R(OP2, length(vec), Ref(vec))
ccall((:sum,libn), Cdouble, (R,), r)
# Now call with S, instead of R
Base.cconvert(::Type{R}, r::R) =
S(r.c, r.n, Base.unsafe_convert(Ptr{Cdouble}, r.e))
ccall((:sum,libn), Cdouble, (S,), r)