Skip to content
Merged
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
36 changes: 19 additions & 17 deletions src/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,28 @@ function Base.iterate(x::MemoryView, i::Int = 1)
return (@inbounds x[i], i + 1)
end

# Note: For zero-sized elements, this always returns 1:x.len, which may not be
# the correct indices. However, the result is indistinguishable from the "correct"
# result, so it doesn't matter
function Base.parentindices(x::MemoryView)
elz = Base.elsize(x)
return if iszero(elz)
(1:(x.len),)
else
byte_offset = pointer(x.ref) - pointer(x.ref.mem)
elem_offset = div(byte_offset % UInt, elz % UInt) % Int
((elem_offset + 1):(elem_offset + x.len),)
# Base.memoryindex exists in Julia 1.13 onwards.
@static if VERSION < v"1.13"
# Note: For zero-sized elements, this always returns 1:x.len, which may not be
# the correct indices. However, the result is indistinguishable from the "correct"
# result, so it doesn't matter
function Base.parentindices(x::MemoryView)
elz = Base.elsize(x)
return if iszero(elz)
(1:(x.len),)
else
byte_offset = pointer(x.ref) - pointer(x.ref.mem)
elem_offset = div(byte_offset % UInt, elz % UInt) % Int
((elem_offset + 1):(elem_offset + x.len),)
end
end
else
function Base.parentindices(x::MemoryView)
start = Base.memoryindex(x.ref)
return (start:(start + length(x) - 1),)
end
end

# TODO: If `Core.memoryrefoffset` is made public, use this impl instead
# function Base.parentindices(x::MemoryView)
# start = Core.memoryrefoffset(x.ref)
# return (start:(start + length(x) - 1),)
# end

function Base.copy(x::MemoryView)
isempty(x) && return x
newmem = @inbounds x.ref.mem[only(parentindices(x))]
Expand Down
Loading