Skip to content

Commit

Permalink
Additional GC.@ preserve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed Mar 17, 2020
1 parent 36241a9 commit 2c25a5b
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions base/gcutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,36 @@ enable(on::Bool) = ccall(:jl_gc_enable, Int32, (Int32,), on) != 0
"""
GC.@preserve x1 x2 ... xn expr
Temporarily protect the given objects from being garbage collected, even if they would
otherwise be unreferenced.
Mark the objects `x1, x2, ...` as being *in use* during the evaluation of the
expression `expr`. This is only required in unsafe code where `expr`
*implicitly uses* memory or other resources owned by one of the `x`s.
*Implicit use* of `x` covers any indirect use of resources logically owned by
`x` which the compiler cannot see. Some examples:
* Accessing memory of an object directly via a `Ptr`
* Passing a pointer to `x` to `ccall` when the pointer is computed outside the
normal `cconvert` argument conversion pipeline.
* Using resources of `x` which would be cleaned up in the finalizer.
`@preserve` should generally not have any performance impact in typical use
cases where it briefly extends object lifetime. In implementation, `@preserve`
has effects such as protecting dynamically allocated objects from garbage
collection.
The last argument is the expression during which the object(s) will be preserved.
The previous arguments are the objects to preserve.
# Examples
When loading from a pointer with `unsafe_load`, the underlying object is
implicitly used, for example `x` is implicitly used by `unsafe_load(p)` in the
following:
```jldoctest
julia> let
x = Ref{Int}(101)
p = Base.unsafe_convert(Ptr{Int}, x)
GC.@preserve x unsafe_load(p)
end
101
```
"""
macro preserve(args...)
syms = args[1:end-1]
Expand Down

0 comments on commit 2c25a5b

Please sign in to comment.