Skip to content

Commit 1828f6a

Browse files
lkdvosLilithHafner
andauthored
Implement DenseArray and Strided Interface (#10)
* Change PtrArray from AbstractArray to DenseArray * Implement the Strided Array interface * Fix small typos * Add some DenseArray tests --------- Co-authored-by: Lilith Orion Hafner <[email protected]>
1 parent 3cd4f5e commit 1828f6a

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/PtrArrays.jl

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ module PtrArrays
33
export malloc, free, PtrArray
44

55
"""
6-
PtrArray(ptr::Ptr{T}, dims::Int...; check_dims=true) <: AbstractArray{T}
6+
PtrArray(ptr::Ptr{T}, dims::Int...; check_dims=true) <: DenseArray{T}
77
8-
Wrap a pointer in an `AbstractArray` interface conformant `PtrArray` using the standard
8+
Wrap a pointer in an `DenseArray` interface conformant `PtrArray` using the standard
99
Julia memory order.
1010
1111
Validates that `dims` are non-negative and don't overflow when multiplied if `check_dims` is
12-
true. Wierd things might happen if you set `check_dims=false` and use nagative or
12+
true. Weird things might happen if you set `check_dims=false` and use negative or
1313
overflowing `dims`.
1414
1515
!!! note
@@ -20,7 +20,7 @@ overflowing `dims`.
2020
2121
see also [`malloc`](@ref), [`free`](@ref)
2222
"""
23-
struct PtrArray{T, N} <: AbstractArray{T, N}
23+
struct PtrArray{T, N} <: DenseArray{T, N}
2424
ptr::Ptr{T}
2525
size::NTuple{N, Int}
2626
function PtrArray(ptr::Ptr{T}, dims::Vararg{Int, N}; check_dims=true) where {T, N}
@@ -71,7 +71,7 @@ end
7171
Free the memory allocated by a [`PtrArray`](@ref) allocated by [`malloc`](@ref).
7272
7373
It is only safe to call this function on `PtrArray`s returned by `malloc`, and it is unsafe
74-
to perform any opperation on a `PtrArray` after calling `free`.
74+
to perform any operation on a `PtrArray` after calling `free`.
7575
"""
7676
free(p::PtrArray) = Libc.free(p.ptr)
7777

@@ -87,4 +87,8 @@ Base.@propagate_inbounds function Base.setindex!(p::PtrArray, v, i::Int)
8787
p
8888
end
8989

90+
# Strided array interface https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-strided-arrays
91+
Base.unsafe_convert(::Type{Ptr{T}}, p::PtrArray{T}) where T = p.ptr
92+
Base.elsize(::Type{P}) where P<:PtrArray = sizeof(eltype(P))
93+
9094
end

test/runtests.jl

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ end
1010
@testset "Basics" begin
1111
x = malloc(Int, 10)
1212
@test x isa AbstractVector{Int}
13+
@test x isa DenseVector{Int}
1314
@test x isa PtrArray
1415

1516
x .= 1:10
@@ -25,7 +26,8 @@ end
2526
y = malloc(Complex{Float64}, 4, 10)
2627
@test length(y) == 40
2728
@test size(y) == (4, 10)
28-
@test y isa AbstractMatrix{Complex{Float64}}
29+
@test strides(y) == (1, 4)
30+
@test y isa DenseMatrix{Complex{Float64}}
2931
@test y isa PtrArray
3032

3133
fill!(y, im)
@@ -39,6 +41,16 @@ end
3941
@test free(y) === nothing
4042

4143
@test_throws ArgumentError malloc(Vector{Int}, 10)
44+
45+
# Strided array API
46+
z = malloc(Int, 4, 6, 10)
47+
@test length(z) == 240
48+
@test size(z) == (4, 6, 10)
49+
@test strides(z) == (1, 4, 24)
50+
@test Base.elsize(z) == sizeof(Int)
51+
@test z isa PtrArray
52+
@test z isa DenseArray{Int, 3}
53+
free(z)
4254
end
4355

4456
function f(x, y)

0 commit comments

Comments
 (0)