xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/talloc-cache.c (revision 9fb66d812c00ebfb445c0b47dea128f32aa6fe96)
1 /* talloc-cache  -- test file concerning memory allocation and cache
2 
3 Copyright 2016-2020 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   MPFR_ASSERTN (v == 1);
40   *(int *) p = 1;
41   return (void *) ((char *) p + A);
42 }
43 
44 static void *
45 my_realloc1 (void *p, size_t t, size_t s)
46 {
47   p = (void *) ((char *) p - A);
48   MPFR_ASSERTN (v == 1);
49   MPFR_ASSERTN (*(int *) p == 1);
50   return (void *) ((char *) realloc (p, s + A) + A);
51 }
52 
53 static void
54 my_free1 (void *p, size_t t)
55 {
56   p = (void *) ((char *) p - A);
57   MPFR_ASSERTN (v == 1);
58   MPFR_ASSERTN (*(int *) p == 1);
59   free (p);
60 }
61 
62 static void *
63 my_alloc2 (size_t s)
64 {
65   void *p = malloc (s + A);
66   MPFR_ASSERTN (v == 2);
67   *(int *) p = 2;
68   return (void *) ((char *) p + A);
69 }
70 
71 static void *
72 my_realloc2 (void *p, size_t t, size_t s)
73 {
74   p = (void *) ((char *) p - A);
75   MPFR_ASSERTN (v == 2);
76   MPFR_ASSERTN (*(int *) p == 2);
77   return (void *) ((char *) realloc (p, s + A) + A);
78 }
79 
80 static void
81 my_free2 (void *p, size_t t)
82 {
83   p = (void *) ((char *) p - A);
84   MPFR_ASSERTN (v == 2);
85   MPFR_ASSERTN (*(int *) p == 2);
86   free (p);
87 }
88 
89 int
90 main (void)
91 {
92   mpfr_t x;
93   int err;
94 
95   tests_memory_disabled = 2;
96   tests_start_mpfr ();
97 
98   err = mpfr_mp_memory_cleanup ();
99   MPFR_ASSERTN (err == 0);
100   mp_set_memory_functions (my_alloc1, my_realloc1, my_free1);
101   v = 1;
102 
103   mpfr_init2 (x, 53);
104   mpfr_set_ui (x, I, MPFR_RNDN);
105   mpfr_sin (x, x, MPFR_RNDN);
106   mpfr_clear (x);
107 
108   err = mpfr_mp_memory_cleanup ();
109   MPFR_ASSERTN (err == 0);
110   mp_set_memory_functions (my_alloc2, my_realloc2, my_free2);
111   v = 2;
112 
113   mpfr_init2 (x, 1000);
114   mpfr_set_ui (x, I, MPFR_RNDN);
115   mpfr_sin (x, x, MPFR_RNDN);
116   mpfr_clear (x);
117 
118   tests_end_mpfr ();
119   return 0;
120 }
121