Home | History | Annotate | Line # | Download | only in src
      1 /* mpfr_free_cache... - Free cache/pool memory used by MPFR.
      2 
      3 Copyright 2004-2023 Free Software Foundation, Inc.
      4 Contributed by the AriC and Caramba projects, INRIA.
      5 
      6 This file is part of the GNU MPFR Library.
      7 
      8 The GNU MPFR Library is free software; you can redistribute it and/or modify
      9 it under the terms of the GNU Lesser General Public License as published by
     10 the Free Software Foundation; either version 3 of the License, or (at your
     11 option) any later version.
     12 
     13 The GNU MPFR Library is distributed in the hope that it will be useful, but
     14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16 License for more details.
     17 
     18 You should have received a copy of the GNU Lesser General Public License
     19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22 
     23 #include "mpfr-impl.h"
     24 
     25 /* These caches may be global to all threads or local to the current one. */
     26 static void
     27 mpfr_free_const_caches (void)
     28 {
     29 #ifndef MPFR_USE_LOGGING
     30   mpfr_clear_cache (__gmpfr_cache_const_pi);
     31   mpfr_clear_cache (__gmpfr_cache_const_log2);
     32 #else
     33   mpfr_clear_cache (__gmpfr_normal_pi);
     34   mpfr_clear_cache (__gmpfr_normal_log2);
     35   mpfr_clear_cache (__gmpfr_logging_pi);
     36   mpfr_clear_cache (__gmpfr_logging_log2);
     37 #endif
     38   mpfr_clear_cache (__gmpfr_cache_const_euler);
     39   mpfr_clear_cache (__gmpfr_cache_const_catalan);
     40 }
     41 
     42 /* These caches/pools are always local to a thread. */
     43 static void
     44 mpfr_free_local_cache (void)
     45 {
     46   /* Before freeing the mpz_t pool, we need to free any cache of
     47      mpz_t numbers, since freeing such a cache may add entries to
     48      the mpz_t pool. */
     49   mpfr_bernoulli_freecache ();
     50   mpfr_free_pool ();
     51 }
     52 
     53 void
     54 mpfr_free_cache (void)
     55 {
     56   mpfr_free_local_cache ();
     57   mpfr_free_const_caches ();
     58 }
     59 
     60 void
     61 mpfr_free_cache2 (mpfr_free_cache_t way)
     62 {
     63   if ((unsigned int) way & MPFR_FREE_LOCAL_CACHE)
     64     {
     65       mpfr_free_local_cache ();
     66 #if !defined(MPFR_WANT_SHARED_CACHE)
     67       mpfr_free_const_caches ();
     68 #endif
     69     }
     70   if ((unsigned int) way & MPFR_FREE_GLOBAL_CACHE)
     71     {
     72 #if defined(MPFR_WANT_SHARED_CACHE)
     73       mpfr_free_const_caches ();
     74 #endif
     75     }
     76 }
     77 
     78 /* Function an application should call before mp_set_memory_functions().
     79    This is currently equivalent to freeing the caches (and pools) since
     80    they are allocated with GMP's current allocator. But this might change
     81    in the future to avoid the drawback of having to free the caches just
     82    because the allocators are changed: the caches could optionally be
     83    allocated with malloc().
     84    This function returns 0 in case of success, non-zero in case of error.
     85    Errors are currently not possible. But let's avoid a prototype change
     86    in the future, in case errors would be possible. */
     87 int
     88 mpfr_mp_memory_cleanup (void)
     89 {
     90   mpfr_free_cache ();
     91   return 0;
     92 }
     93