1 /* TMP_ALLOC routines for debugging.
3 Copyright 2000, 2001, 2004 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
38 /* This method aims to help a malloc debugger find problems. A linked list
39 of allocated block is kept for TMP_FREE to release. This is reentrant
42 Each TMP_ALLOC is a separate malloced block, so redzones or sentinels
43 applied by a malloc debugger either above or below can guard against
44 accesses outside the allocated area.
46 A marker is a "struct tmp_debug_t *" so that TMP_DECL can initialize it
47 to NULL and we can detect TMP_ALLOC without TMP_MARK.
49 It will work to realloc an MPZ_TMP_INIT variable, but when TMP_FREE comes
50 to release the memory it will have the old size, thereby triggering an
51 error from tests/memory.c.
55 It'd be possible to keep a global list of active "struct tmp_debug_t"
56 records, so at the end of a program any TMP leaks could be printed. But
57 if only a couple of routines are under test at any one time then the
58 likely culprit should be easy enough to spot. */
62 __gmp_tmp_debug_mark (const char *file
, int line
,
63 struct tmp_debug_t
**markp
, struct tmp_debug_t
*mark
,
64 const char *decl_name
, const char *mark_name
)
66 if (strcmp (mark_name
, decl_name
) != 0)
68 __gmp_assert_header (file
, line
);
69 fprintf (stderr
, "GNU MP: TMP_MARK(%s) but TMP_DECL(%s) is in scope\n",
70 mark_name
, decl_name
);
76 __gmp_assert_header (file
, line
);
77 fprintf (stderr
, "GNU MP: Repeat of TMP_MARK(%s)\n", mark_name
);
78 if (mark
->file
!= NULL
&& mark
->file
[0] != '\0' && mark
->line
!= -1)
80 __gmp_assert_header (mark
->file
, mark
->line
);
81 fprintf (stderr
, "previous was here\n");
93 __gmp_tmp_debug_alloc (const char *file
, int line
, int dummy
,
94 struct tmp_debug_t
**markp
,
95 const char *decl_name
, size_t size
)
97 struct tmp_debug_t
*mark
= *markp
;
98 struct tmp_debug_entry_t
*p
;
100 ASSERT_ALWAYS (size
>= 1);
104 __gmp_assert_header (file
, line
);
105 fprintf (stderr
, "GNU MP: TMP_ALLOC without TMP_MARK(%s)\n", decl_name
);
109 p
= __GMP_ALLOCATE_FUNC_TYPE (1, struct tmp_debug_entry_t
);
111 p
->block
= (*__gmp_allocate_func
) (size
);
112 p
->next
= mark
->list
;
118 __gmp_tmp_debug_free (const char *file
, int line
, int dummy
,
119 struct tmp_debug_t
**markp
,
120 const char *decl_name
, const char *free_name
)
122 struct tmp_debug_t
*mark
= *markp
;
123 struct tmp_debug_entry_t
*p
, *next
;
127 __gmp_assert_header (file
, line
);
128 fprintf (stderr
, "GNU MP: TMP_FREE(%s) without TMP_MARK(%s)\n",
129 free_name
, decl_name
);
133 if (strcmp (free_name
, decl_name
) != 0)
135 __gmp_assert_header (file
, line
);
136 fprintf (stderr
, "GNU MP: TMP_FREE(%s) when TMP_DECL(%s) is in scope\n",
137 free_name
, decl_name
);
145 (*__gmp_free_func
) (p
->block
, p
->size
);
146 __GMP_FREE_FUNC_TYPE (p
, 1, struct tmp_debug_entry_t
);