function unsafe_read2!(
dest::Ptr{T},
src::AbstractVector{T},
so::Integer,
nbytes::Integer) where T
for i in 1:nbytes
unsafe_store!(dest, src[so+i-1], i)
end
end
It seems I don’t understand the question, because this works:
function unsafe_read1!(dest::Ptr{UInt8},src::AbstractVector{UInt8},nbytes::UInt)
unsafe_copyto!(dest, pointer(src), nbytes)
nothing
end
nbytes=UInt(10)
I want to support reading a specific range of bytes that are not contiguous in CPU memory, for example, the bytes in src may be compressed, in a hard drive, or in GPU memory.
You could use unsafe_wrap to associate a Vector to dest without allocating:
function unsafe_read3!(
dest::Ptr{UInt8},
src::AbstractVector{UInt8},
so::Integer,
nbytes::Integer
)::Nothing
dest_array = unsafe_wrap(Vector{UInt8}, dest, nbytes)
copyto!(dest_array, 1, src, so, nbytes)
return nothing
end
After the setup from @nhz2’s second post, and with nbytes = length(s) (and the original unsafe_read1! modified to accept nbytes::Integer, like in @matthias314 's unsafe_read2!):
If you want to have it faster, you can use @inbounds. Ignoring manual bounds checking, that gives
function unsafe_read3!(
dest::Ptr{T},
src::AbstractVector{T},
so::Integer,
nbytes::Integer) where T
for i in 1:nbytes
unsafe_store!(dest, @inbounds(src[so+i-1]), i)
end
end