Skip to content

Commit

Permalink
Use dlsym(libx,:sym) form everywhere.
Browse files Browse the repository at this point in the history
This form is terser, stylistically better, and less
prone to bootstrapping issues involving strings.

Allow system_error to take a symbol as its first arg
to match dlsym stylistically, since these calls are
often paired.
  • Loading branch information
StefanKarpinski committed Jan 8, 2011
1 parent 5e8a908 commit 72ac155
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion array.j
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ hcat(A::Array...) = cat(2, A...)

function reinterpret{T,S}(::Type{T}, a::Array{S})
b = Array(T, div(numel(a)*sizeof(S),sizeof(T)))
ccall(dlsym(libc,"memcpy"),
ccall(dlsym(libc, :memcpy),
Ptr{T}, (Ptr{T}, Ptr{S}, Size),
b, a, length(b)*sizeof(T))
b
Expand Down
1 change: 1 addition & 0 deletions error.j
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ errno() = ccall(:jl_errno, Int32, ())
strerror(e::Int) = ccall(:jl_strerror, Any, (Int32,), int32(e))::ByteString
strerror() = strerror(errno())
system_error(p::String, b::Bool) = b ? error(SystemError(p)) : true
system_error(s::Symbol, b::Bool) = system_error(string(s), b)

## assertion functions and macros ##

Expand Down
34 changes: 17 additions & 17 deletions libc.j
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## time-related functions ##

sleep(s::Real) = ccall(dlsym(libc,"usleep"), Uint32, (Uint32,), uint32(round(s*1e6)))
unixtime() = ccall(dlsym(libc,"time"), Uint32, (Ptr{Uint32},), C_NULL)
sleep(s::Real) = ccall(dlsym(libc, :usleep), Uint32, (Uint32,), uint32(round(s*1e6)))
unixtime() = ccall(dlsym(libc, :time), Uint32, (Ptr{Uint32},), C_NULL)
function ftime()
t = Array(Uint64,2)
ccall(dlsym(libc,"gettimeofday"), Int32,
ccall(dlsym(libc, :gettimeofday), Int32,
(Ptr{Uint64}, Ptr{Uint8}),
t, C_NULL)
# TODO: this is a hack and probably doesn't work on big-endian systems
Expand All @@ -16,11 +16,11 @@ end
## process-related functions ##
system(cmd::String) =
ccall(dlsym(libc,"system"), Int32, (Ptr{Uint8},), cstring(cmd))
ccall(dlsym(libc, :system), Int32, (Ptr{Uint8},), cstring(cmd))
function fork()
pid = ccall(dlsym(libc,"fork"), Int32, ())
system_error("fork", pid < 0)
pid = ccall(dlsym(libc, :fork), Int32, ())
system_error(:fork, pid < 0)
return pid
end
Expand All @@ -30,45 +30,45 @@ function exec(cmd::String, args...)
arr[1] = cmd
for i = 1:length(args); arr[i+1] = cstring(args[i]); end
arr[length(args)+2] = C_NULL
ccall(dlsym(libc,"execvp"), Int32,
ccall(dlsym(libc, :execvp), Int32,
(Ptr{Uint8}, Ptr{Ptr{Uint8}}),
cmd, arr)
system_error("exec", true)
system_error(:exec, true)
end
function wait(pid::Int32)
status = Array(Int32,1)
ret = ccall(dlsym(libc,"waitpid"), Int32,
ret = ccall(dlsym(libc, :waitpid), Int32,
(Int32, Ptr{Int32}, Int32),
pid, status, 0)
system_error("wait", ret == -1)
system_error(:wait, ret == -1)
status[1]
end
exit(n) = ccall(dlsym(libc,"exit"), Void, (Int32,), int32(n))
exit(n) = ccall(dlsym(libc, :exit), Void, (Int32,), int32(n))
exit() = exit(0)
## environment ##
hasenv(var::String) =
ccall(dlsym(libc,"getenv"), Ptr{Uint8}, (Ptr{Uint8},), var) != C_NULL
ccall(dlsym(libc, :getenv), Ptr{Uint8}, (Ptr{Uint8},), var) != C_NULL
function getenv(var::String)
val = ccall(dlsym(libc,"getenv"), Ptr{Uint8}, (Ptr{Uint8},), var)
val = ccall(dlsym(libc, :getenv), Ptr{Uint8}, (Ptr{Uint8},), var)
if val == C_NULL; error("getenv: Undefined variable: ", var); end
return string(val)
end
function setenv(var::String, val::String)
ret = ccall(dlsym(libc,"setenv"), Int32,
ret = ccall(dlsym(libc, :setenv), Int32,
(Ptr{Uint8}, Ptr{Uint8}, Int32),
var, val, 1)
system_error("setenv", ret != 0)
system_error(:setenv, ret != 0)
end
function unsetenv(var::String)
ret = ccall(dlsym(libc,"unsetenv"), Int32, (Ptr{Uint8},), var)
system_error("unsetenv", ret != 0)
ret = ccall(dlsym(libc, :unsetenv), Int32, (Ptr{Uint8},), var)
system_error(:unsetenv, ret != 0)
end
tty_columns() = parse_int(Int32, getenv("COLUMNS"), 10)
8 changes: 4 additions & 4 deletions math_libm.j
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ for f = {:lrint, :lround, :ilogb}
end
end

abs(x::Float64) = ccall(dlsym(libm,"fabs"), Float64, (Float64,), x)
abs(x::Float32) = ccall(dlsym(libm,"fabsf"), Float32, (Float32,), x)
abs(x::Float64) = ccall(dlsym(libm, :fabs), Float64, (Float64,), x)
abs(x::Float32) = ccall(dlsym(libm, :fabsf), Float32, (Float32,), x)
@vectorize abs

for f = {:atan2, :pow, :fmod, :copysign, :hypot, :fmin, :fmax, :fdim}
Expand All @@ -56,8 +56,8 @@ for f = {:atan2, :pow, :fmod, :copysign, :hypot, :fmin, :fmax, :fdim}
end
end

ldexp(x::Float64,e::Int32) = ccall(dlsym(libm,"ldexp"), Float64, (Float64,Int32), x, e)
ldexp(x::Float32,e::Int32) = ccall(dlsym(libm,"ldexpf"), Float32, (Float32,Int32), x, e)
ldexp(x::Float64,e::Int32) = ccall(dlsym(libm, :ldexp), Float64, (Float64,Int32), x, e)
ldexp(x::Float32,e::Int32) = ccall(dlsym(libm, :ldexpf), Float32, (Float32,Int32), x, e)

rand() = ccall(:rand_double, Float64, ())
randf() = ccall(:rand_float, Float32, ())
Expand Down
12 changes: 6 additions & 6 deletions multi.j
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ end

function start_local_worker()
fds = Array(Int32, 2)
ccall(dlsym(libc,"pipe"), Int32, (Ptr{Int32},), fds)
ccall(dlsym(libc, :pipe), Int32, (Ptr{Int32},), fds)
rdfd = fds[1]
wrfd = fds[2]

Expand All @@ -88,15 +88,15 @@ function start_local_worker()
write(io, port[1])
close(io)
# close stdin; workers will not use it
ccall(dlsym(libc,"close"), Int32, (Int32,), 0)
ccall(dlsym(libc, :close), Int32, (Int32,), 0)

connectfd = ccall(dlsym(libc,"accept"), Int32,
connectfd = ccall(dlsym(libc, :accept), Int32,
(Int32, Ptr{Void}, Ptr{Void}),
sockfd, C_NULL, C_NULL)
jl_worker(connectfd)
ccall(dlsym(libc,"close"), Int32, (Int32,), connectfd)
ccall(dlsym(libc,"close"), Int32, (Int32,), sockfd)
ccall(dlsym(libc,"exit") , Void , (Int32,), 0)
ccall(dlsym(libc, :close), Int32, (Int32,), connectfd)
ccall(dlsym(libc, :close), Int32, (Int32,), sockfd)
ccall(dlsym(libc, :exit) , Void , (Int32,), 0)
end
io = fdio(rdfd)
port = read(io, Int16)
Expand Down
12 changes: 6 additions & 6 deletions process.j
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ other(p::PipeOut) = p.pipe.in

function make_pipe()
fds = Array(Int32, 2)
ret = ccall(dlsym(libc,"pipe"), Int32, (Ptr{Int32},), fds)
system_error("make_pipe", ret != 0)
ret = ccall(dlsym(libc, :pipe), Int32, (Ptr{Int32},), fds)
system_error(:make_pipe, ret != 0)
Pipe(FileDes(fds[2]), FileDes(fds[1]))
end

function dup2(fd1::FileDes, fd2::FileDes)
ret = ccall(dlsym(libc,"dup2"), Int32, (Int32, Int32), fd1.fd, fd2.fd)
system_error("dup2", ret == -1)
ret = ccall(dlsym(libc, :dup2), Int32, (Int32, Int32), fd1.fd, fd2.fd)
system_error(:dup2, ret == -1)
end

function close(fd::FileDes)
ret = ccall(dlsym(libc,"close"), Int32, (Int32,), fd.fd)
system_error("close", ret != 0)
ret = ccall(dlsym(libc, :close), Int32, (Int32,), fd.fd)
system_error(:close, ret != 0)
end

typealias Executable Union((String,Tuple),Function)
Expand Down
10 changes: 5 additions & 5 deletions regex.j
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("pcre_h.j")

libpcre = dlopen("libpcre")

PCRE_VERSION = string(ccall(dlsym(libpcre,"pcre_version"), Ptr{Uint8}, ()))
PCRE_VERSION = string(ccall(dlsym(libpcre, :pcre_version), Ptr{Uint8}, ()))

## masks for supported sets of options ##

Expand Down Expand Up @@ -52,7 +52,7 @@ PCRE_OPTIONS_MASK = PCRE_COMPILE_MASK | PCRE_EXECUTE_MASK
function pcre_compile(pattern::String, options::Int)
errstr = Array(Ptr{Uint8},1)
erroff = Array(Int32,1)
regex = (()->ccall(dlsym(libpcre,"pcre_compile"), Ptr{Void},
regex = (()->ccall(dlsym(libpcre, :pcre_compile), Ptr{Void},
(Ptr{Uint8}, Int32, Ptr{Ptr{Uint8}}, Ptr{Int32}, Ptr{Uint8}),
cstring(pattern), int32(options), errstr, erroff, C_NULL))()
if regex == C_NULL
Expand All @@ -67,7 +67,7 @@ end

function pcre_study(regex::Ptr{Void}, options::Int)
errstr = Array(Ptr{Uint8},1)
extra = (()->ccall(dlsym(libpcre,"pcre_study"), Ptr{Void},
extra = (()->ccall(dlsym(libpcre, :pcre_study), Ptr{Void},
(Ptr{Void}, Int32, Ptr{Ptr{Uint8}}),
regex, int32(options), errstr))()
if errstr[1] != C_NULL
Expand All @@ -78,7 +78,7 @@ end

function pcre_info{T}(regex::Ptr{Void}, extra::Ptr{Void}, what::Int32, ::Type{T})
buf = Array(Uint8,sizeof(T))
ret = ccall(dlsym(libpcre,"pcre_fullinfo"), Int32,
ret = ccall(dlsym(libpcre, :pcre_fullinfo), Int32,
(Ptr{Void}, Ptr{Void}, Int32, Ptr{Uint8}),
regex, extra, what, buf)
if ret != 0
Expand All @@ -96,7 +96,7 @@ function pcre_exec(regex::Ptr{Void}, extra::Ptr{Void},
offset::Index, options::Int)
ncap = pcre_info(regex, extra, PCRE_INFO_CAPTURECOUNT, Int32)
ovec = Array(Int32, 3*(ncap+1))
n = ccall(dlsym(libpcre,"pcre_exec"), Int32,
n = ccall(dlsym(libpcre, :pcre_exec), Int32,
(Ptr{Void}, Ptr{Void}, Ptr{Uint8}, Int32, Int32,
Int32, Ptr{Int32}, Int32),
regex, extra, string, length(string), offset-1,
Expand Down
4 changes: 2 additions & 2 deletions string.j
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ hex(n::Int, l::Int) = lpad(hex(n), l, '0')
## lexicographically compare byte arrays (used by Latin-1 and UTF-8) ##

function lexcmp(a::Array{Uint8,1}, b::Array{Uint8,1})
c = ccall(dlsym(libc,"memcmp"), Int32,
c = ccall(dlsym(libc, :memcmp), Int32,
(Ptr{Uint8}, Ptr{Uint8}, Size),
a, b, min(length(a),length(b)))
c < 0 ? -1 : c > 0 ? +1 : cmp(length(a),length(b))
Expand All @@ -656,7 +656,7 @@ end

function memchr(a::Array{Uint8,1}, b::Int)
p = pointer(a)
q = ccall(dlsym(libc,"memchr"), Ptr{Uint8},
q = ccall(dlsym(libc, :memchr), Ptr{Uint8},
(Ptr{Uint8}, Int32, Size),
p, int32(b), length(a))
if q == C_NULL
Expand Down

0 comments on commit 72ac155

Please sign in to comment.