Skip to content

Commit a378b75

Browse files
committed
ccall improvements for issue #132
- conversions are inserted for the arguments, to the ccall argument types - syntax &x is used to pass a pointer to a scalar
1 parent 162bc6b commit a378b75

33 files changed

+230
-210
lines changed

examples/bitvector.j

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,5 @@ end
125125
length(s::IntSet) = numel(s)
126126
numel(s::IntSet) =
127127
int32(ccall(:bitvector_count, Uint64, (Ptr{Uint32}, Uint64, Uint64),
128-
s.bits, uint64(0), uint64(s.limit)))
128+
s.bits, 0, s.limit))
129129

j/array.j

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ numel(a::Array) = arraylen(a)
1919
## copy ##
2020

2121
copy_to{T}(dest::Ptr{T}, src::Ptr{T}, n::Integer) =
22-
ccall(:memcpy, Ptr{Void}, (Ptr{Void}, Ptr{Void}, Uint), dest, src, uint(n*sizeof(T)))
22+
ccall(:memcpy, Ptr{Void}, (Ptr{Void}, Ptr{Void}, Uint), dest, src, n*sizeof(T))
2323

2424
function copy_to{T}(dest::Array{T}, do, src::Array{T}, so, N)
2525
if so+N-1 > numel(src) || do+N-1 > numel(dest) || do < 1 || so < 1
@@ -40,7 +40,7 @@ copy_to{T}(dest::Array{T}, src::Array{T}) = copy_to(dest, 1, src, 1, numel(src))
4040
function reinterpret{T,S}(::Type{T}, a::Array{S})
4141
b = Array(T, div(numel(a)*sizeof(S),sizeof(T)))
4242
ccall(:memcpy, Ptr{T}, (Ptr{T}, Ptr{S}, Uint),
43-
b, a, uint(length(b)*sizeof(T)))
43+
b, a, length(b)*sizeof(T))
4444
return b
4545
end
4646
reinterpret(t,x) = reinterpret(t,[x])[1]
@@ -81,12 +81,12 @@ ref{T}(::Type{T}) = Array(T,0)
8181
ref{T}(::Type{T}, x) = (a=Array(T,1); a[1]=x; a)
8282

8383
function fill!{T<:Union(Int8,Uint8)}(a::Array{T}, x::Integer)
84-
ccall(:memset, Void, (Ptr{T}, Int32, Int), a, int32(x), int(length(a)))
84+
ccall(:memset, Void, (Ptr{T}, Int32, Int), a, x, length(a))
8585
return a
8686
end
8787
function fill!{T<:Union(Integer,Float)}(a::Array{T}, x)
8888
if convert(T,x) == 0
89-
ccall(:bzero, Void, (Ptr{T}, Int), a, int(length(a)*sizeof(T)))
89+
ccall(:bzero, Void, (Ptr{T}, Int), a, length(a)*sizeof(T))
9090
else
9191
for i = 1:numel(a)
9292
a[i] = x
@@ -417,13 +417,13 @@ function push{T}(a::Array{T,1}, item)
417417
end
418418
# convert first so we don't grow the array if the assignment won't work
419419
item = convert(T, item)
420-
ccall(:jl_array_grow_end, Void, (Any, Uint), a, uint(1))
420+
ccall(:jl_array_grow_end, Void, (Any, Uint), a, 1)
421421
a[end] = item
422422
return a
423423
end
424424

425425
function push(a::Array{Any,1}, item::ANY)
426-
ccall(:jl_array_grow_end, Void, (Any, Uint), a, uint(1))
426+
ccall(:jl_array_grow_end, Void, (Any, Uint), a, 1)
427427
a[end] = item
428428
return a
429429
end
@@ -433,13 +433,13 @@ function append!{T}(a::Array{T,1}, items::Array{T,1})
433433
error("[] cannot grow. Instead, initialize the array with \"T[]\".")
434434
end
435435
n = length(items)
436-
ccall(:jl_array_grow_end, Void, (Any, Uint), a, uint(n))
436+
ccall(:jl_array_grow_end, Void, (Any, Uint), a, n)
437437
a[end-n+1:end] = items
438438
return a
439439
end
440440

