Skip to content

Commit 544e2ae

Browse files
authored
Merge pull request #25616 from JuliaLang/aa/stop-exporting-gc-stuff
Move gc and gc_enable to their own module
2 parents 6c3cee7 + bdc9094 commit 544e2ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+221
-201
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,10 @@ Deprecated or removed
966966

967967
* `ObjectIdDict` has been deprecated in favor of `IdDict{Any,Any}` ([#25210]).
968968

969+
* `gc` and `gc_enable` have been deprecated in favor of `GC.gc` and `GC.enable` ([#25616]).
970+
971+
* `Base.@gc_preserve` has been deprecated in favor of `GC.@preserve` ([#25616]).
972+
969973
Command-line option changes
970974
---------------------------
971975

@@ -1217,3 +1221,4 @@ Command-line option changes
12171221
[#25424]: https://github.com/JuliaLang/julia/issues/25424
12181222
[#25532]: https://github.com/JuliaLang/julia/issues/25532
12191223
[#25545]: https://github.com/JuliaLang/julia/issues/25545
1224+
[#25616]: https://github.com/JuliaLang/julia/issues/25616

base/compiler/optimize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3091,7 +3091,7 @@ function split_struct_alloc!(ctx::AllocOptContext, info, key)
30913091
# Requires all conditions for `getfield`.
30923092
# Additionally require all defs to be mutable.
30933093
#
3094-
# * preserved objects (`@gc_preserve` and `ccall` roots)
3094+
# * preserved objects (`GC.@preserve` and `ccall` roots)
30953095
#
30963096
# No `setfield!` should be called for mutable defs on the NULL sites.
30973097
# This is because it's currently unclear how conditional undefined root slots

base/deepcopy.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function deepcopy_internal(x::String, stackdict::IdDict)
4646
if haskey(stackdict, x)
4747
return stackdict[x]
4848
end
49-
y = @gc_preserve x unsafe_string(pointer(x), sizeof(x))
49+
y = GC.@preserve x unsafe_string(pointer(x), sizeof(x))
5050
stackdict[x] = y
5151
return y
5252
end

base/deprecated.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,10 @@ end
15731573

15741574
@deprecate object_id objectid
15751575

1576+
@deprecate gc GC.gc
1577+
@deprecate gc_enable GC.enable
1578+
@eval @deprecate $(Symbol("@gc_preserve")) GC.$(Symbol("@preserve")) false
1579+
15761580
# issue #9053
15771581
if Sys.iswindows()
15781582
function Filesystem.tempname(uunique::UInt32)

base/env.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ if Sys.iswindows()
9797
blk = block[2]
9898
len = ccall(:wcslen, UInt, (Ptr{UInt16},), pos)
9999
buf = Vector{UInt16}(uninitialized, len)
100-
@gc_preserve buf unsafe_copyto!(pointer(buf), pos, len)
100+
GC.@preserve buf unsafe_copyto!(pointer(buf), pos, len)
101101
env = transcode(String, buf)
102102
m = match(r"^(=?[^=]+)=(.*)$"s, env)
103103
if m === nothing

base/exports.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,9 @@ export
806806
include_dependency,
807807

808808
# RTS internals
809+
GC,
809810
finalizer,
810811
finalize,
811-
gc,
812-
gc_enable,
813812
precompile,
814813

815814
# misc

base/gcutils.jl

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,51 @@ Immediately run finalizers registered for object `x`.
3838
finalize(@nospecialize(o)) = ccall(:jl_finalize_th, Cvoid, (Ptr{Cvoid}, Any,),
3939
Core.getptls(), o)
4040

41+
module GC
42+
4143
"""
42-
gc()
44+
GC.gc()
45+
46+
Perform garbage collection.
4347
44-
Perform garbage collection. This should not generally be used.
48+
!!! warning
49+
Excessive use will likely lead to poor performance.
4550
"""
4651
gc(full::Bool=true) = ccall(:jl_gc_collect, Cvoid, (Int32,), full)
4752

4853
"""
49-
gc_enable(on::Bool)
54+
GC.enable(on::Bool)
5055
5156
Control whether garbage collection is enabled using a boolean argument (`true` for enabled,
52-
`false` for disabled). Return previous GC state. Disabling garbage collection should be
53-
used only with extreme caution, as it can cause memory use to grow without bound.
57+
`false` for disabled). Return previous GC state.
58+
59+
!!! warning
60+
Disabling garbage collection should be used only with caution, as it can cause memory
61+
use to grow without bound.
5462
"""
55-
gc_enable(on::Bool) = ccall(:jl_gc_enable, Int32, (Int32,), on) != 0
63+
enable(on::Bool) = ccall(:jl_gc_enable, Int32, (Int32,), on) != 0
64+
65+
"""
66+
GC.@preserve x1 x2 ... xn expr
67+
68+
Temporarily protect the given objects from being garbage collected, even if they would
69+
otherwise be unreferenced.
70+
71+
The last argument is the expression during which the object(s) will be preserved.
72+
The previous arguments are the objects to preserve.
73+
"""
74+
macro preserve(args...)
75+
syms = args[1:end-1]
76+
for x in syms
77+
isa(x, Symbol) || error("Preserved variable must be a symbol")
78+
end
79+
s, r = gensym(), gensym()
80+
esc(quote
81+
$s = $(Expr(:gc_preserve_begin, syms...))
82+
$r = $(args[end])
83+
$(Expr(:gc_preserve_end, s))
84+
$r
85+
end)
86+
end
87+
88+
end # module GC

base/gmp.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ function tryparse_internal(::Type{BigInt}, s::AbstractString, startpos::Int, end
253253
if Base.containsnul(bstr)
254254
err = -1 # embedded NUL char (not handled correctly by GMP)
255255
else
256-
err = Base.@gc_preserve bstr MPZ.set_str!(z, pointer(bstr)+(i-start(bstr)), base)
256+
err = GC.@preserve bstr MPZ.set_str!(z, pointer(bstr)+(i-start(bstr)), base)
257257
end
258258
if err != 0
259259
raise && throw(ArgumentError("invalid BigInt: $(repr(bstr))"))
@@ -613,7 +613,7 @@ function base(b::Integer, n::BigInt, pad::Integer=1)
613613
nd1 = ndigits(n, b)
614614
nd = max(nd1, pad)
615615
sv = Base.StringVector(nd + isneg(n))
616-
Base.@gc_preserve sv MPZ.get_str!(pointer(sv) + nd - nd1, b, n)
616+
GC.@preserve sv MPZ.get_str!(pointer(sv) + nd - nd1, b, n)
617617
@inbounds for i = (1:nd-nd1) .+ isneg(n)
618618
sv[i] = '0' % UInt8
619619
end

base/io.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ readlines(s=STDIN; chomp::Bool=true) = collect(eachline(s, chomp=chomp))
404404
## byte-order mark, ntoh & hton ##
405405

406406
let a = UInt32[0x01020304]
407-
endian_bom = @gc_preserve a unsafe_load(convert(Ptr{UInt8}, pointer(a)))
407+
endian_bom = GC.@preserve a unsafe_load(convert(Ptr{UInt8}, pointer(a)))
408408
global ntoh, hton, ltoh, htol
409409
if endian_bom == 0x01
410410
ntoh(x) = x
@@ -517,7 +517,7 @@ end
517517

518518
function write(s::IO, a::Array)
519519
if isbits(eltype(a))
520-
return @gc_preserve a unsafe_write(s, pointer(a), sizeof(a))
520+
return GC.@preserve a unsafe_write(s, pointer(a), sizeof(a))
521521
else
522522
depwarn("Calling `write` on non-isbits arrays is deprecated. Use a loop or `serialize` instead.", :write)
523523
nb = 0
@@ -534,7 +534,7 @@ function write(s::IO, a::SubArray{T,N,<:Array}) where {T,N}
534534
end
535535
elsz = sizeof(T)
536536
colsz = size(a,1) * elsz
537-
@gc_preserve a if stride(a,1) != 1
537+
GC.@preserve a if stride(a,1) != 1
538538
for idxs in CartesianIndices(size(a))
539539
unsafe_write(s, pointer(a, idxs.I), elsz)
540540
end
@@ -585,13 +585,13 @@ read(s::IO, ::Type{Bool}) = (read(s, UInt8) != 0)
585585
read(s::IO, ::Type{Ptr{T}}) where {T} = convert(Ptr{T}, read(s, UInt))
586586

587587
function read!(s::IO, a::Array{UInt8})
588-
@gc_preserve a unsafe_read(s, pointer(a), sizeof(a))
588+
GC.@preserve a unsafe_read(s, pointer(a), sizeof(a))
589589
return a
590590
end
591591

592592
function read!(s::IO, a::Array{T}) where T
593593
if isbits(T)
594-
@gc_preserve a unsafe_read(s, pointer(a), sizeof(a))
594+
GC.@preserve a unsafe_read(s, pointer(a), sizeof(a))
595595
else
596596
for i in eachindex(a)
597597
a[i] = read(s, T)

base/iobuffer.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function unsafe_read(from::GenericIOBuffer, p::Ptr{UInt8}, nb::UInt)
149149
from.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
150150
avail = nb_available(from)
151151
adv = min(avail, nb)
152-
@gc_preserve from unsafe_copyto!(p, pointer(from.data, from.ptr), adv)
152+
GC.@preserve from unsafe_copyto!(p, pointer(from.data, from.ptr), adv)
153153
from.ptr += adv
154154
if nb > avail
155155
throw(EOFError())
@@ -164,7 +164,7 @@ function read_sub(from::GenericIOBuffer, a::AbstractArray{T}, offs, nel) where T
164164
end
165165
if isbits(T) && isa(a,Array)
166166
nb = UInt(nel * sizeof(T))
167-
@gc_preserve a unsafe_read(from, pointer(a, offs), nb)
167+
GC.@preserve a unsafe_read(from, pointer(a, offs), nb)
168168
else
169169
for i = offs:offs+nel-1
170170
a[i] = read(to, T)
@@ -395,7 +395,7 @@ function write_sub(to::GenericIOBuffer, a::AbstractArray{UInt8}, offs, nel)
395395
if offs+nel-1 > length(a) || offs < 1 || nel < 0
396396
throw(BoundsError())
397397
end
398-
@gc_preserve a unsafe_write(to, pointer(a, offs), UInt(nel))
398+
GC.@preserve a unsafe_write(to, pointer(a, offs), UInt(nel))
399399
end
400400

401401
@inline function write(to::GenericIOBuffer, a::UInt8)
@@ -428,7 +428,7 @@ read(io::GenericIOBuffer, nb::Integer) = read!(io,StringVector(min(nb, nb_availa
428428

429429
function findfirst(delim::EqualTo{UInt8}, buf::IOBuffer)
430430
p = pointer(buf.data, buf.ptr)
431-
q = @gc_preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim.x,nb_available(buf))
431+
q = GC.@preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim.x,nb_available(buf))
432432
q == C_NULL && return nothing
433433
return Int(q-p+1)
434434
end
@@ -474,7 +474,7 @@ function _crc32c(io::IOBuffer, nb::Integer, crc::UInt32=0x00000000)
474474
io.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
475475
n = min(nb, nb_available(io))
476476
n == 0 && return crc
477-
crc = @gc_preserve io unsafe_crc32c(pointer(io.data, io.ptr), n, crc)
477+
crc = GC.@preserve io unsafe_crc32c(pointer(io.data, io.ptr), n, crc)
478478
io.ptr += n
479479
return crc
480480
end

0 commit comments

Comments
 (0)