Skip to content

Commit

Permalink
rename pointer-based string and array constructors to unsafe_string, …
Browse files Browse the repository at this point in the history
…unsafe_string_wrapper, and unsafe_array_wrapper; restore non-copying behavior of String(::Vector{UInt8}) constructor (closes JuliaLang#16470, closes JuliaLang#16713)
  • Loading branch information
stevengj committed Jun 8, 2016
1 parent 11e4031 commit 77631ce
Show file tree
Hide file tree
Showing 63 changed files with 411 additions and 362 deletions.
4 changes: 2 additions & 2 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function common_prefix(completions)
for c in completions
(i > endof(c) || c[i] != cc) && return ret
end
ret *= string(cc)
ret = string(ret, cc)
i >= endof(c1) && return ret
i = nexti
cc, nexti = next(c1, i)
Expand Down Expand Up @@ -1153,7 +1153,7 @@ function enter_prefix_search(s::MIState, p::PrefixHistoryPrompt, backward::Bool)
pss = state(s, p)
pss.parent = parent
pss.histprompt.parent_prompt = parent
pss.prefix = String(pointer(buf.data), position(buf))
pss.prefix = unsafe_string(pointer(buf.data), position(buf))
copybuf!(pss.response_buffer, buf)
pss.indent = state(s, parent).indent
pss.mi = s
Expand Down
2 changes: 1 addition & 1 deletion base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ function copy_to_bitarray_chunks!(Bc::Vector{UInt64}, pos_d::Int, C::Array{Bool}
nc8 = (nc >>> 3) << 3
if nc8 > 0
ind8 = 1
C8 = reinterpret(UInt64, pointer_to_array(pointer(C, ind), nc8 << 6))
C8 = reinterpret(UInt64, unsafe_array_wrapper(pointer(C, ind), nc8 << 6))
@inbounds for i = 1:nc8
c = UInt64(0)
for j = 0:7
Expand Down
4 changes: 3 additions & 1 deletion base/c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ pointer(p::Cwstring) = convert(Ptr{Cwchar_t}, p)
==(x::Ptr, y::Union{Cstring,Cwstring}) = x == pointer(y)

# here, not in pointer.jl, to avoid bootstrapping problems in coreimg.jl
pointer_to_string(p::Cstring, own::Bool=false) = pointer_to_string(convert(Ptr{UInt8}, p), own)
unsafe_string_wrapper(p::Cstring, own::Bool=false) = unsafe_string_wrapper(convert(Ptr{UInt8}, p), own)
unsafe_string_wrapper(p::Cstring, len::Integer, own::Bool=false) = unsafe_string_wrapper(convert(Ptr{UInt8}, p), len, own)
unsafe_string(s::Cstring) = unsafe_string(convert(Ptr{UInt8}, s))

# convert strings to String etc. to pass as pointers
cconvert(::Type{Cstring}, s::AbstractString) = String(s)
Expand Down
14 changes: 7 additions & 7 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,45 +221,45 @@ function process_options(opts::JLOptions)

# startup worker
if opts.worker != C_NULL
start_worker(String(opts.worker)) # does not return
start_worker(unsafe_string(opts.worker)) # does not return
end
# add processors
if opts.nprocs > 0
addprocs(opts.nprocs)
end
# load processes from machine file
if opts.machinefile != C_NULL
addprocs(load_machine_file(String(opts.machinefile)))
addprocs(load_machine_file(unsafe_string(opts.machinefile)))
end
# load file immediately on all processors
if opts.load != C_NULL
@sync for p in procs()
@async remotecall_fetch(include, p, String(opts.load))
@async remotecall_fetch(include, p, unsafe_string(opts.load))
end
end
# eval expression
if opts.eval != C_NULL
repl = false
eval(Main, parse_input_line(String(opts.eval)))
eval(Main, parse_input_line(unsafe_string(opts.eval)))
break
end
# eval expression and show result
if opts.print != C_NULL
repl = false
show(eval(Main, parse_input_line(String(opts.print))))
show(eval(Main, parse_input_line(unsafe_string(opts.print))))
println()
break
end
# eval expression but don't disable interactive mode
if opts.postboot != C_NULL
eval(Main, parse_input_line(String(opts.postboot)))
eval(Main, parse_input_line(unsafe_string(opts.postboot)))
end
# load file
if !isempty(ARGS) && !isempty(ARGS[1])
# program
repl = false
# remove filename from ARGS
global PROGRAM_FILE = String(shift!(ARGS))
global PROGRAM_FILE = shift!(ARGS)
if !is_interactive
ccall(:jl_exit_on_sigint, Void, (Cint,), 1)
end
Expand Down
2 changes: 1 addition & 1 deletion base/dSFMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function dsfmt_get_idstring()
idstring = ccall((:dsfmt_get_idstring,:libdSFMT),
Ptr{UInt8},
())
return String(idstring)
return unsafe_string(idstring)
end

function dsfmt_get_min_array_size()
Expand Down
32 changes: 14 additions & 18 deletions base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,22 @@ readdlm(input, dlm::Char, eol::Char; opts...) =
readdlm(input, dlm::Char, T::Type, eol::Char; opts...) =
readdlm_auto(input, dlm, T, eol, false; opts...)

function readdlm_auto(input, dlm::Char, T::Type, eol::Char, auto::Bool; opts...)
readdlm_auto(input::Vector{UInt8}, dlm::Char, T::Type, eol::Char, auto::Bool; opts...) =
readdlm_string(String(input), dlm, T, eol, auto, val_opts(opts))
readdlm_auto(input::IO, dlm::Char, T::Type, eol::Char, auto::Bool; opts...) =
readdlm_string(readstring(input), dlm, T, eol, auto, val_opts(opts))
function readdlm_auto(input::AbstractString, dlm::Char, T::Type, eol::Char, auto::Bool; opts...)
optsd = val_opts(opts)
use_mmap = get(optsd, :use_mmap, is_windows() ? false : true)
if isa(input, AbstractString)
fsz = filesize(input)
if use_mmap && fsz > 0 && fsz < typemax(Int)
input = as_mmap(input, fsz)
else
input = readstring(input)
end
end
sinp = isa(input, Vector{UInt8}) ? String(input) :
isa(input, IO) ? readstring(input) :
input
readdlm_string(sinp, dlm, T, eol, auto, optsd)
end

function as_mmap(fname::AbstractString, fsz::Int64)
open(fname) do io
Mmap.mmap(io, Vector{UInt8}, (Int(fsz),))
fsz = filesize(input)
if use_mmap && fsz > 0 && fsz < typemax(Int)
a = Mmap.mmap(input, Vector{UInt8}, (Int(fsz),))
# TODO: It would be nicer to use String(a) without making a copy,
# but because the mmap'ed array is not NUL-terminated this causes
# jl_try_substrtod to segfault below.
return readdlm_string(String(copy(a)), dlm, T, eol, auto, optsd)
else
return readdlm_string(readstring(input), dlm, T, eol, auto, optsd)
end
end

Expand Down
27 changes: 16 additions & 11 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function depwarn(msg, funcsym)
opts = JLOptions()
if opts.depwarn > 0
ln = Int(unsafe_load(cglobal(:jl_lineno, Cint)))
fn = String(unsafe_load(cglobal(:jl_filename, Ptr{Cchar})))
fn = unsafe_string(unsafe_load(cglobal(:jl_filename, Ptr{Cchar})))
bt = backtrace()
caller = firstcaller(bt, funcsym)
if opts.depwarn == 1 # raise a warning
Expand Down Expand Up @@ -442,23 +442,26 @@ end
@deprecate_binding UTF8String String
@deprecate_binding ByteString String

@deprecate utf8(p::Ptr{UInt8}, len::Integer) String(p, len)
@deprecate utf8(p::Ptr{UInt8}) String(p)
@deprecate utf8(p::Ptr{UInt8}, len::Integer) unsafe_string(p, len)
@deprecate utf8(p::Ptr{UInt8}) unsafe_string(p)
@deprecate utf8(v::Vector{UInt8}) String(v)
@deprecate utf8(s::AbstractString) String(s)
@deprecate utf8(x) convert(String, x)

@deprecate ascii(p::Ptr{UInt8}, len::Integer) ascii(String(p, len))
@deprecate ascii(p::Ptr{UInt8}) ascii(String(p))
@deprecate ascii(p::Ptr{UInt8}, len::Integer) ascii(unsafe_string(p, len))
@deprecate ascii(p::Ptr{UInt8}) ascii(unsafe_string(p))
@deprecate ascii(v::Vector{UInt8}) ascii(String(v))
@deprecate ascii(x) ascii(convert(String, x))

@deprecate bytestring(s::Cstring) String(s)
@deprecate bytestring(v::Vector{UInt8}) String(v)
@deprecate bytestring(s::Cstring) unsafe_string(s)
@deprecate bytestring(v::Vector{UInt8}) String(copy(v))
@deprecate bytestring(io::Base.AbstractIOBuffer) String(io)
@deprecate bytestring(p::Union{Ptr{Int8},Ptr{UInt8}}) String(p)
@deprecate bytestring(p::Union{Ptr{Int8},Ptr{UInt8}}, len::Integer) String(p,len)
@deprecate bytestring(p::Union{Ptr{Int8},Ptr{UInt8}}) unsafe_string(p)
@deprecate bytestring(p::Union{Ptr{Int8},Ptr{UInt8}}, len::Integer) unsafe_string(p,len)
@deprecate bytestring(s::AbstractString...) string(s...)
@deprecate String(s::Cstring) unsafe_string(s)
@deprecate String(p::Union{Ptr{Int8},Ptr{UInt8}}) unsafe_string(p)
@deprecate String(p::Union{Ptr{Int8},Ptr{UInt8}}, len::Integer) unsafe_string(p,len)

@deprecate ==(x::Char, y::Integer) UInt32(x) == y
@deprecate ==(x::Integer, y::Char) x == UInt32(y)
Expand Down Expand Up @@ -700,11 +703,13 @@ hist2d(v::AbstractMatrix, n1::Integer, n2::Integer) =
hist2d(v::AbstractMatrix, n::Integer) = hist2d(v, n, n)
hist2d(v::AbstractMatrix) = hist2d(v, sturges(size(v,1)))



@deprecate cell(dims::Integer...) Array{Any}(dims...)
@deprecate cell(dims::Tuple{Vararg{Integer}}) Array{Any}(dims)

@deprecate pointer_to_array{T,N}(p::Ptr{T}, d::Union{Integer,NTuple{N,Int},NTuple{N,Integer}}, own::Bool=false) unsafe_array_wrapper(p, d, own)
@deprecate pointer_to_string(p::Ptr{UInt8}, len::Integer, own::Bool=false) unsafe_string_wrapper(p, len, own)
@deprecate pointer_to_string(p::Ptr{UInt8}, own::Bool=false) unsafe_string_wrapper(p, own)

# During the 0.5 development cycle, do not add any deprecations below this line
# To be deprecated in 0.6

Expand Down
9 changes: 0 additions & 9 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7163,15 +7163,6 @@ Letter: Uppercase, or Lt, Letter: Titlecase.
"""
isupper

"""
pointer_to_array(pointer, dims[, take_ownership::Bool])
Wrap a native pointer as a Julia Array object. The pointer element type determines the array
element type. `own` optionally specifies whether Julia should take ownership of the memory,
calling `free` on the pointer when the array is no longer referenced.
"""
pointer_to_array

"""
show(x)
Expand Down
2 changes: 1 addition & 1 deletion base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ _hasenv(s::AbstractString) = _getenv(s) != C_NULL

function access_env(onError::Function, var::AbstractString)
val = _getenv(var)
val == C_NULL ? onError(var) : String(val)
val == C_NULL ? onError(var) : unsafe_string(val)
end

function _setenv(var::AbstractString, val::AbstractString, overwrite::Bool=true)
Expand Down
5 changes: 3 additions & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,9 @@ export
disable_sigint,
pointer,
pointer_from_objref,
pointer_to_array,
pointer_to_string,
unsafe_array_wrapper,
unsafe_string_wrapper,
unsafe_string,
reenable_sigint,
unsafe_copy!,
unsafe_load,
Expand Down
4 changes: 2 additions & 2 deletions base/fft/FFTW.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export export_wisdom, import_wisdom, import_system_wisdom, forget_wisdom,
const libfftw = Base.libfftw_name
const libfftwf = Base.libfftwf_name

const version = convert(VersionNumber, split(String(cglobal((:fftw_version,Base.DFT.FFTW.libfftw), UInt8)), ['-', ' '])[2])
const version = convert(VersionNumber, split(unsafe_string(cglobal((:fftw_version,Base.DFT.FFTW.libfftw), UInt8)), ['-', ' '])[2])

## Direction of FFT

Expand Down Expand Up @@ -282,7 +282,7 @@ sprint_plan_{T<:fftwDouble}(plan::FFTWPlan{T}) =
sprint_plan_{T<:fftwSingle}(plan::FFTWPlan{T}) =
ccall((:fftwf_sprint_plan,libfftwf), Ptr{UInt8}, (PlanPtr,), plan)
function sprint_plan(plan::FFTWPlan)
pointer_to_string(sprint_plan_(plan), true)
unsafe_string_wrapper(sprint_plan_(plan), true)
end

function show{T,K,inplace}(io::IO, p::cFFTWPlan{T,K,inplace})
Expand Down
8 changes: 4 additions & 4 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function tempname()
d = get(ENV, "TMPDIR", C_NULL) # tempnam ignores TMPDIR on darwin
p = ccall(:tempnam, Cstring, (Cstring,Cstring), d, :julia)
systemerror(:tempnam, p == C_NULL)
s = String(p)
s = unsafe_string(p)
Libc.free(p)
return s
end
Expand All @@ -265,7 +265,7 @@ function mktempdir(parent=tempdir())
b = joinpath(parent, "tmpXXXXXX")
p = ccall(:mkdtemp, Cstring, (Cstring,), b)
systemerror(:mktempdir, p == C_NULL)
return String(p)
return unsafe_string(p)
end

end # os-test
Expand Down Expand Up @@ -307,7 +307,7 @@ function readdir(path::AbstractString)
entries = String[]
ent = Ref{uv_dirent_t}()
while Base.UV_EOF != ccall(:uv_fs_scandir_next, Cint, (Ptr{Void}, Ptr{uv_dirent_t}), uv_readdir_req, ent)
push!(entries, String(ent[].name))
push!(entries, unsafe_string(ent[].name))
end

# Clean up the request string
Expand Down Expand Up @@ -455,7 +455,7 @@ function readlink(path::AbstractString)
uv_error("readlink", ret)
assert(false)
end
tgt = String(ccall(:jl_uv_fs_t_ptr, Ptr{Cchar}, (Ptr{Void}, ), req))
tgt = unsafe_string(ccall(:jl_uv_fs_t_ptr, Ptr{Cchar}, (Ptr{Void}, ), req))
ccall(:uv_fs_req_cleanup, Void, (Ptr{Void}, ), req)
return tgt
finally
Expand Down
5 changes: 2 additions & 3 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ else
end
typealias CdoubleMax Union{Float16, Float32, Float64}

gmp_version() = VersionNumber(String(unsafe_load(cglobal((:__gmp_version, :libgmp), Ptr{Cchar}))))
gmp_version() = VersionNumber(unsafe_string(unsafe_load(cglobal((:__gmp_version, :libgmp), Ptr{Cchar}))))
gmp_bits_per_limb() = Int(unsafe_load(cglobal((:__gmp_bits_per_limb, :libgmp), Cint)))

const GMP_VERSION = gmp_version()
Expand Down Expand Up @@ -514,8 +514,7 @@ hex(n::BigInt) = base(16, n)
function base(b::Integer, n::BigInt)
2 <= b <= 62 || throw(ArgumentError("base must be 2 ≤ base ≤ 62, got $b"))
p = ccall((:__gmpz_get_str,:libgmp), Ptr{UInt8}, (Ptr{UInt8}, Cint, Ptr{BigInt}), C_NULL, b, &n)
len = Int(ccall(:strlen, Csize_t, (Cstring,), p))
String(pointer_to_array(p,len,true))
unsafe_string_wrapper(p, true)
end

function ndigits0z(x::BigInt, b::Integer=10)
Expand Down
2 changes: 1 addition & 1 deletion base/initdefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ end
function init_bind_addr()
opts = JLOptions()
if opts.bindto != C_NULL
bind_to = split(String(opts.bindto), ":")
bind_to = split(unsafe_string(opts.bindto), ":")
bind_addr = string(parse(IPAddr, bind_to[1]))
if length(bind_to) > 1
bind_port = parse(Int,bind_to[2])
Expand Down
2 changes: 1 addition & 1 deletion base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ elseif is_windows()
len = 0
while unsafe_load(plock, len+1) != 0; len += 1; end
# get Vector{UInt16}, transcode data to UTF-8, make a String of it
s = String(utf16to8(pointer_to_array(plock, len)))
s = String(utf16to8(unsafe_array_wrapper(plock, len)))
systemerror(:GlobalUnlock, 0==ccall((:GlobalUnlock, "kernel32"), stdcall, Cint, (Ptr{UInt16},), plock))
return s
end
Expand Down
6 changes: 3 additions & 3 deletions base/libc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function strftime(fmt::AbstractString, tm::TmStruct)
if n == 0
return ""
end
return String(pointer(timestr), n)
return unsafe_string(pointer(timestr), n)
end

"""
Expand Down Expand Up @@ -219,7 +219,7 @@ function gethostname()
ccall(:gethostname, Int32, (Ptr{UInt8}, UInt), hn, length(hn))
end
systemerror("gethostname", err != 0)
return String(pointer(hn))
return unsafe_string(pointer(hn))
end

## system error handling ##
Expand All @@ -242,7 +242,7 @@ errno(e::Integer) = ccall(:jl_set_errno, Void, (Cint,), e)
Convert a system call error code to a descriptive string
"""
strerror(e::Integer) = String(ccall(:strerror, Cstring, (Int32,), e))
strerror(e::Integer) = unsafe_string(ccall(:strerror, Cstring, (Int32,), e))
strerror() = strerror(errno())

"""
Expand Down
6 changes: 3 additions & 3 deletions base/libdl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ find_library(libname::Union{Symbol,AbstractString}, extrapaths=String[]) =

function dlpath(handle::Ptr{Void})
p = ccall(:jl_pathname_for_handle, Cstring, (Ptr{Void},), handle)
s = String(p)
s = unsafe_string(p)
is_windows() && Libc.free(p)
return s
end
Expand Down Expand Up @@ -190,7 +190,7 @@ if is_linux()
# This callback function called by dl_iterate_phdr() on Linux
function dl_phdr_info_callback(di::dl_phdr_info, size::Csize_t, dynamic_libraries::Array{AbstractString,1})
# Skip over objects without a path (as they represent this own object)
name = String(di.name)
name = unsafe_string(di.name)
if !isempty(name)
push!(dynamic_libraries, name)
end
Expand All @@ -212,7 +212,7 @@ function dllist()

# start at 1 instead of 0 to skip self
for i in 1:numImages-1
name = String(ccall(:_dyld_get_image_name, Cstring, (UInt32,), i))
name = unsafe_string(ccall(:_dyld_get_image_name, Cstring, (UInt32,), i))
push!(dynamic_libraries, name)
end
end
Expand Down
Loading

0 comments on commit 77631ce

Please sign in to comment.