441441
function grow(a::Vector, n::Integer)
442-
ccall(:jl_array_grow_end, Void, (Any, Uint), a, uint(n))
442+
ccall(:jl_array_grow_end, Void, (Any, Uint), a, n)
443443
return a
444444
end
445445

@@ -448,7 +448,7 @@ function pop(a::Vector)
448448
error("pop: array is empty")
449449
end
450450
item = a[end]
451-
ccall(:jl_array_del_end, Void, (Any, Uint), a, uint(1))
451+
ccall(:jl_array_del_end, Void, (Any, Uint), a, 1)
452452
return item
453453
end
454454

@@ -457,7 +457,7 @@ function enqueue{T}(a::Array{T,1}, item)
457457
error("[] cannot grow. Instead, initialize the array with \"T[]\".")
458458
end
459459
item = convert(T, item)
460-
ccall(:jl_array_grow_beg, Void, (Any, Uint), a, uint(1))
460+
ccall(:jl_array_grow_beg, Void, (Any, Uint), a, 1)
461461
a[1] = item
462462
return a
463463
end
@@ -468,7 +468,7 @@ function shift(a::Vector)
468468
error("shift: array is empty")
469469
end
470470
item = a[1]
471-
ccall(:jl_array_del_beg, Void, (Any, Uint), a, uint(1))
471+
ccall(:jl_array_del_beg, Void, (Any, Uint), a, 1)
472472
return item
473473
end
474474

@@ -479,14 +479,14 @@ function insert{T}(a::Array{T,1}, i::Integer, item)
479479
item = convert(T, item)
480480
n = length(a)
481481
if i > n
482-
ccall(:jl_array_grow_end, Void, (Any, Uint), a, uint(i-n))
482+
ccall(:jl_array_grow_end, Void, (Any, Uint), a, i-n)
483483
elseif i > div(n,2)
484-
ccall(:jl_array_grow_end, Void, (Any, Uint), a, uint(1))
484+
ccall(:jl_array_grow_end, Void, (Any, Uint), a, 1)
485485
for k=n+1:-1:i+1
486486
a[k] = a[k-1]
487487
end
488488
else
489-
ccall(:jl_array_grow_beg, Void, (Any, Uint), a, uint(1))
489+
ccall(:jl_array_grow_beg, Void, (Any, Uint), a, 1)
490490
for k=1:(i-1)
491491
a[k] = a[k+1]
492492
end
@@ -503,12 +503,12 @@ function del(a::Vector, i::Integer)
503503
for k = i:-1:2
504504
a[k] = a[k-1]
505505
end
506-
ccall(:jl_array_del_beg, Void, (Any, Uint), a, uint(1))
506+
ccall(:jl_array_del_beg, Void, (Any, Uint), a, 1)
507507
else
508508
for k = i:n-1
509509
a[k] = a[k+1]
510510
end
511-
ccall(:jl_array_del_end, Void, (Any, Uint), a, uint(1))
511+
ccall(:jl_array_del_end, Void, (Any, Uint), a, 1)
512512
end
513513
return a
514514
end
@@ -528,18 +528,18 @@ function del{T<:Integer}(a::Vector, r::Range1{T})
528528
for k = l:-1:1+d
529529
a[k] = a[k-d]
530530
end
531-
ccall(:jl_array_del_beg, Void, (Any, Uint), a, uint(d))
531+
ccall(:jl_array_del_beg, Void, (Any, Uint), a, d)
532532
else
533533
for k = f:n-d
534534
a[k] = a[k+d]
535535
end
536-
ccall(:jl_array_del_end, Void, (Any, Uint), a, uint(d))
536+
ccall(:jl_array_del_end, Void, (Any, Uint), a, d)
537537
end
538538
return a
539539
end
540540

541541
function del_all(a::Vector)
542-
ccall(:jl_array_del_end, Void, (Any, Uint), a, uint(length(a)))
542+
ccall(:jl_array_del_end, Void, (Any, Uint), a, length(a))
543543
return a
544544
end
545545

