summaryrefslogtreecommitdiff
path: root/tests/talloc-cache.c
blob: c889b10cd829e48c905d200c60ce7fe8894578a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* talloc-cache  -- test file concerning memory allocation and cache

Copyright 2016-2021 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.

This file is part of the GNU MPFR Library.

The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The GNU MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

#include <stdlib.h>

#include "mpfr-test.h"

#define A 4096
#define I 10000

/* Variable v is used to check that the allocator is the current GMP one.
   The assertions on v in would typically fail with MPFR 3.1.6, where the
   allocation functions are memorized the first time they are seen. */
static int v = 0;

static void *
my_alloc1 (size_t s)
{
  void *p = malloc (s + A);
  if (p == NULL)
    abort ();
  MPFR_ASSERTN (v == 1);
  *(int *) p = 1;
  return (void *) ((char *) p + A);
}

static void *
my_realloc1 (void *p, size_t t, size_t s)
{
  p = (void *) ((char *) p - A);
  MPFR_ASSERTN (v == 1);
  MPFR_ASSERTN (*(int *) p == 1);
  return (void *) ((char *) realloc (p, s + A) + A);
}

static void
my_free1 (void *p, size_t t)
{
  p = (void *) ((char *) p - A);
  MPFR_ASSERTN (v == 1);
  MPFR_ASSERTN (*(int *) p == 1);
  free (p);
}

static void *
my_alloc2 (size_t s)
{
  void *p = malloc (s + A);
  if (p == NULL)
    abort ();
  MPFR_ASSERTN (v == 2);
  *(int *) p = 2;
  return (void *) ((char *) p + A);
}

static void *
my_realloc2 (void *p, size_t t, size_t s)
{
  p = (void *) ((char *) p - A);
  MPFR_ASSERTN (v == 2);
  MPFR_ASSERTN (*(int *) p == 2);
  return (void *) ((char *) realloc (p, s + A) + A);
}

static void
my_free2 (void *p, size_t t)
{
  p = (void *) ((char *) p - A);
  MPFR_ASSERTN (v == 2);
  MPFR_ASSERTN (*(int *) p == 2);
  free (p);
}

int
main (void)
{
  mpfr_t x;
  int err;

  tests_memory_disabled = 2;
  tests_start_mpfr ();

  err = mpfr_mp_memory_cleanup ();
  MPFR_ASSERTN (err == 0);
  mp_set_memory_functions (my_alloc1, my_realloc1, my_free1);
  v = 1;

  mpfr_init2 (x, 53);
  mpfr_set_ui (x, I, MPFR_RNDN);
  mpfr_sin (x, x, MPFR_RNDN);
  mpfr_clear (x);

  err = mpfr_mp_memory_cleanup ();
  MPFR_ASSERTN (err == 0);
  mp_set_memory_functions (my_alloc2, my_realloc2, my_free2);
  v = 2;

  mpfr_init2 (x, 1000);
  mpfr_set_ui (x, I, MPFR_RNDN);
  mpfr_sin (x, x, MPFR_RNDN);
  mpfr_clear (x);

  tests_end_mpfr ();
  return 0;
}