(root)/
mpfr-4.2.1/
tests/
talloc-cache.c
       1  /* talloc-cache  -- test file concerning memory allocation and cache
       2  
       3  Copyright 2016-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 <stdlib.h>
      24  
      25  #include "mpfr-test.h"
      26  
      27  #define A 4096
      28  #define I 10000
      29  
      30  /* Variable v is used to check that the allocator is the current GMP one.
      31     The assertions on v in would typically fail with MPFR 3.1.6, where the
      32     allocation functions are memorized the first time they are seen. */
      33  static int v = 0;
      34  
      35  static void *
      36  my_alloc1 (size_t s)
      37  {
      38    void *p = malloc (s + A);
      39    if (p == NULL)
      40      abort ();
      41    MPFR_ASSERTN (v == 1);
      42    *(int *) p = 1;
      43    return (void *) ((char *) p + A);
      44  }
      45  
      46  static void *
      47  my_realloc1 (void *p, size_t t, size_t s)
      48  {
      49    p = (void *) ((char *) p - A);
      50    MPFR_ASSERTN (v == 1);
      51    MPFR_ASSERTN (*(int *) p == 1);
      52    return (void *) ((char *) realloc (p, s + A) + A);
      53  }
      54  
      55  static void
      56  my_free1 (void *p, size_t t)
      57  {
      58    p = (void *) ((char *) p - A);
      59    MPFR_ASSERTN (v == 1);
      60    MPFR_ASSERTN (*(int *) p == 1);
      61    free (p);
      62  }
      63  
      64  static void *
      65  my_alloc2 (size_t s)
      66  {
      67    void *p = malloc (s + A);
      68    if (p == NULL)
      69      abort ();
      70    MPFR_ASSERTN (v == 2);
      71    *(int *) p = 2;
      72    return (void *) ((char *) p + A);
      73  }
      74  
      75  static void *
      76  my_realloc2 (void *p, size_t t, size_t s)
      77  {
      78    p = (void *) ((char *) p - A);
      79    MPFR_ASSERTN (v == 2);
      80    MPFR_ASSERTN (*(int *) p == 2);
      81    return (void *) ((char *) realloc (p, s + A) + A);
      82  }
      83  
      84  static void
      85  my_free2 (void *p, size_t t)
      86  {
      87    p = (void *) ((char *) p - A);
      88    MPFR_ASSERTN (v == 2);
      89    MPFR_ASSERTN (*(int *) p == 2);
      90    free (p);
      91  }
      92  
      93  int
      94  main (void)
      95  {
      96    mpfr_t x;
      97    int err;
      98  
      99    tests_memory_disabled = 2;
     100    tests_start_mpfr ();
     101  
     102    err = mpfr_mp_memory_cleanup ();
     103    MPFR_ASSERTN (err == 0);
     104    mp_set_memory_functions (my_alloc1, my_realloc1, my_free1);
     105    v = 1;
     106  
     107    mpfr_init2 (x, 53);
     108    mpfr_set_ui (x, I, MPFR_RNDN);
     109    mpfr_sin (x, x, MPFR_RNDN);
     110    mpfr_clear (x);
     111  
     112    err = mpfr_mp_memory_cleanup ();
     113    MPFR_ASSERTN (err == 0);
     114    mp_set_memory_functions (my_alloc2, my_realloc2, my_free2);
     115    v = 2;
     116  
     117    mpfr_init2 (x, 1000);
     118    mpfr_set_ui (x, I, MPFR_RNDN);
     119    mpfr_sin (x, x, MPFR_RNDN);
     120    mpfr_clear (x);
     121  
     122    tests_end_mpfr ();
     123    return 0;
     124  }