Skip to content

Commit

Permalink
PseudoPtr (#3)
Browse files Browse the repository at this point in the history
* Impliment PseudoPtr

* More test coverage and version bump
  • Loading branch information
Tokazama authored Jul 13, 2021
1 parent 4ce7d92 commit 1ddedc8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ManualMemory"
uuid = "d125e4d3-2237-4719-b19c-fa641b8a4667"
authors = ["chriselrod <[email protected]> and contributors"]
version = "0.1.3"
version = "0.1.4"

[compat]
julia = "1.5"
Expand Down
21 changes: 21 additions & 0 deletions src/ManualMemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ end
@inline Base.unsafe_convert(::Type{Ptr{T}}, m::MemoryBuffer) where {T} = Ptr{T}(pointer_from_objref(m))
@inline Base.pointer(m::MemoryBuffer{N,T}) where {N,T} = Ptr{T}(pointer_from_objref(m))

"""
PseudoPtr(data, position=firstindex(data))
Provides a convenient wrapper that functions like `pointer(data)` for instances where `data`
cannot produce a viable pointer.
"""
struct PseudoPtr{T,D} <: Ref{T}
data::D
position::Int

PseudoPtr(data::D, position) where {D} = new{eltype(D),D}(data, position)
PseudoPtr(data) = PseudoPtr(data, firstindex(data))
end
Base.:(+)(x::PseudoPtr, y::Int) = PseudoPtr(getfield(x, :data), getfield(x, :position) + y)
Base.:(+)(x::Int, y::PseudoPtr) = y + x

@inline load(x::PseudoPtr) = @inbounds(getindex(getfield(x, :data), getfield(x, :position)))
@generated function load(p::Ptr{T}) where {T}
if Base.allocatedinline(T)
Expr(:block, Expr(:meta,:inline), :(unsafe_load(p)))
Expand All @@ -18,6 +35,10 @@ end
end
end
@inline load(p::Ptr{UInt}, ::Type{T}) where {T} = load(p, T, 0)[2]

@inline function store!(x::PseudoPtr, val)
@inbounds(setindex!(getfield(x, :data), val, getfield(x, :position)))
end
@generated function store!(p::Ptr{T}, v::T) where {T}
if Base.allocatedinline(T)
Expr(:block, Expr(:meta,:inline), :(unsafe_store!(p, v); return nothing))
Expand Down
10 changes: 9 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ManualMemory: MemoryBuffer, load, store!, LazyPreserve, preserve
using ManualMemory: MemoryBuffer, load, store!, LazyPreserve, preserve, PseudoPtr
using Test

@testset "ManualMemory.jl" begin
Expand All @@ -16,6 +16,14 @@ using Test
x = [0 0; 0 0];
preserve(store!, LazyPreserve(x), 1)
@test x[1] === 1
p = PseudoPtr(x, 1)
@test load(p) === 1
p += 1
store!(p, 2)
@test load(p) === 2
p = 1 + p
store!(p, 3)
@test load(p) === 3
end

using ThreadingUtilities
Expand Down

2 comments on commit 1ddedc8

@Tokazama
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/40802

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.4 -m "<description of version>" 1ddedc8aa4a3c71943ccd3042c767f1331a6c9d1
git push origin v0.1.4

Please sign in to comment.