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 3 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. If not, see http://www.gnu.org/licenses/. */
27 /* This method aims to help a malloc debugger find problems. A linked list
28 of allocated block is kept for TMP_FREE to release. This is reentrant
31 Each TMP_ALLOC is a separate malloced block, so redzones or sentinels
32 applied by a malloc debugger either above or below can guard against
33 accesses outside the allocated area.
35 A marker is a "struct tmp_debug_t *" so that TMP_DECL can initialize it
36 to NULL and we can detect TMP_ALLOC without TMP_MARK.
38 It will work to realloc an MPZ_TMP_INIT variable, but when TMP_FREE comes
39 to release the memory it will have the old size, thereby triggering an
40 error from tests/memory.c.
44 It'd be possible to keep a global list of active "struct tmp_debug_t"
45 records, so at the end of a program any TMP leaks could be printed. But
46 if only a couple of routines are under test at any one time then the
47 likely culprit should be easy enough to spot. */
51 __gmp_tmp_debug_mark (const char *file, int line,
52 struct tmp_debug_t **markp, struct tmp_debug_t *mark,
53 const char *decl_name, const char *mark_name)
55 if (strcmp (mark_name, decl_name) != 0)
57 __gmp_assert_header (file, line);
58 fprintf (stderr, "GNU MP: TMP_MARK(%s) but TMP_DECL(%s) is in scope\n",
59 mark_name, decl_name);
65 __gmp_assert_header (file, line);
66 fprintf (stderr, "GNU MP: Repeat of TMP_MARK(%s)\n", mark_name);
67 if (mark->file != NULL && mark->file[0] != '\0' && mark->line != -1)
69 __gmp_assert_header (mark->file, mark->line);
70 fprintf (stderr, "previous was here\n");
82 __gmp_tmp_debug_alloc (const char *file, int line, int dummy,
83 struct tmp_debug_t **markp,
84 const char *decl_name, size_t size)
86 struct tmp_debug_t *mark = *markp;
87 struct tmp_debug_entry_t *p;
89 ASSERT_ALWAYS (size >= 1);
93 __gmp_assert_header (file, line);
94 fprintf (stderr, "GNU MP: TMP_ALLOC without TMP_MARK(%s)\n", decl_name);
98 p = __GMP_ALLOCATE_FUNC_TYPE (1, struct tmp_debug_entry_t);
100 p->block = (*__gmp_allocate_func) (size);
101 p->next = mark->list;
107 __gmp_tmp_debug_free (const char *file, int line, int dummy,
108 struct tmp_debug_t **markp,
109 const char *decl_name, const char *free_name)
111 struct tmp_debug_t *mark = *markp;
112 struct tmp_debug_entry_t *p, *next;
116 __gmp_assert_header (file, line);
117 fprintf (stderr, "GNU MP: TMP_FREE(%s) without TMP_MARK(%s)\n",
118 free_name, decl_name);
122 if (strcmp (free_name, decl_name) != 0)
124 __gmp_assert_header (file, line);
125 fprintf (stderr, "GNU MP: TMP_FREE(%s) when TMP_DECL(%s) is in scope\n",
126 free_name, decl_name);
134 (*__gmp_free_func) (p->block, p->size);
135 __GMP_FREE_FUNC_TYPE (p, 1, struct tmp_debug_entry_t);