Skip to content

Commit

Permalink
remove unused Ref subtypes. disallow implicit convert to Ptr{T} (ref #…
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Feb 16, 2015
1 parent de481a4 commit 0904f36
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 119 deletions.
11 changes: 0 additions & 11 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ else
end
end

# convert Arrays to pointer arrays for ccall
cconvert_gcroot{P<:Ptr}(::Type{Ptr{P}}, a::Array{P}) = a
function cconvert_gcroot{P<:Ptr}(::Type{Ptr{P}}, a::Array)
ptrs = Array(P, length(a)+1)
for i = 1:length(a)
ptrs[i] = convert(P, a[i])
end
ptrs[length(a)+1] = C_NULL
return ptrs
end

size{T,n}(t::AbstractArray{T,n}, d) = d <= n ? size(t)[d] : 1
size(x, d1::Integer, d2::Integer, dx::Integer...) = tuple(size(x, d1), size(x, d2, dx...)...)
eltype{T,n}(::Type{AbstractArray{T,n}}) = T
Expand Down
11 changes: 11 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ call{T}(::Type{Matrix{T}}, m::Integer, n::Integer) = Array{T}(m, n)

## Basic functions ##

# convert Arrays to pointer arrays for ccall
cconvert_gcroot{P<:Ptr}(::Type{Ptr{P}}, a::Array{P}) = a
function cconvert_gcroot{P<:Ptr}(::Type{Ptr{P}}, a::Array)
ptrs = Array(P, length(a)+1)
for i = 1:length(a)
ptrs[i] = cconvert(P, a[i])
end
ptrs[length(a)+1] = C_NULL
return ptrs
end

size(a::Array) = arraysize(a)
size(a::Array, d) = arraysize(a, d)
size(a::Matrix) = (arraysize(a,1), arraysize(a,2))
Expand Down
2 changes: 1 addition & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function Base.parseint_nocheck(::Type{BigInt}, s::AbstractString, base::Int)
z = BigInt()
err = ccall((:__gmpz_set_str, :libgmp),
Int32, (Ptr{BigInt}, Ptr{UInt8}, Int32),
&z, convert(Ptr{UInt8},SubString(s,i)), base)
&z, SubString(s,i), base)
err == 0 || throw(ArgumentError("invalid BigInt: $(repr(s))"))
return sgn < 0 ? -z : z
end
Expand Down
2 changes: 1 addition & 1 deletion base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,7 @@ function inlineable(f::ANY, e::Expr, atypes::Tuple, sv::StaticVarInfo, enclosing
if incompletematch
cost *= 4
end
if is(f, next) || is(f, done)
if is(f, next) || is(f, done) || is(f, cconvert) || is(f, cconvert_gcroot)
cost /= 4
end
if !inline_worthy(body, cost)
Expand Down
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function write(s::IO, p::Ptr, n::Integer)
end

function write(io::IO, s::Symbol)
pname = convert(Ptr{UInt8}, s)
pname = cconvert(Ptr{UInt8}, s)
write(io, pname, int(ccall(:strlen, Csize_t, (Ptr{UInt8},), pname)))
end

Expand Down
2 changes: 1 addition & 1 deletion base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function IOStream(name::AbstractString, finalize::Bool)
end
IOStream(name::AbstractString) = IOStream(name, true)

convert(T::Type{Ptr{Void}}, s::IOStream) = convert(T, s.ios)
convert(T::Type{Ptr{Void}}, s::IOStream) = convert(T, pointer(s.ios))
show(io::IO, s::IOStream) = print(io, "IOStream(", s.name, ")")
fd(s::IOStream) = int(ccall(:jl_ios_fd, Clong, (Ptr{Void},), s.ios))
stat(s::IOStream) = stat(fd(s))
Expand Down
14 changes: 7 additions & 7 deletions base/libc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type TmStruct
t = floor(t)
tm = TmStruct()
# TODO: add support for UTC via gmtime_r()
ccall(:localtime_r, Ptr{Void}, (Ptr{Int}, Ptr{Void}), &t, &tm)
ccall(:localtime_r, Ptr{TmStruct}, (Ptr{Int}, Ptr{TmStruct}), &t, &tm)
return tm
end
end
Expand All @@ -37,18 +37,18 @@ strftime(t) = strftime("%c", t)
strftime(fmt::AbstractString, t::Real) = strftime(fmt, TmStruct(t))
function strftime(fmt::AbstractString, tm::TmStruct)
timestr = Array(UInt8, 128)
n = ccall(:strftime, Int, (Ptr{UInt8}, Int, Ptr{UInt8}, Ptr{Void}),
n = ccall(:strftime, Int, (Ptr{UInt8}, Int, Ptr{UInt8}, Ptr{TmStruct}),
timestr, length(timestr), fmt, &tm)
if n == 0
return ""
end
bytestring(convert(Ptr{UInt8},timestr))
bytestring(pointer(timestr), n)
end

strptime(timestr::AbstractString) = strptime("%c", timestr)
function strptime(fmt::AbstractString, timestr::AbstractString)
tm = TmStruct()
r = ccall(:strptime, Ptr{UInt8}, (Ptr{UInt8}, Ptr{UInt8}, Ptr{Void}),
r = ccall(:strptime, Ptr{UInt8}, (Ptr{UInt8}, Ptr{UInt8}, Ptr{TmStruct}),
timestr, fmt, &tm)
# the following would tell mktime() that this is a local time, and that
# it should try to guess the timezone. not sure if/how this should be
Expand All @@ -62,13 +62,13 @@ function strptime(fmt::AbstractString, timestr::AbstractString)
# if we didn't explicitly parse the weekday or year day, use mktime
# to fill them in automatically.
if !ismatch(r"([^%]|^)%(a|A|j|w|Ow)", fmt)
ccall(:mktime, Int, (Ptr{Void},), &tm)
ccall(:mktime, Int, (Ptr{TmStruct},), &tm)
end
end
tm
end

time(tm::TmStruct) = float64(ccall(:mktime, Int, (Ptr{Void},), &tm))
time(tm::TmStruct) = float64(ccall(:mktime, Int, (Ptr{TmStruct},), &tm))

## process-related functions ##

Expand All @@ -81,7 +81,7 @@ function gethostname()
@unix_only err=ccall(:gethostname, Int32, (Ptr{UInt8}, UInt), hn, length(hn))
@windows_only err=ccall(:gethostname, stdcall, Int32, (Ptr{UInt8}, UInt32), hn, length(hn))
systemerror("gethostname", err != 0)
bytestring(convert(Ptr{UInt8},hn))
bytestring(pointer(hn))
end

## Memory related ##
Expand Down
14 changes: 7 additions & 7 deletions base/pointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ convert{T}(::Type{Ptr{T}}, x::Signed) = box(Ptr{T},unbox(Int,Int(x)))
convert{T}(::Type{Ptr{T}}, p::Ptr{T}) = p
convert{T}(::Type{Ptr{T}}, p::Ptr) = box(Ptr{T}, unbox(Ptr,p))

# object to pointer
convert(::Type{Ptr{UInt8}}, x::Symbol) = ccall(:jl_symbol_name, Ptr{UInt8}, (Any,), x)
convert(::Type{Ptr{Int8}}, x::Symbol) = ccall(:jl_symbol_name, Ptr{Int8}, (Any,), x)
convert(::Type{Ptr{UInt8}}, s::ByteString) = convert(Ptr{UInt8}, s.data)
convert(::Type{Ptr{Int8}}, s::ByteString) = convert(Ptr{Int8}, s.data)
# object to pointer (when used with ccall)
cconvert(::Type{Ptr{UInt8}}, x::Symbol) = ccall(:jl_symbol_name, Ptr{UInt8}, (Any,), x)
cconvert(::Type{Ptr{Int8}}, x::Symbol) = ccall(:jl_symbol_name, Ptr{Int8}, (Any,), x)
cconvert(::Type{Ptr{UInt8}}, s::ByteString) = cconvert(Ptr{UInt8}, s.data)
cconvert(::Type{Ptr{Int8}}, s::ByteString) = cconvert(Ptr{Int8}, s.data)
# convert strings to ByteString to pass as pointers
cconvert_gcroot(::Type{Ptr{UInt8}}, s::AbstractString) = bytestring(s)
cconvert_gcroot(::Type{Ptr{Int8}}, s::AbstractString) = bytestring(s)

convert{T}(::Type{Ptr{T}}, a::Array{T}) = ccall(:jl_array_ptr, Ptr{T}, (Any,), a)
convert(::Type{Ptr{Void}}, a::Array) = ccall(:jl_array_ptr, Ptr{Void}, (Any,), a)
cconvert{T}(::Type{Ptr{T}}, a::Array{T}) = ccall(:jl_array_ptr, Ptr{T}, (Any,), a)
cconvert(::Type{Ptr{Void}}, a::Array) = ccall(:jl_array_ptr, Ptr{Void}, (Any,), a)

# unsafe pointer to array conversions
pointer_to_array(p, d::Integer, own=false) = pointer_to_array(p, (d,), own)
Expand Down
20 changes: 2 additions & 18 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ type ProcessChain
end
typealias ProcessChainOrNot Union(Bool,ProcessChain)

function _jl_spawn(cmd::Ptr{UInt8}, argv::Ptr{Ptr{UInt8}}, loop::Ptr{Void}, pp::Process,
function _jl_spawn(cmd, argv, loop::Ptr{Void}, pp::Process,
in, out, err)
proc = c_malloc(_sizeof_uv_process)
error = ccall(:jl_spawn, Int32,
Expand Down Expand Up @@ -341,10 +341,9 @@ function spawn(pc::ProcessChainOrNot, cmd::Cmd, stdios::StdIOSet, exitcb::Callba
loop = eventloop()
pp = Process(cmd, C_NULL, stdios[1], stdios[2], stdios[3]);
@setup_stdio
ptrs = _jl_pre_exec(cmd.exec)
pp.exitcb = exitcb
pp.closecb = closecb
pp.handle = _jl_spawn(ptrs[1], convert(Ptr{Ptr{UInt8}}, ptrs), loop, pp,
pp.handle = _jl_spawn(cmd.exec[1], cmd.exec, loop, pp,
in, out, err)
@cleanup_stdio
if isa(pc, ProcessChain)
Expand Down Expand Up @@ -559,21 +558,6 @@ function process_status(s::Process)
error("process status error")
end

# WARNING: do not call this and keep the returned array of pointers
# around longer than the args vector and then use array of pointers.
# this could cause a segfault. this is really just for use by the
# spawn function below so that we can exec more efficiently.
#
function _jl_pre_exec(args::Vector{ByteString})
isempty(args) && throw(ArgumentError("exec called with no arguments"))
ptrs = Array(Ptr{UInt8}, length(args)+1)
for i = 1:length(args)
ptrs[i] = args[i].data
end
ptrs[length(args)+1] = C_NULL
return ptrs
end

## implementation of `cmd` syntax ##

arg_gen() = ByteString[]
Expand Down
72 changes: 12 additions & 60 deletions base/refpointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Base.eltype{T}(x::Type{Ref{T}}) = T
Base.convert{T}(::Type{Ref{T}}, x::Ref{T}) = x

# create Ref objects for general object conversion
@inline Base.cconvert_gcroot{T}(::Type{Ref{T}}, x) = convert(Ref{T}, x)
@inline Base.cconvert{T}(::Type{Ref{T}}, x) = convert(Ptr{T}, x)
Base.cconvert_gcroot{T}(::Type{Ref{T}}, x) = convert(Ref{T}, x)
Base.cconvert{T}(::Type{Ref{T}}, x) = cconvert(Ptr{T}, x)

### Methods for a Ref object that can store a single value
### Methods for a Ref object that can store a single value of any type

type RefValue{T} <: Ref{T}
x::T
Expand All @@ -22,100 +22,52 @@ Ref{T}(x::T) = RefValue{T}(x)
Ref{T}(x::Ptr{T}, i::Integer=1) = x + (i-1)*Core.sizeof(T)
Ref(x, i::Integer) = (i != 1 && error("Object only has one element"); Ref(x))

@inline function Base.convert{T}(P::Type{Ptr{T}}, b::RefValue{T})
function Base.cconvert{T}(P::Type{Ptr{T}}, b::RefValue{T})
if isbits(T)
return convert(P, data_pointer_from_objref(b))
else
return convert(P, data_pointer_from_objref(b.x))
end
end
@inline function Base.convert(P::Type{Ptr{Any}}, b::RefValue{Any})
function Base.cconvert(P::Type{Ptr{Any}}, b::RefValue{Any})
return convert(P, data_pointer_from_objref(b))
end
@inline Base.convert{T}(::Type{Ptr{Void}}, b::RefValue{T}) = Base.convert(Ptr{Void}, Base.convert(Ptr{T}, b))
Base.cconvert{T}(::Type{Ptr{Void}}, b::RefValue{T}) = Base.convert(Ptr{Void}, Base.cconvert(Ptr{T}, b))

### Methods for a Ref object that is backed by an array at index i

# note: the following type definitions don't mean any AbstractArray is convertible to
# a data Ref. they just map the array element type to the pointer type for
# convenience in cases that work.
pointer{T}(x::AbstractArray{T}) = convert(Ptr{T},x)
pointer{T}(x::AbstractArray{T}, i::Integer) = convert(Ptr{T},x) + (i-1)*elsize(x)
pointer{T}(x::AbstractArray{T}) = cconvert(Ptr{T}, x)
pointer{T}(x::AbstractArray{T}, i::Integer) = cconvert(Ptr{T},x) + (i-1)*elsize(x)

immutable RefArray{T, A<:AbstractArray} <: Ref{T}
x::A
i::Int
RefArray(x,i) = (@assert(eltype(A) == T); new(x,i))
end
convert{T}(::Type{Ref{T}}, x::AbstractArray{T}) = RefArray{T,typeof(x)}(x, 1)
Base.convert{T}(::Type{Ref{T}}, x::AbstractArray{T}) = RefArray{T,typeof(x)}(x, 1)
Ref{T}(x::AbstractArray{T}, i::Integer=1) = RefArray{T,typeof(x)}(x, i)

@inline function Base.convert{T}(P::Type{Ptr{T}}, b::RefArray{T})
function Base.cconvert{T}(P::Type{Ptr{T}}, b::RefArray{T})
if isbits(T)
convert(P, pointer(b.x, b.i))
else
convert(P, data_pointer_from_objref(b.x[b.i]))
end
end
@inline function Base.convert(P::Type{Ptr{Any}}, b::RefArray{Any})
function Base.cconvert(P::Type{Ptr{Any}}, b::RefArray{Any})
return convert(P, pointer(b.x, b.i))
end
@inline Base.convert{T}(::Type{Ptr{Void}}, b::RefArray{T}) = Base.convert(Ptr{Void}, Base.convert(Ptr{T}, b))

### Methods for a Ref object that is backed by an array at index (i...)

immutable RefArrayND{T, A<:AbstractArray} <: Ref{T}
x::A
i::(Int...)
RefArrayND(x,i) = (@assert(eltype(A) == T); new(x,i))
end
Ref{A<:AbstractArray}(x::A, i::Integer...) = RefArrayND{eltype(A),A}(x, i)
Ref{A<:AbstractArray}(x::A, i::(Integer...)) = RefArrayND{eltype(A),A}(x, i)

@inline function Base.convert{T}(P::Type{Ptr{T}}, b::RefArrayND{T})
if isbits(T)
convert(P, pointer(b.x, b.i...))
else
convert(P, data_pointer_from_objref(b.x[b.i...]))
end
end
@inline function Base.convert(P::Type{Ptr{Any}}, b::RefArrayND{Any})
return convert(P, pointer(b.x, b.i...))
end
@inline Base.convert{T}(::Type{Ptr{Void}}, b::RefArrayND{T}) = Base.convert(Ptr{Void}, Base.convert(Ptr{T}, b))

### Methods for a Ref object that is backed by an array at index (Any...)

immutable RefArrayI{T} <: Ref{T}
x::AbstractArray{T}
i::Tuple
RefArrayI(x,i::ANY) = (@assert(eltype(A) == T); new(x,i))
end
Ref{T}(x::AbstractArray{T}, i...) = RefArrayI{T}(x, i)
Ref{T}(x::AbstractArray{T}, i::Tuple) = RefArrayI{T}(x, i)

@inline function Base.convert{T}(P::Type{Ptr{T}}, b::RefArrayI{T})
if isbits(T)
convert(P, pointer(b.x, b.i...))
else
convert(P, data_pointer_from_objref(b.x[b.i...]))
end
end
@inline function Base.convert(P::Type{Ptr{Any}}, b::RefArrayI{Any})
return convert(P, pointer(b.x, b.i...))
end
@inline Base.convert{T}(::Type{Ptr{Void}}, b::RefArrayI{T}) = Base.convert(Ptr{Void}, Base.convert(Ptr{T}, b))
Base.cconvert{T}(::Type{Ptr{Void}}, b::RefArray{T}) = Base.convert(Ptr{Void}, Base.cconvert(Ptr{T}, b))

###

Base.getindex(b::RefValue) = b.x
Base.getindex(b::RefArray) = b.x[b.i]
Base.getindex(b::RefArrayND) = b.x[b.i...]
Base.getindex(b::RefArrayI) = b.x[b.i...]

Base.setindex!(b::RefValue, x) = (b.x = x; b)
Base.setindex!(b::RefArray, x) = (b.x[b.i] = x; b)
Base.setindex!(b::RefArrayND, x) = (b.x[b.i...] = x; b)
Base.setindex!(b::RefArrayI, x) = (b.x[b.i...] = x; b)

###
2 changes: 1 addition & 1 deletion base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function serialize(s, x::Symbol)
if haskey(ser_tag, x)
return write_as_tag(s, x)
end
pname = convert(Ptr{UInt8}, x)
pname = cconvert(Ptr{UInt8}, x)
ln = int(ccall(:strlen, Csize_t, (Ptr{UInt8},), pname))
if ln <= 255
writetag(s, Symbol)
Expand Down
2 changes: 1 addition & 1 deletion base/sharedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ sdata(A::AbstractArray) = A

localindexes(S::SharedArray) = S.pidx > 0 ? range_1dim(S, S.pidx) : 1:0

convert{T}(::Type{Ptr{T}}, S::SharedArray) = convert(Ptr{T}, sdata(S))
cconvert{T}(::Type{Ptr{T}}, S::SharedArray) = cconvert(Ptr{T}, sdata(S))

convert(::Type{SharedArray}, A::Array) = (S = SharedArray(eltype(A), size(A)); copy!(S, A))
convert{T}(::Type{SharedArray{T}}, A::Array) = (S = SharedArray(T, size(A)); copy!(S, A))
Expand Down
2 changes: 1 addition & 1 deletion base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ function getindex(s::AbstractString, r::UnitRange{Int})
SubString(s, first(r), last(r))
end

function convert{P<:Union(Int8,UInt8),T<:ByteString}(::Type{Ptr{P}}, s::SubString{T})
function cconvert{P<:Union(Int8,UInt8),T<:ByteString}(::Type{Ptr{P}}, s::SubString{T})
if s.offset+s.endof < endof(s.string)
throw(ArgumentError("a SubString must coincide with the end of the original string to be convertible to pointer"))
end
Expand Down
6 changes: 3 additions & 3 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,11 @@ function subarray_linearindexing_dim{A<:AbstractArray}(::Type{A}, It::Tuple)
LD
end

convert{T,N,P<:Array,I<:(RangeIndex...)}(::Type{Ptr{T}}, V::SubArray{T,N,P,I}) =
cconvert{T,N,P<:Array,I<:(RangeIndex...)}(::Type{Ptr{T}}, V::SubArray{T,N,P,I}) =
pointer(V.parent) + (V.first_index-1)*sizeof(T)

convert{T,N,P<:Array,I<:(RangeIndex...)}(::Type{Ptr{Void}}, V::SubArray{T,N,P,I}) =
convert(Ptr{Void}, convert(Ptr{T}, V))
cconvert{T,N,P<:Array,I<:(RangeIndex...)}(::Type{Ptr{Void}}, V::SubArray{T,N,P,I}) =
convert(Ptr{Void}, cconvert(Ptr{T}, V))

pointer(V::SubArray, i::Int) = pointer(V, ind2sub(size(V), i))

Expand Down
2 changes: 1 addition & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ include("number.jl")
include("int.jl")
include("operators.jl")
include("pointer.jl")
include("refpointer.jl")

# rounding utilities
include("rounding.jl")
Expand All @@ -66,7 +67,6 @@ include("dict.jl")
include("set.jl")
include("hashing.jl")
include("iterator.jl")
include("refpointer.jl") #XXX: move this earlier when we don't need @inline annotations anymore

# SIMD loops
include("simdloop.jl")
Expand Down
2 changes: 1 addition & 1 deletion base/utf16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ convert(::Type{UTF8String}, s::UTF16String) =
sprint(length(s.data)-1, io->for c in s; write(io,c::Char); end)

sizeof(s::UTF16String) = sizeof(s.data) - sizeof(UInt16)
convert{T<:Union(Int16,UInt16)}(::Type{Ptr{T}}, s::UTF16String) =
cconvert{T<:Union(Int16,UInt16)}(::Type{Ptr{T}}, s::UTF16String) =
convert(Ptr{T}, pointer(s))

function is_valid_utf16(data::AbstractArray{UInt16})
Expand Down
2 changes: 1 addition & 1 deletion base/utf32.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ convert(::Type{Array{Char}}, s::UTF32String) = s.data
reverse(s::UTF32String) = UTF32String(reverse!(copy(s.data), 1, length(s)))

sizeof(s::UTF32String) = sizeof(s.data) - sizeof(Char)
convert{T<:Union(Int32,UInt32,Char)}(::Type{Ptr{T}}, s::UTF32String) =
cconvert{T<:Union(Int32,UInt32,Char)}(::Type{Ptr{T}}, s::UTF32String) =
convert(Ptr{T}, pointer(s))

function convert(T::Type{UTF32String}, bytes::AbstractArray{UInt8})
Expand Down
Loading

0 comments on commit 0904f36

Please sign in to comment.