tizen 2.3 release
[external/gmp.git] / tal-debug.c
1 /* TMP_ALLOC routines for debugging.
2
3 Copyright 2000, 2001, 2004 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
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.
11
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.
16
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. */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "gmp.h"
26 #include "gmp-impl.h"
27
28
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
31    and thread safe.
32
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.
36
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.
39
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.
43
44    Possibilities:
45
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.  */
50
51
52 void
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)
56 {
57   if (strcmp (mark_name, decl_name) != 0)
58     {
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);
62       abort ();
63     }
64
65   if (*markp != NULL)
66     {
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)
70         {
71           __gmp_assert_header (mark->file, mark->line);
72           fprintf (stderr, "previous was here\n");
73         }
74       abort ();
75     }
76
77   *markp = mark;
78   mark->file = file;
79   mark->line = line;
80   mark->list = NULL;
81 }
82
83 void *
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)
87 {
88   struct tmp_debug_t        *mark = *markp;
89   struct tmp_debug_entry_t  *p;
90
91   ASSERT_ALWAYS (size >= 1);
92
93   if (mark == NULL)
94     {
95       __gmp_assert_header (file, line);
96       fprintf (stderr, "GNU MP: TMP_ALLOC without TMP_MARK(%s)\n", decl_name);
97       abort ();
98     }
99
100   p = __GMP_ALLOCATE_FUNC_TYPE (1, struct tmp_debug_entry_t);
101   p->size = size;
102   p->block = (*__gmp_allocate_func) (size);
103   p->next = mark->list;
104   mark->list = p;
105   return p->block;
106 }
107
108 void
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)
112 {
113   struct tmp_debug_t        *mark = *markp;
114   struct tmp_debug_entry_t  *p, *next;
115
116   if (mark == NULL)
117     {
118       __gmp_assert_header (file, line);
119       fprintf (stderr, "GNU MP: TMP_FREE(%s) without TMP_MARK(%s)\n",
120                free_name, decl_name);
121       abort ();
122     }
123
124   if (strcmp (free_name, decl_name) != 0)
125     {
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);
129       abort ();
130     }
131
132   p = mark->list;
133   while (p != NULL)
134     {
135       next = p->next;
136       (*__gmp_free_func) (p->block, p->size);
137       __GMP_FREE_FUNC_TYPE (p, 1, struct tmp_debug_entry_t);
138       p = next;
139     }
140
141   *markp = NULL;
142 }