j/base.j

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ dlsym(hnd, s::String) =
9191
ccall(:jl_dlsym, Ptr{Void}, (Ptr{Void}, Ptr{Uint8}), hnd, cstring(s))
9292

9393
dlsym(hnd, s::Symbol) =
94-
ccall(:jl_dlsym, Ptr{Void}, (Ptr{Void}, Ptr{Uint8}),
95-
hnd, convert(Ptr{Uint8}, s))
94+
ccall(:jl_dlsym, Ptr{Void}, (Ptr{Void}, Ptr{Uint8}), hnd, s)
9695

9796
dlopen(fname::String) =
9897
ccall(:jl_load_dynamic_library, Ptr{Void}, (Ptr{Uint8},), cstring(fname))
@@ -152,10 +151,10 @@ Array(T, d::Int...) = Array(T, d)
152151

153152
Array{T}(::Type{T}, m::Integer) =
154153
ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1},
155-
int(m))
154+
m)
156155
Array{T}(::Type{T}, m::Integer,n::Integer) =
157156
ccall(:jl_alloc_array_2d, Array{T,2}, (Any,Int,Int), Array{T,2},
158-
int(m), int(n))
157+
m, n)
159158
Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) =
160159
ccall(:jl_alloc_array_3d, Array{T,3}, (Any,Int,Int,Int), Array{T,3},
161-
int(m), int(n), int(o))
160+
m, n, o)

j/env.j

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ end
1313

1414
function setenv(var::String, val::String, overwrite::Bool)
1515
ret = ccall(:setenv, Int32, (Ptr{Uint8}, Ptr{Uint8}, Int32),
16-
cstring(var), cstring(val), int32(overwrite))
16+
cstring(var), cstring(val), overwrite)
1717
system_error(:setenv, ret != 0)
1818
end
1919

@@ -51,9 +51,9 @@ del(::EnvHash, k::String) = unsetenv(k)
5151
assign(::EnvHash, v::String, k::String) = (setenv(k,v); v)
5252

5353
start(::EnvHash) = 0
54-
done(::EnvHash, i) = (ccall(:jl_environ, Any, (Int32,), int32(i)) == nothing)
54+
done(::EnvHash, i) = (ccall(:jl_environ, Any, (Int32,), i) == nothing)
5555
function next(::EnvHash, i)
56-
env = ccall(:jl_environ, Any, (Int32,), int32(i))
56+
env = ccall(:jl_environ, Any, (Int32,), i)
5757
if env == nothing
5858
error("environ: index out of range")
5959
end

j/error.j

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ end
1111
## system error handling ##
1212

1313
errno() = ccall(:jl_errno, Int32, ())
14-
strerror(e::Integer) = ccall(:jl_strerror, Any, (Int32,), int32(e))::ByteString
14+
strerror(e::Integer) = ccall(:jl_strerror, Any, (Int32,), e)::ByteString
1515
strerror() = strerror(errno())
1616
system_error(p, b::Bool) = b ? error(SystemError(string(p))) : nothing
1717

j/expr.j

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
symbol(s::ASCIIString) = symbol(s.data)
44
symbol(s::UTF8String) = symbol(s.data)
55
symbol(a::Array{Uint8,1}) =
6-
ccall(:jl_symbol_n, Any, (Ptr{Uint8}, Int32), a, int32(length(a)))::Symbol
6+
ccall(:jl_symbol_n, Any, (Ptr{Uint8}, Int32), a, length(a))::Symbol
77

88
gensym() = ccall(:jl_gensym, Any, ())::Symbol
99
gensym(n::Integer) = ntuple(n, i->gensym())

j/grisu.j

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ macro grisu_ccall(value, mode, ndigits)
1111
ccall(dlsym(_jl_libgrisu, :grisu), Void,
1212
(Float64, Int32, Int32, Ptr{Uint8}, Int32,
1313
Ptr{Bool}, Ptr{Int32}, Ptr{Int32}),
14-
float64($value), int32($mode), int32($ndigits),
14+
$value, $mode, $ndigits,
1515
_jl_digits, _jl_buflen, _jl_neg, _jl_length, _jl_point)
1616
end
1717
end

