From d3b8ebf2a2970beb4203c1eb6d38ac9c71e1e5de Mon Sep 17 00:00:00 2001 From: KristofferC Date: Tue, 18 Jun 2024 17:17:21 +0200 Subject: [PATCH] Revert "add `wrap` function which is the safe counterpart to `unsafe_wrap`. (#52049)" This reverts commit 84cfe04e7f149235cbb903cc6f4110ddaa7007eb. --- NEWS.md | 1 - base/array.jl | 51 ------------------------------------------------ base/exports.jl | 1 - test/arrayops.jl | 28 -------------------------- 4 files changed, 81 deletions(-) diff --git a/NEWS.md b/NEWS.md index 11424d5df1381..927fa4ea5fb81 100644 --- a/NEWS.md +++ b/NEWS.md @@ -88,7 +88,6 @@ New library functions * `Sys.username()` can be used to return the current user's username ([#51897]). * `Sys.isreadable(), Sys.iswritable()` can be used to check if the current user has access permissions that permit reading and writing, respectively. ([#53320]). -* `wrap(Array, m::Union{MemoryRef{T}, Memory{T}}, dims)` is the safe counterpart to `unsafe_wrap` ([#52049]). * `GC.logging_enabled()` can be used to test whether GC logging has been enabled via `GC.enable_logging` ([#51647]). * `IdSet` is now exported from Base and considered public ([#53262]). * `@time` now reports a count of any lock conflicts where a `ReentrantLock` had to wait, plus a new macro diff --git a/base/array.jl b/base/array.jl index 190b37cc12cf6..8824e77799690 100644 --- a/base/array.jl +++ b/base/array.jl @@ -3060,54 +3060,3 @@ intersect(r::AbstractRange, v::AbstractVector) = intersect(v, r) _getindex(v, i) end end - -""" - wrap(Array, m::Union{Memory{T}, MemoryRef{T}}, dims) - -Create an array of size `dims` using `m` as the underlying memory. This can be thought of as a safe version -of [`unsafe_wrap`](@ref) utilizing `Memory` or `MemoryRef` instead of raw pointers. -""" -function wrap end - -# validity checking for _wrap calls, separate from allocation of Array so that it can be more likely to inline into the caller -function _wrap(ref::MemoryRef{T}, dims::NTuple{N, Int}) where {T, N} - mem = ref.mem - mem_len = length(mem) + 1 - memoryrefoffset(ref) - len = Core.checked_dims(dims...) - @boundscheck mem_len >= len || invalid_wrap_err(mem_len, dims, len) - if N != 1 && !(ref === GenericMemoryRef(mem) && len === mem_len) - mem = ccall(:jl_genericmemory_slice, Memory{T}, (Any, Ptr{Cvoid}, Int), mem, ref.ptr_or_offset, len) - ref = MemoryRef(mem) - end - return ref -end - -@noinline invalid_wrap_err(len, dims, proddims) = throw(DimensionMismatch( - "Attempted to wrap a MemoryRef of length $len with an Array of size dims=$dims, which is invalid because prod(dims) = $proddims > $len, so that the array would have more elements than the underlying memory can store.")) - -@eval @propagate_inbounds function wrap(::Type{Array}, m::MemoryRef{T}, dims::NTuple{N, Integer}) where {T, N} - dims = convert(Dims, dims) - ref = _wrap(m, dims) - $(Expr(:new, :(Array{T, N}), :ref, :dims)) -end - -@eval @propagate_inbounds function wrap(::Type{Array}, m::Memory{T}, dims::NTuple{N, Integer}) where {T, N} - dims = convert(Dims, dims) - ref = _wrap(MemoryRef(m), dims) - $(Expr(:new, :(Array{T, N}), :ref, :dims)) -end -@eval @propagate_inbounds function wrap(::Type{Array}, m::MemoryRef{T}, l::Integer) where {T} - dims = (Int(l),) - ref = _wrap(m, dims) - $(Expr(:new, :(Array{T, 1}), :ref, :dims)) -end -@eval @propagate_inbounds function wrap(::Type{Array}, m::Memory{T}, l::Integer) where {T} - dims = (Int(l),) - ref = _wrap(MemoryRef(m), (l,)) - $(Expr(:new, :(Array{T, 1}), :ref, :dims)) -end -@eval @propagate_inbounds function wrap(::Type{Array}, m::Memory{T}) where {T} - ref = MemoryRef(m) - dims = (length(m),) - $(Expr(:new, :(Array{T, 1}), :ref, :dims)) -end diff --git a/base/exports.jl b/base/exports.jl index 03aa795c38a77..ffbe0dd6830ba 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -460,7 +460,6 @@ export vcat, vec, view, - wrap, zeros, # search, find, match and related functions diff --git a/test/arrayops.jl b/test/arrayops.jl index 566dd44b8dcd9..c2459fdbd5cb9 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -3186,31 +3186,3 @@ end @test c + zero(c) == c end end - -@testset "Wrapping Memory into Arrays" begin - mem = Memory{Int}(undef, 10) .= 1 - memref = MemoryRef(mem) - @test_throws DimensionMismatch wrap(Array, mem, (10, 10)) - @test wrap(Array, mem, (5,)) == ones(Int, 5) - @test wrap(Array, mem, 2) == ones(Int, 2) - @test wrap(Array, memref, 10) == ones(Int, 10) - @test wrap(Array, memref, (2,2,2)) == ones(Int,2,2,2) - @test wrap(Array, mem, (5, 2)) == ones(Int, 5, 2) - - memref2 = MemoryRef(mem, 3) - @test wrap(Array, memref2, (5,)) == ones(Int, 5) - @test wrap(Array, memref2, 2) == ones(Int, 2) - @test wrap(Array, memref2, (2,2,2)) == ones(Int,2,2,2) - @test wrap(Array, memref2, (3, 2)) == ones(Int, 3, 2) - @test_throws DimensionMismatch wrap(Array, memref2, 9) - @test_throws DimensionMismatch wrap(Array, memref2, 10) -end - -@testset "Memory size" begin - len = 5 - mem = Memory{Int}(undef, len) - @test size(mem, 1) == len - @test size(mem, 0x1) == len - @test size(mem, 2) == 1 - @test size(mem, 0x2) == 1 -end