Skip to content

Commit

Permalink
create Libc and Libdl modules for the relevant functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Mar 9, 2015
1 parent 5bacb18 commit 67d3cd2
Show file tree
Hide file tree
Showing 26 changed files with 317 additions and 331 deletions.
2 changes: 1 addition & 1 deletion base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ function add_history(hist::REPLHistoryProvider, s)
push!(hist.history, str)
hist.history_file == nothing && return
entry = """
# time: $(strftime("%Y-%m-%d %H:%M:%S %Z", time()))
# time: $(Libc.strftime("%Y-%m-%d %H:%M:%S %Z", time()))
# mode: $mode
$(replace(str, r"^"ms, "\t"))
"""
Expand Down
59 changes: 1 addition & 58 deletions base/c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,7 @@

import Core.Intrinsics.cglobal

# constants to match JL_RTLD_* in src/julia.h
const RTLD_LOCAL = 0x00000000
const RTLD_GLOBAL = 0x00000001
const RTLD_LAZY = 0x00000002
const RTLD_NOW = 0x00000004
const RTLD_NODELETE = 0x00000008
const RTLD_NOLOAD = 0x00000010
const RTLD_DEEPBIND = 0x00000020
const RTLD_FIRST = 0x00000040

function dlsym(hnd::Ptr, s::Union(Symbol,AbstractString))
hnd == C_NULL && error("NULL library handle")
ccall(:jl_dlsym, Ptr{Void}, (Ptr{Void}, Ptr{UInt8}), hnd, s)
end

function dlsym_e(hnd::Ptr, s::Union(Symbol,AbstractString))
hnd == C_NULL && error("NULL library handle")
ccall(:jl_dlsym_e, Ptr{Void}, (Ptr{Void}, Ptr{UInt8}), hnd, s)
end

dlopen(s::Symbol, flags::Integer = RTLD_LAZY | RTLD_DEEPBIND) =
dlopen(string(s), flags)

dlopen(s::AbstractString, flags::Integer = RTLD_LAZY | RTLD_DEEPBIND) =
ccall(:jl_load_dynamic_library, Ptr{Void}, (Ptr{UInt8},UInt32), s, flags)

dlopen_e(s::AbstractString, flags::Integer = RTLD_LAZY | RTLD_DEEPBIND) =
ccall(:jl_load_dynamic_library_e, Ptr{Void}, (Ptr{UInt8},UInt32), s, flags)

dlopen_e(s::Symbol, flags::Integer = RTLD_LAZY | RTLD_DEEPBIND) =
dlopen_e(string(s), flags)

dlclose(p::Ptr) = if p!=C_NULL; ccall(:uv_dlclose,Void,(Ptr{Void},),p); end

cfunction(f::Function, r, a) =
ccall(:jl_function_ptr, Ptr{Void}, (Any, Any, Any), f, r, a)
cfunction(f::Function, r, a) = ccall(:jl_function_ptr, Ptr{Void}, (Any, Any, Any), f, r, a)

if ccall(:jl_is_char_signed, Any, ())
typealias Cchar Int8
Expand Down Expand Up @@ -90,28 +55,6 @@ sigatomic_end() = ccall(:jl_sigatomic_end, Void, ())
disable_sigint(f::Function) = try sigatomic_begin(); f(); finally sigatomic_end(); end
reenable_sigint(f::Function) = try sigatomic_end(); f(); finally sigatomic_begin(); end

# flush C stdio output from external libraries
flush_cstdio() = ccall(:jl_flush_cstdio, Void, ())

function find_library{T<:ByteString, S<:ByteString}(libnames::Array{T,1}, extrapaths::Array{S,1}=ASCIIString[])
for lib in libnames
for path in extrapaths
l = joinpath(path, lib)
p = dlopen_e(l, RTLD_LAZY)
if p != C_NULL
dlclose(p)
return l
end
end
p = dlopen_e(lib, RTLD_LAZY)
if p != C_NULL
dlclose(p)
return lib
end
end
return ""
end