j/inference.j

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tmatch(a::ANY,b::ANY) = ccall(:jl_type_match, Any, (Any,Any), a, b)
3131

3232
getmethods(f,t) = getmethods(f,t,-1)::Array{Any,1}
3333
getmethods(f,t,lim) = ccall(:jl_matching_methods, Any, (Any,Any,Int32),
34-
f, t, int32(lim))
34+
f, t, lim)
3535

3636
typeseq(a,b) = subtype(a,b)&&subtype(b,a)
3737

j/intset.j

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ start(s::IntSet) = int64(0)
9999
done(s::IntSet, i) = (next(s,i)[1] >= s.limit)
100100
function next(s::IntSet, i)
101101
n = ccall(:bitvector_next, Int64, (Ptr{Uint32}, Uint64, Uint64),
102-
s.bits, uint64(i), uint64(s.limit))
102+
s.bits, i, s.limit)
103103
(n, n+1)
104104
end
105105

106106
isempty(s::IntSet) =
107107
ccall(:bitvector_any1, Uint32, (Ptr{Uint32}, Uint64, Uint64),
108-
s.bits, uint64(0), uint64(s.limit))==0
108+
s.bits, 0, s.limit)==0
109109

110110
function choose(s::IntSet)
111111
n = next(s,0)[1]
@@ -123,7 +123,7 @@ end
123123

124124
numel(s::IntSet) =
125125
int(ccall(:bitvector_count, Uint64, (Ptr{Uint32}, Uint64, Uint64),
126-
s.bits, uint64(0), uint64(s.limit)))
126+
s.bits, 0, s.limit))
127127

128128
function show(s::IntSet)
129129
print("intset(")

j/io.j

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ close(s::IOStream) = ccall(:ios_close, Void, (Ptr{Void},), s.ios)
2727
function fdio(fd::Integer, own::Bool)
2828
s = IOStream()
2929
ccall(:ios_fd, Void, (Ptr{Uint8}, Int, Int32, Int32),
30-
s.ios, int(fd), int32(0), int32(own));
30+
s.ios, fd, 0, own);
3131
return s
3232
end
3333
fdio(fd::Integer) = fdio(fd, false)
@@ -36,8 +36,7 @@ function open(fname::String, rd::Bool, wr::Bool, cr::Bool, tr::Bool, ff::Bool)
3636
s = IOStream()
3737
if ccall(:ios_file, Ptr{Void},
3838
(Ptr{Uint8}, Ptr{Uint8}, Int32, Int32, Int32, Int32),
39-
s.ios, cstring(fname),
40-
int32(rd), int32(wr), int32(cr), int32(tr)) == C_NULL
39+
s.ios, cstring(fname), rd, wr, cr, tr) == C_NULL
4140
error("could not open file ", fname)
4241
end
4342
if ff && ccall(:ios_seek_end, Uint, (Ptr{Void},), s.ios) != 0
@@ -59,7 +58,7 @@ end
5958

6059
function memio(x::Integer, finalize::Bool)
6160
s = IOStream(finalize)
62-
ccall(:jl_ios_mem, Ptr{Void}, (Ptr{Uint8}, Uint), s.ios, uint(x))
61+
ccall(:jl_ios_mem, Ptr{Void}, (Ptr{Uint8}, Uint), s.ios, x)
6362
s
6463
end
6564
memio(x::Integer) = memio(x, true)
@@ -166,7 +165,7 @@ end
166165
## low-level calls ##
167166

168167
write(s::IOStream, b::Uint8) =
169-
ccall(:ios_putc, Int32, (Int32, Ptr{Void}), int32(b), s.ios)
168+
ccall(:ios_putc, Int32, (Int32, Ptr{Void}), b, s.ios)
170169

171170
write(s::IOStream, c::Char) =
172171
ccall(:ios_pututf8, Int32, (Ptr{Void}, Char), s.ios, c)
@@ -175,16 +174,15 @@ function write{T}(s::IOStream, a::Array{T})
175174
if isa(T,BitsKind)
176175
ccall(:ios_write, Uint,
177176
(Ptr{Void}, Ptr{Void}, Uint),
178-
s.ios, a, uint(numel(a)*sizeof(T)))
177+
s.ios, a, numel(a)*sizeof(T))
179178
else
180179
invoke(write, (Any, Array), s, a)
181180
end
182181
end
183182

