Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: create Libc and Libdl modules #10328

Merged
merged 3 commits into from
Mar 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ Deprecated or removed
`Array{T}(x)`, `map(T,x)`, or `round(T,x)`. To parse a string as an integer
or floating-point number, use `parseint` or `parsefloat` ([#1470], [#6211]).

* Low-level functions from the C library and dynamic linker have been moved to
modules `Libc` and `Libdl`, respectively ([#10328]).

Julia v0.3.0 Release Notes
==========================

Expand Down
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
2 changes: 1 addition & 1 deletion base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type SystemError <: Exception
prefix::AbstractString
errnum::Int32
SystemError(p::AbstractString, e::Integer) = new(p, e)
SystemError(p::AbstractString) = new(p, errno())
SystemError(p::AbstractString) = new(p, Libc.errno())
end

type TypeError <: Exception
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
20 changes: 18 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 Expand Up @@ -423,6 +421,24 @@ end
@deprecate flipud(A::AbstractArray) flipdim(A, 1)
@deprecate fliplr(A::AbstractArray) flipdim(A, 2)

@deprecate strftime Libc.strftime
@deprecate strptime Libc.strptime
@deprecate flush_cstdio Libc.flush_cstdio
@deprecate mmap Libc.mmap
@deprecate c_free Libc.free
@deprecate c_malloc Libc.malloc
@deprecate c_calloc Libc.calloc
@deprecate c_realloc Libc.realloc
@deprecate errno Libc.errno
@deprecate strerror Libc.strerror

@deprecate dlclose Libdl.dlclose
@deprecate dlopen Libdl.dlopen
@deprecate dlopen_e Libdl.dlopen_e
@deprecate dlsym Libdl.dlsym
@deprecate dlsym_e Libdl.dlsym_e
@deprecate find_library Libdl.find_library

# 0.4 discontinued functions

@noinline function subtypetree(x::DataType, level=-1)
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 @@ -33,10 +33,6 @@ kwerr(kw) = error("unrecognized keyword argument \"", kw, "\"")

## 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
32 changes: 2 additions & 30 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 @@ -1022,8 +1010,6 @@ export

# time
sleep,
strftime,
strptime,
tic,
time,
time_ns,
Expand Down Expand Up @@ -1132,7 +1118,6 @@ export
fd,
fdio,
flush,
flush_cstdio,
getaddrinfo,
gethostname,
getipaddr,
Expand All @@ -1145,7 +1130,6 @@ export
listenany,
ltoh,
mark,
mmap,
mmap_array,
mmap_bitarray,
msync,
Expand Down Expand Up @@ -1317,25 +1301,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
4 changes: 2 additions & 2 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,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 Expand Up @@ -164,7 +164,7 @@ function mktempdir()
if ret == 0
return filename
end
systemerror(:mktempdir, errno()!=EEXIST)
systemerror(:mktempdir, Libc.errno()!=EEXIST)
seed += 1
end
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