function ccallable(f::Function, rt::Type, argt::(Type...), name::Union(AbstractString,Symbol)=string(f))
ccall(:jl_extern_c, Void, (Any, Any, Any, Ptr{UInt8}), f, rt, argt, name)
end
Expand Down
2 changes: 1 addition & 1 deletion base/dates/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ end
# Returns unix seconds since 1970-01-01T00:00:00
datetime2unix(dt::DateTime) = (value(dt) - UNIXEPOCH)/1000.0
function now()
tm = TmStruct(time())
tm = Libc.TmStruct(time())
return DateTime(tm.year+1900,tm.month+1,tm.mday,tm.hour,tm.min,tm.sec)
end
today() = Date(now())
Expand Down
2 changes: 0 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ const Range1 = UnitRange
@deprecate set_bigfloat_rounding(r::RoundingMode) set_rounding(BigFloat,r)
@deprecate get_bigfloat_rounding() get_rounding(BigFloat)
@deprecate with_bigfloat_rounding(f::Function, r::RoundingMode) with_rounding(f::Function, BigFloat, r)
eval(Sys, :(@deprecate shlib_list dllist))
# Sys.shlib_ext is deprecated, renamed to Sys.dlext. Remove alias before release

@deprecate degrees2radians deg2rad
@deprecate radians2degrees rad2deg
Expand Down
2 changes: 0 additions & 2 deletions base/env.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
## core libc calls ##

@unix_only begin
_getenv(var::AbstractString) = ccall(:getenv, Ptr{UInt8}, (Ptr{UInt8},), var)
_hasenv(s::AbstractString) = _getenv(s) != C_NULL
Expand Down
4 changes: 0 additions & 4 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ catch_backtrace() = ccall(:jl_get_backtrace, Array{Ptr{Void},1}, ())

## system error handling ##

errno() = ccall(:jl_errno, Cint, ())
errno(e::Integer) = ccall(:jl_set_errno, Void, (Cint,), e)
strerror(e::Integer) = bytestring(ccall(:strerror, Ptr{UInt8}, (Int32,), e))
strerror() = strerror(errno())
systemerror(p, b::Bool) = b ? throw(SystemError(string(p))) : nothing

## assertion functions and macros ##
Expand Down
33 changes: 2 additions & 31 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export
Dates,
Sys,
Test,
Libc,
Libdl,
LinAlg,
BLAS,
LAPACK,
Expand All @@ -34,7 +36,6 @@ export
BufferStream,
CartesianIndex,
CartesianRange,
CFILE,
Cmd,
Colon,
Complex,
Expand Down Expand Up @@ -108,7 +109,6 @@ export
Symmetric,
SymTridiagonal,
Timer,
TmStruct,
Tridiagonal,
UnitRange,
UpperTriangular,
Expand Down Expand Up @@ -164,29 +164,17 @@ export
ARGS,
C_NULL,
CPU_CORES,
DL_LOAD_PATH,
ENDIAN_BOM,
ENV,
Inf,
Inf16,
Inf32,
JULIA_HOME,
LOAD_PATH,
MS_ASYNC,
MS_INVALIDATE,
MS_SYNC,
NaN,
NaN16,
NaN32,
OS_NAME,
RTLD_DEEPBIND,
RTLD_FIRST,
RTLD_GLOBAL,
RTLD_LAZY,
RTLD_LOCAL,
RTLD_NODELETE,
RTLD_NOLOAD,
RTLD_NOW,
STDERR,
STDIN,
STDOUT,
Expand Down Expand Up @@ -1024,8 +1012,6 @@ export

# time
sleep,
strftime,
strptime,
tic,
time,
time_ns,
Expand Down Expand Up @@ -1134,7 +1120,6 @@ export
fd,
fdio,
flush,
flush_cstdio,
getaddrinfo,
gethostname,
getipaddr,
Expand All @@ -1147,10 +1132,8 @@ export
listenany,
ltoh,
mark,
mmap,
mmap_array,
mmap_bitarray,
msync,
nb_available,
ntoh,
open,
Expand Down Expand Up @@ -1319,25 +1302,13 @@ export
success,

# C interface
c_free,
c_malloc,
c_calloc,
c_realloc,
cfunction,
cglobal,
disable_sigint,
dlclose,
dlopen,
dlopen_e,
dlsym,
dlsym_e,
errno,
find_library,
pointer,
pointer_from_objref,
pointer_to_array,
reenable_sigint,
strerror,
unsafe_copy!,
unsafe_load,
unsafe_pointer_to_objref,
Expand Down
2 changes: 1 addition & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function tempname()
p = ccall(:tempnam, Ptr{UInt8}, (Ptr{UInt8},Ptr{UInt8}), d, "julia")
systemerror(:tempnam, p == C_NULL)
s = bytestring(p)
c_free(p)
Libc.free(p)
return s
end

