1 | /* Memory allocation used during tests. |
---|
2 | |
---|
3 | Copyright 2001, 2002 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., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> /* for abort */ |
---|
24 | #include "gmp.h" |
---|
25 | #include "gmp-impl.h" |
---|
26 | |
---|
27 | |
---|
28 | /* Each block allocated is a separate malloc, for the benefit of a redzoning |
---|
29 | malloc debugger during development or when bug hunting. |
---|
30 | |
---|
31 | Sizes passed when reallocating or freeing are checked (the default |
---|
32 | routines don't care about these). |
---|
33 | |
---|
34 | Memory leaks are checked by requiring that all blocks have been freed |
---|
35 | when tests_memory_end() is called. Test programs must be sure to have |
---|
36 | "clear"s for all temporary variables used. */ |
---|
37 | |
---|
38 | |
---|
39 | struct header { |
---|
40 | void *ptr; |
---|
41 | size_t size; |
---|
42 | struct header *next; |
---|
43 | }; |
---|
44 | |
---|
45 | struct header *tests_memory_list = NULL; |
---|
46 | |
---|
47 | /* Return a pointer to a pointer to the found block (so it can be updated |
---|
48 | when unlinking). */ |
---|
49 | struct header ** |
---|
50 | tests_memory_find (void *ptr) |
---|
51 | { |
---|
52 | struct header **hp; |
---|
53 | |
---|
54 | for (hp = &tests_memory_list; *hp != NULL; hp = &((*hp)->next)) |
---|
55 | if ((*hp)->ptr == ptr) |
---|
56 | return hp; |
---|
57 | |
---|
58 | return NULL; |
---|
59 | } |
---|
60 | |
---|
61 | int |
---|
62 | tests_memory_valid (void *ptr) |
---|
63 | { |
---|
64 | return (tests_memory_find (ptr) != NULL); |
---|
65 | } |
---|
66 | |
---|
67 | void * |
---|
68 | tests_allocate (size_t size) |
---|
69 | { |
---|
70 | struct header *h; |
---|
71 | |
---|
72 | if (size == 0) |
---|
73 | { |
---|
74 | printf ("tests_allocate(): attempt to allocate 0 bytes\n"); |
---|
75 | abort (); |
---|
76 | } |
---|
77 | |
---|
78 | h = (struct header *) __gmp_default_allocate (sizeof (*h)); |
---|
79 | h->next = tests_memory_list; |
---|
80 | tests_memory_list = h; |
---|
81 | |
---|
82 | h->size = size; |
---|
83 | h->ptr = __gmp_default_allocate (size); |
---|
84 | return h->ptr; |
---|
85 | } |
---|
86 | |
---|
87 | void * |
---|
88 | tests_reallocate (void *ptr, size_t old_size, size_t new_size) |
---|
89 | { |
---|
90 | struct header **hp, *h; |
---|
91 | |
---|
92 | if (new_size == 0) |
---|
93 | { |
---|
94 | printf ("tests_reallocate(): attempt to reallocate 0x%lX to 0 bytes\n", |
---|
95 | (unsigned long) ptr); |
---|
96 | abort (); |
---|
97 | } |
---|
98 | |
---|
99 | hp = tests_memory_find (ptr); |
---|
100 | if (hp == NULL) |
---|
101 | { |
---|
102 | printf ("tests_reallocate(): attempt to reallocate bad pointer 0x%lX\n", |
---|
103 | (unsigned long) ptr); |
---|
104 | abort (); |
---|
105 | } |
---|
106 | h = *hp; |
---|
107 | |
---|
108 | if (h->size != old_size) |
---|
109 | { |
---|
110 | printf ("tests_reallocate(): bad old size %u, should be %u\n", |
---|
111 | old_size, h->size); |
---|
112 | abort (); |
---|
113 | } |
---|
114 | |
---|
115 | h->size = new_size; |
---|
116 | h->ptr = __gmp_default_reallocate (ptr, old_size, new_size); |
---|
117 | return h->ptr; |
---|
118 | } |
---|
119 | |
---|
120 | struct header ** |
---|
121 | tests_free_find (void *ptr) |
---|
122 | { |
---|
123 | struct header **hp = tests_memory_find (ptr); |
---|
124 | if (hp == NULL) |
---|
125 | { |
---|
126 | printf ("tests_free(): attempt to free bad pointer 0x%lX\n", |
---|
127 | (unsigned long) ptr); |
---|
128 | abort (); |
---|
129 | } |
---|
130 | return hp; |
---|
131 | } |
---|
132 | |
---|
133 | void |
---|
134 | tests_free_nosize (void *ptr) |
---|
135 | { |
---|
136 | struct header **hp = tests_free_find (ptr); |
---|
137 | struct header *h = *hp; |
---|
138 | |
---|
139 | *hp = h->next; /* unlink */ |
---|
140 | |
---|
141 | __gmp_default_free (ptr, h->size); |
---|
142 | __gmp_default_free (h, sizeof (*h)); |
---|
143 | } |
---|
144 | |
---|
145 | void |
---|
146 | tests_free (void *ptr, size_t size) |
---|
147 | { |
---|
148 | struct header **hp = tests_free_find (ptr); |
---|
149 | struct header *h = *hp; |
---|
150 | |
---|
151 | if (h->size != size) |
---|
152 | { |
---|
153 | printf ("tests_free(): bad size %u, should be %u\n", size, h->size); |
---|
154 | abort (); |
---|
155 | } |
---|
156 | |
---|
157 | tests_free_nosize (ptr); |
---|
158 | } |
---|
159 | |
---|
160 | void |
---|
161 | tests_memory_start (void) |
---|
162 | { |
---|
163 | mp_set_memory_functions (tests_allocate, tests_reallocate, tests_free); |
---|
164 | } |
---|
165 | |
---|
166 | void |
---|
167 | tests_memory_end (void) |
---|
168 | { |
---|
169 | if (tests_memory_list != NULL) |
---|
170 | { |
---|
171 | struct header *h; |
---|
172 | unsigned count; |
---|
173 | |
---|
174 | printf ("tests_memory_end(): not all memory freed\n"); |
---|
175 | |
---|
176 | count = 0; |
---|
177 | for (h = tests_memory_list; h != NULL; h = h->next) |
---|
178 | count++; |
---|
179 | |
---|
180 | printf (" %u blocks remaining\n", count); |
---|
181 | abort (); |
---|
182 | } |
---|
183 | } |
---|