184183
function write(s::IOStream, p::Ptr, nb::Integer)
185184
ccall(:ios_write, Uint,
186-
(Ptr{Void}, Ptr{Void}, Uint),
187-
s.ios, p, uint(nb))
185+
(Ptr{Void}, Ptr{Void}, Uint), s.ios, p, nb)
188186
end
189187

190188
function write{T,N}(s::IOStream, a::SubArray{T,N,Array})
@@ -219,7 +217,7 @@ function read{T}(s::IOStream, a::Array{T})
219217
if isa(T,BitsKind)
220218
nb = numel(a)*sizeof(T)
221219
if ccall(:ios_readall, Uint,
222-
(Ptr{Void}, Ptr{Void}, Uint), s.ios, a, uint(nb)) < nb
220+
(Ptr{Void}, Ptr{Void}, Uint), s.ios, a, nb) < nb
223221
throw(EOFError())
224222
end
225223
a
@@ -246,14 +244,14 @@ readline(s::IOStream) = readuntil(s, uint8('\n'))
246244
flush(s::IOStream) = ccall(:ios_flush, Void, (Ptr{Void},), s.ios)
247245

248246
truncate(s::IOStream, n::Integer) =
249-
ccall(:ios_trunc, Uint, (Ptr{Void}, Uint), s.ios, uint(n))
247+
ccall(:ios_trunc, Uint, (Ptr{Void}, Uint), s.ios, n)
250248

251249
seek(s::IOStream, n::Integer) =
252-
(ccall(:ios_seek, Int, (Ptr{Void}, Int), s.ios, int(n))==0 ||
250+
(ccall(:ios_seek, Int, (Ptr{Void}, Int), s.ios, n)==0 ||
253251
error("seek failed"))
254252

255253
skip(s::IOStream, delta::Integer) =
256-
(ccall(:ios_skip, Int, (Ptr{Void}, Int), s.ios, int(delta))==0 ||
254+
(ccall(:ios_skip, Int, (Ptr{Void}, Int), s.ios, delta)==0 ||
257255
error("skip failed"))
258256

259257
position(s::IOStream) = ccall(:ios_pos, Int, (Ptr{Void},), s.ios)
@@ -285,7 +283,7 @@ function add(s::FDSet, i::Integer)
285283
if !(0 <= i < sizeof_fd_set*8)
286284
error("invalid descriptor ", i)
287285
end
288-
ccall(:jl_fd_set, Void, (Ptr{Void}, Int32), s.data, int32(i))
286+
ccall(:jl_fd_set, Void, (Ptr{Void}, Int32), s.data, i)
289287
if i >= s.nfds
290288
s.nfds = i+1
291289
end
@@ -295,14 +293,14 @@ end
295293
function has(s::FDSet, i::Integer)
296294
if 0 <= i < sizeof_fd_set*8
297295
return ccall(:jl_fd_isset, Int32,
298-
(Ptr{Void}, Int32), s.data, int32(i))!=0
296+
(Ptr{Void}, Int32), s.data, i)!=0
299297
end
300298
return false
301299
end
302300

303301
function del(s::FDSet, i::Integer)
304302
if 0 <= i < sizeof_fd_set*8
305-
ccall(:jl_fd_clr, Void, (Ptr{Void}, Int32), s.data, int32(i))
303+
ccall(:jl_fd_clr, Void, (Ptr{Void}, Int32), s.data, i)
306304
if i == s.nfds-1
307305
s.nfds -= 1
308306
while s.nfds>0 && !has(s, s.nfds-1)
@@ -327,7 +325,7 @@ begin
327325
tout = C_NULL
328326
else
329327
ccall(:jl_set_timeval, Void, (Ptr{Void}, Float64),
330-
tv, float64(timeout))
328+
tv, timeout)
331329
tout = convert(Ptr{Void}, tv)
332330
end
333331
ccall(:select, Int32,

0 commit comments

Comments
 (0)