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