summaryrefslogtreecommitdiff
path: root/tests/talloc-cache.c
blob: fba89bb25beff466d8818299908c5ebcfd7109bf (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
/* talloc-cache  -- test file concerning memory allocation and cache

Copyright 2016-2017 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
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

/* This test program is for temporary testing only. It is not in the
   usual test suite (not in check_PROGRAMS from Makefile.am).
   In the 3.1 branch, it crashes before r9467, and r9467 fixes the crash
   (among other issues).
   In the trunk, it crashes, which is not expected with the current code.
*/

#include <stdlib.h>

#include "mpfr-test.h"

#define A 4096
#define I 10000

static void *
my_alloc1 (size_t s)
{
return malloc (s);
}

static void *
my_realloc1 (void *p, size_t t, size_t s)
{
return realloc (p, s);
}

static void
my_free1 (void *p, size_t t)
{
  return free (p);
}

static void *
my_alloc2 (size_t s)
{
  return (void *) ((char *) malloc (s + A) + A);
}

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

static void
my_free2 (void *p, size_t t)
{
  return free ((char *) p - A);
}

int
main (void)
{
  mpfr_t x;

  mp_set_memory_functions (my_alloc1, my_realloc1, my_free1);

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

  mp_set_memory_functions (my_alloc2, my_realloc2, my_free2);

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

  return 0;
}