Skip to content

Commit 0c65bff

Browse files
authored
Fix reference constructors (#9)
1 parent d9f69d9 commit 0c65bff

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ManualMemory"
22
uuid = "d125e4d3-2237-4719-b19c-fa641b8a4667"
33
authors = ["chriselrod <[email protected]> and contributors"]
4-
version = "0.1.5"
4+
version = "0.1.6"
55

66
[compat]
77
julia = "1.5"

src/ManualMemory.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ end
5353
mutable struct Reference{T}
5454
data::T
5555

56-
Reference{T}() where {T} = new()
57-
Reference{T}(x) where {T} = new(x)
56+
Reference{T}() where {T} = new{T}()
57+
Reference(x) = new{typeof(x)}(x)
5858
end
59+
@inline Base.pointer(r::Reference{T}) where {T} = Ptr{T}(pointer_from_objref(r))
5960
@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)
6061
@inline dereference(r::Reference) = getfield(r, :data)
6162
@inline dereference(x) = x

test/runtests.jl

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ManualMemory: MemoryBuffer, load, store!, LazyPreserve, preserve, PseudoPtr
1+
using ManualMemory: MemoryBuffer, load, store!, LazyPreserve, preserve, PseudoPtr, Reference
22
using Test
33

44
@testset "ManualMemory.jl" begin
@@ -24,6 +24,13 @@ using Test
2424
p = 1 + p
2525
store!(p, 3)
2626
@test load(p) === 3
27+
28+
x = Reference{Int}()
29+
y = Reference(1)
30+
GC.@preserve x y begin
31+
store!(pointer(x), 1)
32+
@test load(pointer(x)) === 1 === load(pointer(y))
33+
end
2734
end
2835

2936
using ThreadingUtilities

0 commit comments

Comments
 (0)