1 /* Copyright (C) 2000 Free Software Foundation, Inc.
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.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libguile/_scm.h"
22 #include "libguile/alist.h"
23 #include "libguile/strings.h"
25 #include "libguile/debug-malloc.h"
28 * The following code is a hack which I wrote quickly in order to
29 * solve a memory leak problem. Since I wanted to have the
30 * application running at close to normal speed, I prioritized speed
31 * over maintainability. /mdj
34 typedef struct hash_entry
{
41 static int malloc_type_size
= 31;
42 static hash_entry_t
*malloc_type
= 0;
43 static int malloc_object_size
= 8191;
44 static hash_entry_t
*malloc_object
= 0;
46 #define TABLE(table) malloc_ ## table
47 #define SIZE(table) malloc_ ## table ## _size
48 #define HASH(table, key) \
49 &TABLE (table)[((unsigned long) key >> 4UL) * 2654435761UL % SIZE (table)]
51 #define CREATE_HASH_ENTRY_AT(entry, table, h, k, done) \
56 for (i = 0; i < N_SEEK; ++i) \
59 grow (&TABLE (table), &SIZE (table)); \
60 h = HASH (table, k); \
67 #define CREATE_HASH_ENTRY(table, k, d, done) \
70 hash_entry_t *h = HASH (table, k); \
71 hash_entry_t *entry; \
72 CREATE_HASH_ENTRY_AT (entry, table, h, k, done); \
78 #define GET_CREATE_HASH_ENTRY(entry, table, k, done) \
81 hash_entry_t *h = HASH (table, k); \
83 for (i = 0; i < N_SEEK; ++i) \
84 if (h[i].key == (void *) (k)) \
86 CREATE_HASH_ENTRY_AT (entry, table, h, k, gche ## done); \
96 grow (hash_entry_t
**table
, int *size
)
98 hash_entry_t
*oldtable
= *table
;
99 int oldsize
= *size
+ N_SEEK
;
100 hash_entry_t
*TABLE (new) = 0;
103 SIZE (new) = 2 * (oldsize
- N_SEEK
+ 1) - 1;
105 TABLE (new) = realloc (TABLE (new),
106 sizeof (hash_entry_t
) * (SIZE (new) + N_SEEK
));
107 memset (TABLE (new), 0, sizeof (hash_entry_t
) * (SIZE (new) + N_SEEK
));
108 for (i
= 0; i
< oldsize
; ++i
)
111 hash_entry_t
*h
= HASH (new, oldtable
[i
].key
);
112 for (j
= 0; j
< N_SEEK
; ++j
)
123 if (table
== &malloc_type
)
125 /* relocate malloc_object entries */
126 for (i
= 0; i
< oldsize
; ++i
)
129 hash_entry_t
*h
= HASH (new, oldtable
[i
].key
);
130 while (h
->key
!= oldtable
[i
].key
)
132 oldtable
[i
].data
= h
;
134 for (i
= 0; i
< malloc_object_size
+ N_SEEK
; ++i
)
135 if (malloc_object
[i
].key
)
136 malloc_object
[i
].data
137 = ((hash_entry_t
*) malloc_object
[i
].data
)->data
;
140 *table
= TABLE (new);
145 scm_malloc_register (void *obj
, const char *what
)
148 GET_CREATE_HASH_ENTRY (type
, type
, what
, l1
);
149 type
->data
= (void *) ((int) type
->data
+ 1);
150 CREATE_HASH_ENTRY (object
, obj
, type
, l2
);
154 scm_malloc_unregister (void *obj
)
156 hash_entry_t
*object
, *type
;
157 GET_CREATE_HASH_ENTRY (object
, object
, obj
, l1
);
158 type
= (hash_entry_t
*) object
->data
;
162 "scm_gc_free called on object not allocated with scm_gc_malloc\n");
165 type
->data
= (void *) ((int) type
->data
- 1);
170 scm_malloc_reregister (void *old
, void *new, const char *newwhat
)
172 hash_entry_t
*object
, *type
;
175 scm_malloc_register (new, newwhat
);
178 GET_CREATE_HASH_ENTRY (object
, object
, old
, l1
);
179 type
= (hash_entry_t
*) object
->data
;
183 "scm_gc_realloc called on object not allocated "
184 "with scm_gc_malloc\n");
187 if (strcmp ((char *) type
->key
, newwhat
) != 0)
189 if (strcmp (newwhat
, "vector-set-length!") != 0)
192 "scm_gc_realloc called with arg %s, was %s\n",
201 CREATE_HASH_ENTRY (object
, new, type
, l2
);
206 SCM_DEFINE (scm_malloc_stats
, "malloc-stats", 0, 0, 0,
208 "Return an alist ((@var{what} . @var{n}) ...) describing number\n"
209 "of malloced objects.\n"
210 "@var{what} is the second argument to @code{scm_gc_malloc},\n"
211 "@var{n} is the number of objects of that type currently\n"
213 #define FUNC_NAME s_scm_malloc_stats
217 for (i
= 0; i
< malloc_type_size
+ N_SEEK
; ++i
)
218 if (malloc_type
[i
].key
)
219 res
= scm_acons (scm_makfrom0str ((char *) malloc_type
[i
].key
),
220 scm_from_int ((int) malloc_type
[i
].data
),
227 scm_debug_malloc_prehistory ()
229 malloc_type
= malloc (sizeof (hash_entry_t
)
230 * (malloc_type_size
+ N_SEEK
));
231 memset (malloc_type
, 0, sizeof (hash_entry_t
) * (malloc_type_size
+ N_SEEK
));
232 malloc_object
= malloc (sizeof (hash_entry_t
)
233 * (malloc_object_size
+ N_SEEK
));
234 memset (malloc_object
, 0, sizeof (hash_entry_t
) * (malloc_object_size
+ N_SEEK
));
238 scm_init_debug_malloc ()
240 #include "libguile/debug-malloc.x"