Expand Down
12 changes: 6 additions & 6 deletions base/fs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ uvtype(::File) = Base.UV_RAW_FD
_uv_fs_result(req) = ccall(:jl_uv_fs_result,Int32,(Ptr{Void},),req)

function open(f::File,flags::Integer,mode::Integer)
req = c_malloc(_sizeof_uv_fs)
req = Libc.malloc(_sizeof_uv_fs)
ret = ccall(:uv_fs_open,Int32,(Ptr{Void},Ptr{Void},Ptr{UInt8},Int32,Int32,Ptr{Void}),
eventloop(), req, f.path, flags,mode, C_NULL)
f.handle = _uv_fs_result(req)
ccall(:uv_fs_req_cleanup,Void,(Ptr{Void},),req)
c_free(req)
Libc.free(req)
uv_error("open",ret)
f.open = true
f
Expand Down Expand Up @@ -189,19 +189,19 @@ function write{T}(f::File, a::Array{T})
end

function truncate(f::File, n::Integer)
req = Base.c_malloc(_sizeof_uv_fs)
req = Base.Libc.malloc(_sizeof_uv_fs)
err = ccall(:uv_fs_ftruncate,Int32,(Ptr{Void},Ptr{Void},Int32,Int64,Ptr{Void}),
eventloop(),req,f.handle,n,C_NULL)
c_free(req)
Libc.free(req)
uv_error("ftruncate", err)
f
end

function futime(f::File, atime::Float64, mtime::Float64)
req = Base.c_malloc(_sizeof_uv_fs)
req = Base.Libc.malloc(_sizeof_uv_fs)
err = ccall(:uv_fs_futime,Int32,(Ptr{Void},Ptr{Void},Int32,Float64,Float64,Ptr{Void}),
eventloop(),req,f.handle,atime,mtime,C_NULL)
c_free(req)
Libc.free(req)
uv_error("futime", err)
f
end
Expand Down
26 changes: 0 additions & 26 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ function flush(s::IOStream)
end
iswritable(s::IOStream) = Bool(ccall(:ios_get_writable, Cint, (Ptr{Void},), s.ios))
isreadable(s::IOStream) = Bool(ccall(:ios_get_readable, Cint, (Ptr{Void},), s.ios))
modestr(s::IO) = modestr(isreadable(s), iswritable(s))
modestr(r::Bool, w::Bool) = r ? (w ? "r+" : "r") : (w ? "w" : throw(ArgumentError("neither readable nor writable")))

function truncate(s::IOStream, n::Integer)
systemerror("truncate", ccall(:ios_trunc, Int32, (Ptr{Void}, UInt), s.ios, n) != 0)
Expand Down Expand Up @@ -73,30 +71,6 @@ end

eof(s::IOStream) = ccall(:ios_eof_blocking, Int32, (Ptr{Void},), s.ios)!=0

# For interfacing with C FILE* functions


immutable CFILE
ptr::Ptr{Void}
end

function CFILE(s::IO)
@unix_only FILEp = ccall(:fdopen, Ptr{Void}, (Cint, Ptr{UInt8}), convert(Cint, fd(s)), modestr(s))
@windows_only FILEp = ccall(:_fdopen, Ptr{Void}, (Cint, Ptr{UInt8}), convert(Cint, fd(s)), modestr(s))
systemerror("fdopen", FILEp == C_NULL)
seek(CFILE(FILEp), position(s))
end

convert(::Type{CFILE}, s::IO) = CFILE(s)

function seek(h::CFILE, offset::Integer)
systemerror("fseek", ccall(:fseek, Cint, (Ptr{Void}, Clong, Cint),
h.ptr, offset, 0) != 0)
h
end

position(h::CFILE) = ccall(:ftell, Clong, (Ptr{Void},), h.ptr)

## constructing and opening streams ##

# "own" means the descriptor will be closed with the IOStream
Expand Down
Loading

0 comments on commit 67d3cd2

Please sign in to comment.