Skip to content

Commit

Permalink
tabs == 2 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod committed Sep 5, 2021
1 parent 0c65bff commit 71ef718
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions src/ManualMemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Provides a convenient wrapper that functions like `pointer(data)` for instances
cannot produce a viable pointer.
"""
struct PseudoPtr{T,D} <: Ref{T}
data::D
position::Int
data::D
position::Int

PseudoPtr(data::D, position) where {D} = new{eltype(D),D}(data, position)
PseudoPtr(data) = PseudoPtr(data, firstindex(data))
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
Expand Down Expand Up @@ -51,10 +51,9 @@ end
@inline store!(p::Ptr{T}, v) where {T} = store!(p, convert(T, v))

mutable struct Reference{T}
data::T

Reference{T}() where {T} = new{T}()
Reference(x) = new{typeof(x)}(x)
data::T
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)
Expand All @@ -71,8 +70,8 @@ specified conversion to a pointer occurs through a call equivalent to
`pointer(preserve_buffer(x))` occurs.
"""
struct LazyPreserve{A,P}
arg::A
ptrcall::P
arg::A
ptrcall::P
end
LazyPreserve(arg) = LazyPreserve(arg, nothing)
(p::LazyPreserve)(x) = p.ptrcall(x, p.arg)
Expand Down Expand Up @@ -228,37 +227,37 @@ julia> x[1]
"""
preserve(op, args...; kwargs...) = _preserve(op, args, kwargs.data)
@generated function _preserve(op, args::A, kwargs::NamedTuple{syms,K}) where {A,syms,K}
_preserve_expr(A, syms, K)
_preserve_expr(A, syms, K)
end
function _preserve_expr(::Type{A}, syms::Tuple{Vararg{Symbol}}, ::Type{K}) where {A,K}
body = Expr(:block, Expr(:meta,:inline))
call = Expr(:call, :op)
pres = :(GC.@preserve)
@inbounds for i in 1:length(A.parameters)
arg_i = _unwrap_preserve(body, pres, :(getfield(args, $i)), A.parameters[i])
push!(call.args, arg_i)
end
if length(syms) > 0
kwargs = Expr(:parameters)
@inbounds for i in 1:length(syms)
arg_i = _unwrap_preserve(body, pres, :(getfield(kwargs, $i)), K.parameters[i])
push!(call.args, Expr(:kw, syms[i], arg_i))
end
push!(call.args, kwargs)
body = Expr(:block, Expr(:meta,:inline))
call = Expr(:call, :op)
pres = :(GC.@preserve)
@inbounds for i in 1:length(A.parameters)
arg_i = _unwrap_preserve(body, pres, :(getfield(args, $i)), A.parameters[i])
push!(call.args, arg_i)
end
if length(syms) > 0
kwargs = Expr(:parameters)
@inbounds for i in 1:length(syms)
arg_i = _unwrap_preserve(body, pres, :(getfield(kwargs, $i)), K.parameters[i])
push!(call.args, Expr(:kw, syms[i], arg_i))
end
push!(pres.args, call)
push!(body.args, pres)
return body
push!(call.args, kwargs)
end
push!(pres.args, call)
push!(body.args, pres)
return body
end
function _unwrap_preserve(body::Expr, pres::Expr, argexpr::Expr, argtype::Type)
if argtype <: LazyPreserve
bufsym = gensym()
push!(body.args, Expr(:(=), bufsym, Expr(:call, :preserve_buffer, argexpr)))
push!(pres.args, bufsym)
return :($argexpr($bufsym))
else
return argexpr
end
if argtype <: LazyPreserve
bufsym = gensym()
push!(body.args, Expr(:(=), bufsym, Expr(:call, :preserve_buffer, argexpr)))
push!(pres.args, bufsym)
return :($argexpr($bufsym))
else
return argexpr
end
end

end

0 comments on commit 71ef718

Please sign in to comment.