forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split refpointer into RefValue parts and everything else
- Loading branch information
Showing
5 changed files
with
66 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
### Methods for a Ref object that can store a single value of any type | ||
|
||
mutable struct RefValue{T} <: Ref{T} | ||
x::T | ||
RefValue{T}() where {T} = new() | ||
RefValue{T}(x) where {T} = new(x) | ||
end | ||
RefValue(x::T) where {T} = RefValue{T}(x) | ||
isassigned(x::RefValue) = isdefined(x, :x) | ||
|
||
function unsafe_convert(P::Type{Ptr{T}}, b::RefValue{T}) where T | ||
if datatype_pointerfree(RefValue{T}) | ||
p = pointer_from_objref(b) | ||
elseif isconcretetype(T) && T.mutable | ||
p = pointer_from_objref(b.x) | ||
else | ||
# If the slot is not leaf type, it could be either immutable or not. | ||
# If it is actually an immutable, then we can't take it's pointer directly | ||
# Instead, explicitly load the pointer from the `RefValue`, | ||
# which also ensures this returns same pointer as the one rooted in the `RefValue` object. | ||
p = pointerref(Ptr{Ptr{Cvoid}}(pointer_from_objref(b)), 1, Core.sizeof(Ptr{Cvoid})) | ||
end | ||
return convert(P, p) | ||
end | ||
function unsafe_convert(P::Type{Ptr{Any}}, b::RefValue{Any}) | ||
return convert(P, pointer_from_objref(b)) | ||
end | ||
unsafe_convert(::Type{Ptr{Cvoid}}, b::RefValue{T}) where {T} = convert(Ptr{Cvoid}, unsafe_convert(Ptr{T}, b)) | ||
|
||
getindex(b::RefValue) = b.x | ||
setindex!(b::RefValue, x) = (b.x = x; b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters