Skip to content

Commit 0f30900

Browse files
authored
Merge pull request #17010 from JuliaLang/yyc/global-init
Lazy initialize gmp and mpfr finalizers
2 parents 01e3c8a + 9e611a7 commit 0f30900

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

base/gmp.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ type BigInt <: Integer
4444
function BigInt()
4545
b = new(zero(Cint), zero(Cint), C_NULL)
4646
ccall((:__gmpz_init,:libgmp), Void, (Ptr{BigInt},), &b)
47-
finalizer(b, _gmp_clear_func)
47+
finalizer(b, cglobal((:__gmpz_clear, :libgmp)))
4848
return b
4949
end
5050
end
5151

52-
_gmp_clear_func = C_NULL
53-
_mpfr_clear_func = C_NULL
54-
5552
function __init__()
5653
try
5754
if gmp_version().major != GMP_VERSION.major || gmp_bits_per_limb() != GMP_BITS_PER_LIMB
@@ -60,8 +57,6 @@ function __init__()
6057
"Please rebuild Julia."))
6158
end
6259

63-
global _gmp_clear_func = cglobal((:__gmpz_clear, :libgmp))
64-
global _mpfr_clear_func = cglobal((:mpfr_clear, :libmpfr))
6560
ccall((:__gmp_set_memory_functions, :libgmp), Void,
6661
(Ptr{Void},Ptr{Void},Ptr{Void}),
6762
cglobal(:jl_gc_counted_malloc),

base/mpfr.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type BigFloat <: AbstractFloat
5151
N = precision(BigFloat)
5252
z = new(zero(Clong), zero(Cint), zero(Clong), C_NULL)
5353
ccall((:mpfr_init2,:libmpfr), Void, (Ptr{BigFloat}, Clong), &z, N)
54-
finalizer(z, Base.GMP._mpfr_clear_func)
54+
finalizer(z, cglobal((:mpfr_clear, :libmpfr)))
5555
return z
5656
end
5757
# Not recommended for general use
@@ -887,7 +887,7 @@ function Base.deepcopy_internal(x::BigFloat, stackdict::ObjectIdDict)
887887
N = precision(x)
888888
y = BigFloat(zero(Clong), zero(Cint), zero(Clong), C_NULL)
889889
ccall((:mpfr_init2,:libmpfr), Void, (Ptr{BigFloat}, Clong), &y, N)
890-
finalizer(y, Base.GMP._mpfr_clear_func)
890+
finalizer(y, cglobal((:mpfr_clear, :libmpfr)))
891891
ccall((:mpfr_set, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Int32),
892892
&y, &x, ROUNDING_MODE[end])
893893
stackdict[x] = y

0 commit comments

Comments
 (0)