Skip to content

Commit

Permalink
start to remove pointer(Array)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Apr 24, 2014
1 parent 6dfc787 commit b75525a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function next(::EnvHash, i)
if env == nothing
error(BoundsError)
end
env::ByteString
env = env::ByteString
m = match(r"^(.*?)=(.*)$"s, env)
if m == nothing
error("malformed environment entry: $env")
Expand Down
16 changes: 8 additions & 8 deletions base/pointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ 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)
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{T}(::Type{Ptr{T}}, a::Array{T}) = ccall(:jl_array_ptr, Ptr{T}, (Any,), a)
convert(::Type{Ptr{None}}, a::Array) = ccall(:jl_array_ptr, Ptr{None}, (Any,), a)
cconvert{T}(::Type{Ptr{T}}, a::Array{T}) = ccall(:jl_array_ptr, Ptr{T}, (Any,), a)
cconvert(::Type{Ptr{None}}, a::Array) = ccall(:jl_array_ptr, Ptr{None}, (Any,), a)

pointer{T}(::Type{T}, x::Uint) = convert(Ptr{T}, x)
pointer{T}(::Type{T}, x::Ptr) = convert(Ptr{T}, x)
# note: these definitions don't mean any AbstractArray is convertible to
# pointer. 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)*sizeof(T)
pointer{T}(x::AbstractArray{T}) = cconvert(Ptr{T},x)
pointer{T}(x::AbstractArray{T}, i::Integer) = cconvert(Ptr{T},x)+(i-1)*sizeof(T)

# unsafe pointer to array conversions
pointer_to_array(p, dims::Dims) = pointer_to_array(p, dims, false)
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
show(x) = show(STDOUT::IO, x)

function print(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

3 comments on commit b75525a

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To what end?

@JeffBezanson
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cconvert is supposed to be a hidden thing only used internally by ccall. Why in the world would you want to call cconvert instead of convert? Telling people "to get a pointer, use cconvert instead of convert" doesn't make things safer, just more arbitrary and annoying. This won't stop people from doing it; they'll just have an extra arbitrary thing to memorize.

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rather agree with that. The approach here has to be to discourage it, but make it as safe and usable when necessary.

Please sign in to comment.