Skip to content

Commit

Permalink
Fix reference constructors (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokazama authored Aug 9, 2021
1 parent d9f69d9 commit 0c65bff
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 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.5"
version = "0.1.6"

[compat]
julia = "1.5"
Expand Down
5 changes: 3 additions & 2 deletions src/ManualMemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 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, PseudoPtr
using ManualMemory: MemoryBuffer, load, store!, LazyPreserve, preserve, PseudoPtr, Reference
using Test

@testset "ManualMemory.jl" begin
Expand All @@ -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
Expand Down

2 comments on commit 0c65bff

@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/42442

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.6 -m "<description of version>" 0c65bff4897433cc8f3a83db8f65a56b31473cb0
git push origin v0.1.6

Please sign in to comment.