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 the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA. */
29 /* This method aims to help a malloc debugger find problems. A linked list
30 of allocated block is kept for TMP_FREE to release. This is reentrant
33 Each TMP_ALLOC is a separate malloced block, so redzones or sentinels
34 applied by a malloc debugger either above or below can guard against
35 accesses outside the allocated area.
37 A marker is a "struct tmp_debug_t *" so that TMP_DECL can initialize it
38 to NULL and we can detect TMP_ALLOC without TMP_MARK.
40 It will work to realloc an MPZ_TMP_INIT variable, but when TMP_FREE comes
41 to release the memory it will have the old size, thereby triggering an
42 error from tests/memory.c.
46 It'd be possible to keep a global list of active "struct tmp_debug_t"
47 records, so at the end of a program any TMP leaks could be printed. But
48 if only a couple of routines are under test at any one time then the
49 likely culprit should be easy enough to spot. */
53 __gmp_tmp_debug_mark (const char *file, int line,
54 struct tmp_debug_t **markp, struct tmp_debug_t *mark,
55 const char *decl_name, const char *mark_name)
57 if (strcmp (mark_name, decl_name) != 0)
59 __gmp_assert_header (file, line);
60 fprintf (stderr, "GNU MP: TMP_MARK(%s) but TMP_DECL(%s) is in scope\n",
61 mark_name, decl_name);
67 __gmp_assert_header (file, line);
68 fprintf (stderr, "GNU MP: Repeat of TMP_MARK(%s)\n", mark_name);
69 if (mark->file != NULL && mark->file[0] != '\0' && mark->line != -1)
71 __gmp_assert_header (mark->file, mark->line);
72 fprintf (stderr, "previous was here\n");
84 __gmp_tmp_debug_alloc (const char *file, int line, int dummy,
85 struct tmp_debug_t **markp,
86 const char *decl_name, size_t size)
88 struct tmp_debug_t *mark = *markp;
89 struct tmp_debug_entry_t *p;
91 ASSERT_ALWAYS (size >= 1);
95 __gmp_assert_header (file, line);
96 fprintf (stderr, "GNU MP: TMP_ALLOC without TMP_MARK(%s)\n", decl_name);
100 p = __GMP_ALLOCATE_FUNC_TYPE (1, struct tmp_debug_entry_t);
102 p->block = (*__gmp_allocate_func) (size);
103 p->next = mark->list;
109 __gmp_tmp_debug_free (const char *file, int line, int dummy,
110 struct tmp_debug_t **markp,
111 const char *decl_name, const char *free_name)
113 struct tmp_debug_t *mark = *markp;
114 struct tmp_debug_entry_t *p, *next;
118 __gmp_assert_header (file, line);
119 fprintf (stderr, "GNU MP: TMP_FREE(%s) without TMP_MARK(%s)\n",
120 free_name, decl_name);
124 if (strcmp (free_name, decl_name) != 0)
126 __gmp_assert_header (file, line);
127 fprintf (stderr, "GNU MP: TMP_FREE(%s) when TMP_DECL(%s) is in scope\n",
128 free_name, decl_name);
136 (*__gmp_free_func) (p->block, p->size);
137 __GMP_FREE_FUNC_TYPE (p, 1, struct tmp_debug_entry_t);