File source code
Line
| Exclusive |
Inclusive |
Code |
1 |
|
|
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
2 |
|
|
|
3 |
|
|
==(w::WeakRef, v::WeakRef) = isequal(w.value, v.value)
|
4 |
|
|
==(w::WeakRef, v) = isequal(w.value, v)
|
5 |
|
|
==(w, v::WeakRef) = isequal(w, v.value)
|
6 |
|
|
|
7 |
|
|
"""
|
8 |
|
|
finalizer(f, x)
|
9 |
|
|
|
10 |
|
|
Register a function `f(x)` to be called when there are no program-accessible references to
|
11 |
|
|
`x`, and return `x`. The type of `x` must be a `mutable struct`, otherwise the behavior of
|
12 |
|
|
this function is unpredictable.
|
13 |
|
|
"""
|
14 |
|
|
function finalizer(@nospecialize(f), @nospecialize(o))
|
15 |
|
|
if isimmutable(o)
|
16 |
|
|
error("objects of type ", typeof(o), " cannot be finalized")
|
17 |
|
|
end
|
18 |
|
|
ccall(:jl_gc_add_finalizer_th, Cvoid, (Ptr{Cvoid}, Any, Any),
|
19 |
|
|
Core.getptls(), o, f)
|
20 |
|
|
return o
|
21 |
|
|
end
|
22 |
|
|
|
23 |
|
|
function finalizer(f::Ptr{Cvoid}, o::T) where T
|
24 |
|
|
@_inline_meta
|
25 |
|
|
if isimmutable(T)
|
26 |
|
|
error("objects of type ", T, " cannot be finalized")
|
27 |
|
|
end
|
28 |
|
|
ccall(:jl_gc_add_ptr_finalizer, Cvoid, (Ptr{Cvoid}, Any, Ptr{Cvoid}),
|
29 |
|
|
Core.getptls(), o, f)
|
30 |
|
|
return o
|
31 |
|
|
end
|
32 |
|
|
|
33 |
|
|
"""
|
34 |
|
|
finalize(x)
|
35 |
|
|
|
36 |
|
|
Immediately run finalizers registered for object `x`.
|
37 |
|
|
"""
|
38 |
|
|
finalize(@nospecialize(o)) = ccall(:jl_finalize_th, Cvoid, (Ptr{Cvoid}, Any,),
|
39 |
|
|
Core.getptls(), o)
|
40 |
|
|
|
41 |
|
|
"""
|
42 |
|
|
Base.GC
|
43 |
|
|
|
44 |
|
|
Module with garbage collection utilities.
|
45 |
|
|
"""
|
46 |
|
|
module GC
|
47 |
|
|
|
48 |
|
|
"""
|
49 |
|
|
GC.gc()
|
50 |
|
|
|
51 |
|
|
Perform garbage collection.
|
52 |
|
|
|
53 |
|
|
!!! warning
|
54 |
|
|
Excessive use will likely lead to poor performance.
|
55 |
|
|
"""
|
56 |
|
|
gc(full::Bool=true) = ccall(:jl_gc_collect, Cvoid, (Int32,), full)
|
57 |
|
|
|
58 |
|
|
"""
|
59 |
|
|
GC.enable(on::Bool)
|
60 |
|
|
|
61 |
|
|
Control whether garbage collection is enabled using a boolean argument (`true` for enabled,
|
62 |
|
|
`false` for disabled). Return previous GC state.
|
63 |
|
|
|
64 |
|
|
!!! warning
|
65 |
|
|
Disabling garbage collection should be used only with caution, as it can cause memory
|
66 |
|
|
use to grow without bound.
|
67 |
|
|
"""
|
68 |
|
|
enable(on::Bool) = ccall(:jl_gc_enable, Int32, (Int32,), on) != 0
|
69 |
|
|
|
70 |
|
|
"""
|
71 |
|
|
GC.@preserve x1 x2 ... xn expr
|
72 |
|
|
|
73 |
|
|
Temporarily protect the given objects from being garbage collected, even if they would
|
74 |
|
|
otherwise be unreferenced.
|
75 |
|
|
|
76 |
|
|
The last argument is the expression during which the object(s) will be preserved.
|
77 |
|
|
The previous arguments are the objects to preserve.
|
78 |
|
|
"""
|
79 |
|
|
macro preserve(args...)
|
80 |
|
|
syms = args[1:end-1]
|
81 |
|
|
for x in syms
|
82 |
|
|
isa(x, Symbol) || error("Preserved variable must be a symbol")
|
83 |
|
|
end
|
84 |
|
|
s, r = gensym(), gensym()
|
85 |
|
|
esc(quote
|
86 |
|
|
$s = $(Expr(:gc_preserve_begin, syms...))
|
87 |
|
5 (11.90%) |
$r = $(args[end])
|
88 |
|
|
$(Expr(:gc_preserve_end, s))
|
89 |
|
|
$r
|
90 |
|
|
end)
|
91 |
|
|
end
|
92 |
|
|
|
93 |
|
|
end # module GC
|