88
99# The parent method for memoryref was added in 1.12. In versions before that,
1010# it can be accessed by reaching into internals.
11- @static if VERSION < v " 1.12"
11+ @static if VERSION < v " 1.12.0-DEV.966 "
1212 Base. parent (v:: MemoryView ) = v. ref. mem
1313else
1414 Base. parent (v:: MemoryView ) = parent (v. ref)
@@ -23,17 +23,10 @@ function Base.iterate(x::MemoryView, i::Int = 1)
2323end
2424
2525# Base.memoryindex exists in Julia 1.13 onwards.
26- @static if VERSION < v " 1.13"
26+ @static if VERSION < v " 1.13.0-DEV.1289 "
2727 function Base. parentindices (x:: MemoryView )
28- elz = Base. elsize (x)
29- return if iszero (elz)
30- offset = Int (x. ref. ptr_or_offset)
31- ((1 + offset): (x. len + offset),)
32- else
33- byte_offset = pointer (x. ref) - pointer (x. ref. mem)
34- elem_offset = div (byte_offset % UInt, elz % UInt) % Int
35- ((elem_offset + 1 ): (elem_offset + x. len),)
36- end
28+ start = Core. memoryrefoffset (x. ref)
29+ return (start: (start + length (x) - 1 ),)
3730 end
3831else
3932 function Base. parentindices (x:: MemoryView )
@@ -85,12 +78,14 @@ function Base.mightalias(a::MemoryView{T}, b::MemoryView{T}) where {T}
8578 (isempty (a) | isempty (b)) && return false
8679 # We can't compare the underlying Memory with === to add a fast path here,
8780 # because users can create aliasing, but distinct Memory using unsafe_wrap.
88- (p1, p2) = (pointer (a), pointer (b))
89- elz = Base. elsize (a)
90- return if p1 < p2
91- p1 + length (a) * elz > p2
92- else
93- p2 + length (b) * elz > p1
81+ GC. @preserve a b begin
82+ (p1, p2) = (pointer (a), pointer (b))
83+ elz = Base. elsize (a)
84+ return if p1 < p2
85+ p1 + length (a) * elz > p2
86+ else
87+ p2 + length (b) * elz > p1
88+ end
9489 end
9590end
9691
@@ -183,12 +178,23 @@ function Base.copyto!(dst::MutableMemoryView{T}, src::MemoryView{T}) where {T}
183178 return unsafe_copyto! (dst, src)
184179end
185180
181+ # This function is kind of bad API, and users should not use it. However, without this overload,
182+ # the fallback definition is used instead which is even worse.
183+ function Base. copyto! (dst:: MutableMemoryView , di:: Integer , src:: MemoryView{T} , si:: Integer , N:: Integer ) where {T}
184+ di = Int (di):: Int
185+ si = Int (si):: Int
186+ N = Int (N):: Int
187+ dst = dst[di: (di + N - 1 )]
188+ src = src[si: (si + N - 1 )]
189+ return copyto! (dst, src)
190+ end
191+
186192function Base. fill! (v:: MutableMemoryView{UInt8} , x:: Integer )
187193 xT = convert (UInt8, x):: UInt8
188194 isempty (v) && return v
189195 GC. @preserve v @ccall memset (
190- pointer (v):: Ptr{Nothing } ,
191- Int32 (xT):: Cint ,
196+ pointer (v):: Ptr{Cvoid } ,
197+ Cint (xT):: Cint ,
192198 (length (v) % UInt):: Csize_t
193199 ):: Cvoid
194200 return v
@@ -252,7 +258,7 @@ function memchr(mem::ImmutableMemoryView{T}, byte::T) where {T <: Union{Int8, UI
252258 GC. @preserve mem begin
253259 ptr = Ptr {UInt8} (pointer (mem))
254260 p = @ccall memchr (
255- ptr:: Ptr{UInt8 } ,
261+ ptr:: Ptr{Cvoid } ,
256262 (byte % UInt8):: Cint ,
257263 length (mem):: Csize_t ,
258264 ):: Ptr{Cvoid}
@@ -311,7 +317,7 @@ function memrchr(mem::ImmutableMemoryView{T}, byte::T) where {T <: Union{Int8, U
311317 GC. @preserve mem begin
312318 ptr = Ptr {UInt8} (pointer (mem))
313319 p = @ccall memrchr (
314- ptr:: Ptr{UInt8 } ,
320+ ptr:: Ptr{Cvoid } ,
315321 (byte % UInt8):: Cint ,
316322 length (mem):: Csize_t ,
317323 ):: Ptr{Cvoid}
@@ -338,7 +344,7 @@ function Base.:(==)(a::Mem, b::Mem) where {Mem <: BitMemory}
338344 GC. @preserve a b begin
339345 aptr = Ptr {Nothing} (pointer (a))
340346 bptr = Ptr {Nothing} (pointer (b))
341- y = @ccall memcmp (aptr:: Ptr{Nothing } , bptr:: Ptr{Nothing } , nbytes:: Csize_t ):: Cint
347+ y = @ccall memcmp (aptr:: Ptr{Cvoid } , bptr:: Ptr{Cvoid } , nbytes:: Csize_t ):: Cint
342348 end
343349 return iszero (y)
344350end
@@ -349,9 +355,9 @@ function Base.cmp(a::MemoryView{UInt8}, b::MemoryView{UInt8})
349355 aptr = Ptr {Nothing} (pointer (a))
350356 bptr = Ptr {Nothing} (pointer (b))
351357 @ccall memcmp (
352- aptr:: Ptr{Nothing } ,
353- bptr:: Ptr{Nothing } ,
354- min (length (a), length (b)):: Int ,
358+ aptr:: Ptr{Cvoid } ,
359+ bptr:: Ptr{Cvoid } ,
360+ min (length (a), length (b)):: Csize_t ,
355361 ):: Cint
356362 end
357363 else
@@ -518,7 +524,8 @@ function split_unaligned(v::MemoryView{T, M}, ::Val{A}) where {A, T, M}
518524 # Early return here to avoid division by zero: Size sz is statically known,
519525 # this will be compiled away
520526 iszero (sz) && return (unsafe_new_memoryview (M, v. ref, 0 ), v)
521- unaligned_bytes = ((alignment - (UInt (pointer (v)) & mask)) & mask)
527+ ptr_int = GC. @preserve v UInt (pointer (v))
528+ unaligned_bytes = ((alignment - (ptr_int & mask)) & mask)
522529 n_elements = min (length (v), div (unaligned_bytes, sz % UInt) % Int)
523530 return @inbounds split_at (v, n_elements + 1 )
524531end
0 commit comments