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

minor cleanup to MemoryRef #51937

Merged
merged 3 commits into from
Oct 31, 2023
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
2 changes: 2 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,8 @@ the other elements are left untouched.

See also [`copy!`](@ref Base.copy!), [`copy`](@ref).

$(_DOCS_ALIASING_WARNING)

# Examples
```jldoctest
julia> x = [1., 0., 3., 0., 5.];
Expand Down
20 changes: 10 additions & 10 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,6 @@ source and `do` in the destination (1-indexed).
The `unsafe` prefix on this function indicates that no validation is performed to ensure
that N is inbounds on either array. Incorrect usage may corrupt or segfault your program, in
the same manner as C.

$(_DOCS_ALIASING_WARNING)
"""
function unsafe_copyto!(dest::Array, doffs, src::Array, soffs, n)
n == 0 && return dest
Expand All @@ -291,24 +289,26 @@ end
Copy `N` elements from collection `src` starting at the linear index `so`, to array `dest` starting at
the index `do`. Return `dest`.
"""
function copyto!(dest::Array, doffs::Integer, src::Array, soffs::Integer, n::Integer)
return _copyto_impl!(dest, doffs, src, soffs, n)
end
copyto!(dest::Array, doffs::Integer, src::Array, soffs::Integer, n::Integer) = _copyto_impl!(dest, doffs, src, soffs, n)
copyto!(dest::Array, doffs::Integer, src::Memory, soffs::Integer, n::Integer) = _copyto_impl!(dest, doffs, src, soffs, n)
copyto!(dest::Memory, doffs::Integer, src::Array, soffs::Integer, n::Integer) = _copyto_impl!(dest, doffs, src, soffs, n)

# this is only needed to avoid possible ambiguities with methods added in some packages
function copyto!(dest::Array{T}, doffs::Integer, src::Array{T}, soffs::Integer, n::Integer) where T
return _copyto_impl!(dest, doffs, src, soffs, n)
end
copyto!(dest::Array{T}, doffs::Integer, src::Array{T}, soffs::Integer, n::Integer) where {T} = _copyto_impl!(dest, doffs, src, soffs, n)

function _copyto_impl!(dest::Array, doffs::Integer, src::Array, soffs::Integer, n::Integer)
function _copyto_impl!(dest::Union{Array,Memory}, doffs::Integer, src::Union{Array,Memory}, soffs::Integer, n::Integer)
n == 0 && return dest
n > 0 || _throw_argerror("Number of elements to copy must be non-negative.")
@boundscheck checkbounds(dest, doffs:doffs+n-1)
@boundscheck checkbounds(src, soffs:soffs+n-1)
unsafe_copyto!(dest, doffs, src, soffs, n)
@inbounds let dest = GenericMemoryRef(dest isa Array ? getfield(dest, :ref) : dest, doffs)
src = GenericMemoryRef(src isa Array ? getfield(src, :ref) : src, soffs)
unsafe_copyto!(dest, src, n)
end
return dest
end


# Outlining this because otherwise a catastrophic inference slowdown
# occurs, see discussion in #27874.
# It is also mitigated by using a constant string.
Expand Down
9 changes: 8 additions & 1 deletion base/genericmemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ end
@eval isassigned(a::GenericMemoryRef) = memoryref_isassigned(a, :not_atomic, $(Expr(:boundscheck)))

## copy ##
@eval function unsafe_copyto!(dest::MemoryRef{T}, src::MemoryRef{T}, n) where {T}
function unsafe_copyto!(dest::MemoryRef{T}, src::MemoryRef{T}, n) where {T}
@_terminates_globally_meta
n == 0 && return dest
@boundscheck GenericMemoryRef(dest, n), GenericMemoryRef(src, n)
Expand Down Expand Up @@ -118,6 +118,13 @@ end

copy(a::T) where {T<:Memory} = ccall(:jl_genericmemory_copy, Ref{T}, (Any,), a)

function copyto!(dest::Memory, doffs::Integer, src::Memory, soffs::Integer, n::Integer)
n < 0 && _throw_argerror("Number of elements to copy must be non-negative.")
unsafe_copyto!(dest, doffs, src, soffs, n)
return dest
end


## Constructors ##

similar(a::Memory{T}) where {T} = Memory{T}(undef, length(a))
Expand Down
4 changes: 2 additions & 2 deletions doc/src/manual/embedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,14 @@ object has just been allocated and no garbage collection has run since then. Not
`jl_...` functions can sometimes invoke garbage collection.

The write barrier is also necessary for arrays of pointers when updating their data directly.
For example:
Calling `jl_array_ptr_set` is usually much preferred. But direct updates can be done. For example:

```c
jl_array_t *some_array = ...; // e.g. a Vector{Any}
void **data = jl_array_data(some_array, void*);
jl_value_t *some_value = ...;
data[0] = some_value;
jl_gc_wb(some_array, some_value);
jl_gc_wb(jl_array_owner(some_array), some_value);
```

### Controlling the Garbage Collector
Expand Down
22 changes: 11 additions & 11 deletions stdlib/Base64/src/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@

# Data buffer for pipes.
mutable struct Buffer
data::Memory{UInt8}
ptr::Ptr{UInt8}
const data::Memory{UInt8}
offset::Int
size::Int

function Buffer(bufsize)
data = Memory{UInt8}(undef, bufsize)
return new(data, pointer(data), 0)
return new(data, 0, 0)
end
end

Base.empty!(buffer::Buffer) = buffer.size = 0
Base.getindex(buffer::Buffer, i::Integer) = unsafe_load(buffer.ptr, i)
Base.setindex!(buffer::Buffer, v::UInt8, i::Integer) = unsafe_store!(buffer.ptr, v, i)
Base.getindex(buffer::Buffer, i::Integer) = buffer.data[buffer.offset + i]
Base.setindex!(buffer::Buffer, v::UInt8, i::Integer) = buffer.data[buffer.offset + i] = v
Base.firstindex(buffer::Buffer) = 1
Base.lastindex(buffer::Buffer) = buffer.size
Base.pointer(buffer::Buffer) = buffer.ptr
capacity(buffer::Buffer) = Int(pointer(buffer.data, lastindex(buffer.data)) - buffer.ptr) + 1
Base.pointer(buffer::Buffer) = pointer(buffer.data) + buffer.offset
capacity(buffer::Buffer) = length(buffer.data) - buffer.offset

function consumed!(buffer::Buffer, n::Integer)
@assert n ≤ buffer.size
buffer.ptr += n
buffer.offset += n
buffer.size -= n
end

function read_to_buffer(io::IO, buffer::Buffer)
offset = buffer.ptr - pointer(buffer.data)
offset = buffer.offset
copyto!(buffer.data, 1, buffer.data, offset + 1, buffer.size)
buffer.ptr = pointer(buffer.data)
buffer.offset = 0
if !eof(io)
n = min(bytesavailable(io), capacity(buffer) - buffer.size)
unsafe_read(io, buffer.ptr + buffer.size, n)
unsafe_read(io, pointer(buffer) + buffer.size, n)
buffer.size += n
end
return
Expand Down