Skip to content

Commit

Permalink
Add Reference struct wrapper to load/store objects by pointer.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod committed Jul 11, 2021
1 parent 5b06e69 commit 4ce7d92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 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.2"
version = "0.1.3"

[compat]
julia = "1.5"
Expand Down
15 changes: 10 additions & 5 deletions src/ManualMemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end
Expr(:block, Expr(:meta,:inline), :(ccall(:jl_value_ptr, Ref{$T}, (Ptr{Cvoid},), unsafe_load(Base.unsafe_convert(Ptr{Ptr{Cvoid}}, p)))))
end
end
@inline load(p::Ptr{UInt}, ::Type{T}) where {T} = load(reinterpret(Ptr{T}, p))
@inline load(p::Ptr{UInt}, ::Type{T}) where {T} = load(p, T, 0)[2]
@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 All @@ -29,6 +29,11 @@ end

@inline store!(p::Ptr{T}, v) where {T} = store!(p, convert(T, v))

mutable struct Reference{T}; data::T; end
@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

"""
LazyPreserve(x, ptrcall=nothing)
Expand Down Expand Up @@ -113,7 +118,7 @@ function load_aggregate(::Type{T}, offset::Int) where {T}
TF = fieldtype(T, f)
if Base.issingletontype(TF)
push!(call.args, TF.instance)
elseif fieldcount(TF) 0
elseif (fieldcount(TF) 0) || (TF <: Reference)
ptr = :(p + (offset + $offset))
ptr = TF === UInt ? ptr : :(reinterpret(Ptr{$TF}, $ptr))
push!(call.args, :(load($ptr)))
Expand All @@ -128,7 +133,7 @@ end
@generated function load(p::Ptr{UInt}, ::Type{T}, offset::Int) where {T}
if Base.issingletontype(T)
call = Expr(:tuple, :offset, T.instance)
elseif fieldcount(T) 0
elseif (fieldcount(T) 0) || (T <: Reference)
ptr = :(p + offset)
ptr = T === UInt ? ptr : :(reinterpret(Ptr{$T}, $ptr))
call = :(((offset + $(offsetsize(T)), load($ptr))))
Expand All @@ -145,7 +150,7 @@ function store_aggregate!(q::Expr, sym, ::Type{T}, offset::Int) where {T}
TF = fieldtype(T, f)
Base.issingletontype(TF) && continue
gfcall = Expr(:call, gf, sym, f)
if fieldcount(TF) 0
if (fieldcount(TF) 0) || (TF <: Reference)
ptr = :(p + (offset + $offset))
ptr = TF === UInt ? ptr : :(reinterpret(Ptr{$TF}, $ptr))
push!(q.args, :(store!($ptr, $gfcall)))
Expand All @@ -161,7 +166,7 @@ end
@generated function store!(p::Ptr{UInt}, x::T, offset::Int) where {T}
Base.issingletontype(T) && return :offset
body = Expr(:block, Expr(:meta,:inline))
if fieldcount(T) 0
if (fieldcount(T) 0) || (T <: Reference)
ptr = :(p + offset)
ptr = T === UInt ? ptr : :(reinterpret(Ptr{$T}, $ptr))
push!(body.args, :(store!($ptr, x)))
Expand Down

2 comments on commit 4ce7d92

@chriselrod
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/40696

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.3 -m "<description of version>" 4ce7d920bc957d1597b5cd386868906b641c76b3
git push origin v0.1.3

Please sign in to comment.