diff --git a/Project.toml b/Project.toml index 40b9223..f760ecc 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ManualMemory" uuid = "d125e4d3-2237-4719-b19c-fa641b8a4667" authors = ["chriselrod and contributors"] -version = "0.1.5" +version = "0.1.6" [compat] julia = "1.5" diff --git a/src/ManualMemory.jl b/src/ManualMemory.jl index 5f41426..820644c 100644 --- a/src/ManualMemory.jl +++ b/src/ManualMemory.jl @@ -53,9 +53,10 @@ end mutable struct Reference{T} data::T - Reference{T}() where {T} = new() - Reference{T}(x) where {T} = new(x) + Reference{T}() where {T} = new{T}() + Reference(x) = new{typeof(x)}(x) end +@inline Base.pointer(r::Reference{T}) where {T} = Ptr{T}(pointer_from_objref(r)) @inline load(p::Ptr{Reference{T}}) where {T} = getfield(ccall(:jl_value_ptr, Ref{Reference{T}}, (Ptr{Cvoid},), unsafe_load(Base.unsafe_convert(Ptr{Ptr{Cvoid}}, p))), :data) @inline dereference(r::Reference) = getfield(r, :data) @inline dereference(x) = x diff --git a/test/runtests.jl b/test/runtests.jl index 014336c..7fc1516 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using ManualMemory: MemoryBuffer, load, store!, LazyPreserve, preserve, PseudoPtr +using ManualMemory: MemoryBuffer, load, store!, LazyPreserve, preserve, PseudoPtr, Reference using Test @testset "ManualMemory.jl" begin @@ -24,6 +24,13 @@ using Test p = 1 + p store!(p, 3) @test load(p) === 3 + + x = Reference{Int}() + y = Reference(1) + GC.@preserve x y begin + store!(pointer(x), 1) + @test load(pointer(x)) === 1 === load(pointer(y)) + end end using ThreadingUtilities