Commit | Line | Data |
---|---|---|
dbb605f5 | 1 | /* Copyright (C) 2000, 2006, 2008 Free Software Foundation, Inc. |
f1322139 | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
f1322139 | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
f1322139 | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
f1322139 | 18 | |
dbb605f5 LC |
19 | #ifdef HAVE_CONFIG_H |
20 | # include <config.h> | |
21 | #endif | |
22 | ||
f1322139 | 23 | #include <string.h> |
67e8151b | 24 | #include <stdio.h> |
f1322139 | 25 | |
a0599745 MD |
26 | #include "libguile/_scm.h" |
27 | #include "libguile/alist.h" | |
28 | #include "libguile/strings.h" | |
f1322139 | 29 | |
0e850825 | 30 | #include "libguile/debug-malloc.h" |
f1322139 MD |
31 | |
32 | /* | |
eb422a4c MD |
33 | * The following code is a hack which I wrote quickly in order to |
34 | * solve a memory leak problem. Since I wanted to have the | |
35 | * application running at close to normal speed, I prioritized speed | |
36 | * over maintainability. /mdj | |
f1322139 MD |
37 | */ |
38 | ||
39 | typedef struct hash_entry { | |
40 | const void *key; | |
41 | const void *data; | |
42 | } hash_entry_t; | |
43 | ||
44 | #define N_SEEK 8 | |
45 | ||
46 | static int malloc_type_size = 31; | |
47 | static hash_entry_t *malloc_type = 0; | |
0e850825 | 48 | static int malloc_object_size = 8191; |
f1322139 MD |
49 | static hash_entry_t *malloc_object = 0; |
50 | ||
51 | #define TABLE(table) malloc_ ## table | |
52 | #define SIZE(table) malloc_ ## table ## _size | |
53 | #define HASH(table, key) \ | |
54 | &TABLE (table)[((unsigned long) key >> 4UL) * 2654435761UL % SIZE (table)] | |
55 | ||
56 | #define CREATE_HASH_ENTRY_AT(entry, table, h, k, done) \ | |
57 | { \ | |
58 | int i; \ | |
59 | do \ | |
60 | { \ | |
61 | for (i = 0; i < N_SEEK; ++i) \ | |
62 | if (h[i].key == 0) \ | |
63 | goto done; \ | |
64 | grow (&TABLE (table), &SIZE (table)); \ | |
65 | h = HASH (table, k); \ | |
66 | } \ | |
67 | while (1); \ | |
68 | done: \ | |
69 | (entry) = &h[i]; \ | |
70 | } | |
71 | ||
72 | #define CREATE_HASH_ENTRY(table, k, d, done) \ | |
73 | do \ | |
74 | { \ | |
75 | hash_entry_t *h = HASH (table, k); \ | |
76 | hash_entry_t *entry; \ | |
77 | CREATE_HASH_ENTRY_AT (entry, table, h, k, done); \ | |
78 | entry->key = (k); \ | |
79 | entry->data = (d); \ | |
80 | } \ | |
81 | while (0) | |
82 | ||
83 | #define GET_CREATE_HASH_ENTRY(entry, table, k, done) \ | |
84 | do \ | |
85 | { \ | |
86 | hash_entry_t *h = HASH (table, k); \ | |
87 | int i; \ | |
88 | for (i = 0; i < N_SEEK; ++i) \ | |
89 | if (h[i].key == (void *) (k)) \ | |
90 | goto done; \ | |
91 | CREATE_HASH_ENTRY_AT (entry, table, h, k, gche ## done); \ | |
92 | entry->key = (k); \ | |
93 | entry->data = 0; \ | |
94 | break; \ | |
95 | done: \ | |
96 | (entry) = &h[i]; \ | |
97 | } \ | |
98 | while (0) | |
99 | ||
f1322139 MD |
100 | static void |
101 | grow (hash_entry_t **table, int *size) | |
102 | { | |
103 | hash_entry_t *oldtable = *table; | |
104 | int oldsize = *size + N_SEEK; | |
105 | hash_entry_t *TABLE (new) = 0; | |
106 | int SIZE (new); | |
107 | int i, j; | |
0e850825 | 108 | SIZE (new) = 2 * (oldsize - N_SEEK + 1) - 1; |
f1322139 MD |
109 | again: |
110 | TABLE (new) = realloc (TABLE (new), | |
111 | sizeof (hash_entry_t) * (SIZE (new) + N_SEEK)); | |
f22ed5a0 | 112 | memset (TABLE (new), 0, sizeof (hash_entry_t) * (SIZE (new) + N_SEEK)); |
f1322139 MD |
113 | for (i = 0; i < oldsize; ++i) |
114 | if (oldtable[i].key) | |
115 | { | |
116 | hash_entry_t *h = HASH (new, oldtable[i].key); | |
117 | for (j = 0; j < N_SEEK; ++j) | |
118 | if (h[j].key == 0) | |
119 | { | |
120 | h[j] = oldtable[i]; | |
121 | goto next; | |
122 | } | |
123 | SIZE (new) *= 2; | |
124 | goto again; | |
125 | next: | |
126 | ; | |
127 | } | |
128 | if (table == &malloc_type) | |
129 | { | |
130 | /* relocate malloc_object entries */ | |
131 | for (i = 0; i < oldsize; ++i) | |
132 | if (oldtable[i].key) | |
133 | { | |
134 | hash_entry_t *h = HASH (new, oldtable[i].key); | |
135 | while (h->key != oldtable[i].key) | |
136 | ++h; | |
137 | oldtable[i].data = h; | |
138 | } | |
139 | for (i = 0; i < malloc_object_size + N_SEEK; ++i) | |
140 | if (malloc_object[i].key) | |
141 | malloc_object[i].data | |
142 | = ((hash_entry_t *) malloc_object[i].data)->data; | |
143 | } | |
144 | free (*table); | |
145 | *table = TABLE (new); | |
146 | *size = SIZE (new); | |
147 | } | |
148 | ||
149 | void | |
150 | scm_malloc_register (void *obj, const char *what) | |
151 | { | |
152 | hash_entry_t *type; | |
153 | GET_CREATE_HASH_ENTRY (type, type, what, l1); | |
154 | type->data = (void *) ((int) type->data + 1); | |
155 | CREATE_HASH_ENTRY (object, obj, type, l2); | |
156 | } | |
157 | ||
158 | void | |
159 | scm_malloc_unregister (void *obj) | |
160 | { | |
161 | hash_entry_t *object, *type; | |
162 | GET_CREATE_HASH_ENTRY (object, object, obj, l1); | |
163 | type = (hash_entry_t *) object->data; | |
164 | if (type == 0) | |
165 | { | |
166 | fprintf (stderr, | |
4c9419ac | 167 | "scm_gc_free called on object not allocated with scm_gc_malloc\n"); |
f1322139 MD |
168 | abort (); |
169 | } | |
170 | type->data = (void *) ((int) type->data - 1); | |
171 | object->key = 0; | |
172 | } | |
173 | ||
174 | void | |
175 | scm_malloc_reregister (void *old, void *new, const char *newwhat) | |
176 | { | |
177 | hash_entry_t *object, *type; | |
4c9419ac MV |
178 | |
179 | if (old == NULL) | |
180 | scm_malloc_register (new, newwhat); | |
181 | else | |
f1322139 | 182 | { |
4c9419ac MV |
183 | GET_CREATE_HASH_ENTRY (object, object, old, l1); |
184 | type = (hash_entry_t *) object->data; | |
185 | if (type == 0) | |
f1322139 MD |
186 | { |
187 | fprintf (stderr, | |
4c9419ac MV |
188 | "scm_gc_realloc called on object not allocated " |
189 | "with scm_gc_malloc\n"); | |
f1322139 MD |
190 | abort (); |
191 | } | |
4c9419ac MV |
192 | if (strcmp ((char *) type->key, newwhat) != 0) |
193 | { | |
194 | if (strcmp (newwhat, "vector-set-length!") != 0) | |
195 | { | |
196 | fprintf (stderr, | |
197 | "scm_gc_realloc called with arg %s, was %s\n", | |
198 | newwhat, | |
199 | (char *) type->key); | |
200 | abort (); | |
201 | } | |
202 | } | |
203 | if (new != old) | |
204 | { | |
205 | object->key = 0; | |
206 | CREATE_HASH_ENTRY (object, new, type, l2); | |
207 | } | |
f1322139 MD |
208 | } |
209 | } | |
210 | ||
211 | SCM_DEFINE (scm_malloc_stats, "malloc-stats", 0, 0, 0, | |
212 | (), | |
6836c87b MG |
213 | "Return an alist ((@var{what} . @var{n}) ...) describing number\n" |
214 | "of malloced objects.\n" | |
4c9419ac | 215 | "@var{what} is the second argument to @code{scm_gc_malloc},\n" |
6836c87b MG |
216 | "@var{n} is the number of objects of that type currently\n" |
217 | "allocated.") | |
f1322139 MD |
218 | #define FUNC_NAME s_scm_malloc_stats |
219 | { | |
220 | SCM res = SCM_EOL; | |
221 | int i; | |
222 | for (i = 0; i < malloc_type_size + N_SEEK; ++i) | |
223 | if (malloc_type[i].key) | |
36c66a07 | 224 | res = scm_acons (scm_from_locale_string ((char *) malloc_type[i].key), |
e11e83f3 | 225 | scm_from_int ((int) malloc_type[i].data), |
f1322139 MD |
226 | res); |
227 | return res; | |
228 | } | |
229 | #undef FUNC_NAME | |
230 | ||
231 | void | |
232 | scm_debug_malloc_prehistory () | |
233 | { | |
234 | malloc_type = malloc (sizeof (hash_entry_t) | |
235 | * (malloc_type_size + N_SEEK)); | |
f22ed5a0 | 236 | memset (malloc_type, 0, sizeof (hash_entry_t) * (malloc_type_size + N_SEEK)); |
f1322139 MD |
237 | malloc_object = malloc (sizeof (hash_entry_t) |
238 | * (malloc_object_size + N_SEEK)); | |
f22ed5a0 | 239 | memset (malloc_object, 0, sizeof (hash_entry_t) * (malloc_object_size + N_SEEK)); |
f1322139 MD |
240 | } |
241 | ||
242 | void | |
243 | scm_init_debug_malloc () | |
244 | { | |
0e850825 | 245 | #include "libguile/debug-malloc.x" |
f1322139 | 246 | } |
8dc9439f | 247 |