xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/talloc-cache.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1299c6f0cSmrg /* talloc-cache  -- test file concerning memory allocation and cache
2299c6f0cSmrg 
3*ba125506Smrg Copyright 2016-2023 Free Software Foundation, Inc.
4299c6f0cSmrg Contributed by the AriC and Caramba projects, INRIA.
5299c6f0cSmrg 
6299c6f0cSmrg This file is part of the GNU MPFR Library.
7299c6f0cSmrg 
8299c6f0cSmrg The GNU MPFR Library is free software; you can redistribute it and/or modify
9299c6f0cSmrg it under the terms of the GNU Lesser General Public License as published by
10299c6f0cSmrg the Free Software Foundation; either version 3 of the License, or (at your
11299c6f0cSmrg option) any later version.
12299c6f0cSmrg 
13299c6f0cSmrg The GNU MPFR Library is distributed in the hope that it will be useful, but
14299c6f0cSmrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15299c6f0cSmrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16299c6f0cSmrg License for more details.
17299c6f0cSmrg 
18299c6f0cSmrg You should have received a copy of the GNU Lesser General Public License
19299c6f0cSmrg along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
202ba2404bSmrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21299c6f0cSmrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22299c6f0cSmrg 
23299c6f0cSmrg #include <stdlib.h>
24299c6f0cSmrg 
25299c6f0cSmrg #include "mpfr-test.h"
26299c6f0cSmrg 
27299c6f0cSmrg #define A 4096
28299c6f0cSmrg #define I 10000
29299c6f0cSmrg 
30299c6f0cSmrg /* Variable v is used to check that the allocator is the current GMP one.
31299c6f0cSmrg    The assertions on v in would typically fail with MPFR 3.1.6, where the
32299c6f0cSmrg    allocation functions are memorized the first time they are seen. */
33299c6f0cSmrg static int v = 0;
34299c6f0cSmrg 
35299c6f0cSmrg static void *
my_alloc1(size_t s)36299c6f0cSmrg my_alloc1 (size_t s)
37299c6f0cSmrg {
38299c6f0cSmrg   void *p = malloc (s + A);
39*ba125506Smrg   if (p == NULL)
40*ba125506Smrg     abort ();
41299c6f0cSmrg   MPFR_ASSERTN (v == 1);
42299c6f0cSmrg   *(int *) p = 1;
43299c6f0cSmrg   return (void *) ((char *) p + A);
44299c6f0cSmrg }
45299c6f0cSmrg 
46299c6f0cSmrg static void *
my_realloc1(void * p,size_t t,size_t s)47299c6f0cSmrg my_realloc1 (void *p, size_t t, size_t s)
48299c6f0cSmrg {
49299c6f0cSmrg   p = (void *) ((char *) p - A);
50299c6f0cSmrg   MPFR_ASSERTN (v == 1);
51299c6f0cSmrg   MPFR_ASSERTN (*(int *) p == 1);
52299c6f0cSmrg   return (void *) ((char *) realloc (p, s + A) + A);
53299c6f0cSmrg }
54299c6f0cSmrg 
55299c6f0cSmrg static void
my_free1(void * p,size_t t)56299c6f0cSmrg my_free1 (void *p, size_t t)
57299c6f0cSmrg {
58299c6f0cSmrg   p = (void *) ((char *) p - A);
59299c6f0cSmrg   MPFR_ASSERTN (v == 1);
60299c6f0cSmrg   MPFR_ASSERTN (*(int *) p == 1);
61299c6f0cSmrg   free (p);
62299c6f0cSmrg }
63299c6f0cSmrg 
64299c6f0cSmrg static void *
my_alloc2(size_t s)65299c6f0cSmrg my_alloc2 (size_t s)
66299c6f0cSmrg {
67299c6f0cSmrg   void *p = malloc (s + A);
68*ba125506Smrg   if (p == NULL)
69*ba125506Smrg     abort ();
70299c6f0cSmrg   MPFR_ASSERTN (v == 2);
71299c6f0cSmrg   *(int *) p = 2;
72299c6f0cSmrg   return (void *) ((char *) p + A);
73299c6f0cSmrg }
74299c6f0cSmrg 
75299c6f0cSmrg static void *
my_realloc2(void * p,size_t t,size_t s)76299c6f0cSmrg my_realloc2 (void *p, size_t t, size_t s)
77299c6f0cSmrg {
78299c6f0cSmrg   p = (void *) ((char *) p - A);
79299c6f0cSmrg   MPFR_ASSERTN (v == 2);
80299c6f0cSmrg   MPFR_ASSERTN (*(int *) p == 2);
81299c6f0cSmrg   return (void *) ((char *) realloc (p, s + A) + A);
82299c6f0cSmrg }
83299c6f0cSmrg 
84299c6f0cSmrg static void
my_free2(void * p,size_t t)85299c6f0cSmrg my_free2 (void *p, size_t t)
86299c6f0cSmrg {
87299c6f0cSmrg   p = (void *) ((char *) p - A);
88299c6f0cSmrg   MPFR_ASSERTN (v == 2);
89299c6f0cSmrg   MPFR_ASSERTN (*(int *) p == 2);
90299c6f0cSmrg   free (p);
91299c6f0cSmrg }
92299c6f0cSmrg 
93299c6f0cSmrg int
main(void)94299c6f0cSmrg main (void)
95299c6f0cSmrg {
96299c6f0cSmrg   mpfr_t x;
97299c6f0cSmrg   int err;
98299c6f0cSmrg 
99299c6f0cSmrg   tests_memory_disabled = 2;
100299c6f0cSmrg   tests_start_mpfr ();
101299c6f0cSmrg 
102299c6f0cSmrg   err = mpfr_mp_memory_cleanup ();
103299c6f0cSmrg   MPFR_ASSERTN (err == 0);
104299c6f0cSmrg   mp_set_memory_functions (my_alloc1, my_realloc1, my_free1);
105299c6f0cSmrg   v = 1;
106299c6f0cSmrg 
107299c6f0cSmrg   mpfr_init2 (x, 53);
108299c6f0cSmrg   mpfr_set_ui (x, I, MPFR_RNDN);
109299c6f0cSmrg   mpfr_sin (x, x, MPFR_RNDN);
110299c6f0cSmrg   mpfr_clear (x);
111299c6f0cSmrg 
112299c6f0cSmrg   err = mpfr_mp_memory_cleanup ();
113299c6f0cSmrg   MPFR_ASSERTN (err == 0);
114299c6f0cSmrg   mp_set_memory_functions (my_alloc2, my_realloc2, my_free2);
115299c6f0cSmrg   v = 2;
116299c6f0cSmrg 
117299c6f0cSmrg   mpfr_init2 (x, 1000);
118299c6f0cSmrg   mpfr_set_ui (x, I, MPFR_RNDN);
119299c6f0cSmrg   mpfr_sin (x, x, MPFR_RNDN);
120299c6f0cSmrg   mpfr_clear (x);
121299c6f0cSmrg 
122299c6f0cSmrg   tests_end_mpfr ();
123299c6f0cSmrg   return 0;
124299c6f0cSmrg }
125