1 /* Copyright (C) 2000 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program 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
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
45 #include "libguile/_scm.h"
46 #include "libguile/alist.h"
47 #include "libguile/strings.h"
49 #include "libguile/debug-malloc.h"
52 * The following code is a hack which I wrote quickly in order to
53 * solve a memory leak problem. Since I wanted to have the
54 * application running at close to normal speed, I prioritized speed
55 * over maintainability. /mdj
58 typedef struct hash_entry
{
65 static int malloc_type_size
= 31;
66 static hash_entry_t
*malloc_type
= 0;
67 static int malloc_object_size
= 8191;
68 static hash_entry_t
*malloc_object
= 0;
70 #define TABLE(table) malloc_ ## table
71 #define SIZE(table) malloc_ ## table ## _size
72 #define HASH(table, key) \
73 &TABLE (table)[((unsigned long) key >> 4UL) * 2654435761UL % SIZE (table)]
75 #define CREATE_HASH_ENTRY_AT(entry, table, h, k, done) \
80 for (i = 0; i < N_SEEK; ++i) \
83 grow (&TABLE (table), &SIZE (table)); \
84 h = HASH (table, k); \
91 #define CREATE_HASH_ENTRY(table, k, d, done) \
94 hash_entry_t *h = HASH (table, k); \
95 hash_entry_t *entry; \
96 CREATE_HASH_ENTRY_AT (entry, table, h, k, done); \
102 #define GET_CREATE_HASH_ENTRY(entry, table, k, done) \
105 hash_entry_t *h = HASH (table, k); \
107 for (i = 0; i < N_SEEK; ++i) \
108 if (h[i].key == (void *) (k)) \
110 CREATE_HASH_ENTRY_AT (entry, table, h, k, gche ## done); \
120 grow (hash_entry_t
**table
, int *size
)
122 hash_entry_t
*oldtable
= *table
;
123 int oldsize
= *size
+ N_SEEK
;
124 hash_entry_t
*TABLE (new) = 0;
127 SIZE (new) = 2 * (oldsize
- N_SEEK
+ 1) - 1;
129 TABLE (new) = realloc (TABLE (new),
130 sizeof (hash_entry_t
) * (SIZE (new) + N_SEEK
));
131 memset (TABLE (new), 0, sizeof (hash_entry_t
) * (SIZE (new) + N_SEEK
));
132 for (i
= 0; i
< oldsize
; ++i
)
135 hash_entry_t
*h
= HASH (new, oldtable
[i
].key
);
136 for (j
= 0; j
< N_SEEK
; ++j
)
147 if (table
== &malloc_type
)
149 /* relocate malloc_object entries */
150 for (i
= 0; i
< oldsize
; ++i
)
153 hash_entry_t
*h
= HASH (new, oldtable
[i
].key
);
154 while (h
->key
!= oldtable
[i
].key
)
156 oldtable
[i
].data
= h
;
158 for (i
= 0; i
< malloc_object_size
+ N_SEEK
; ++i
)
159 if (malloc_object
[i
].key
)
160 malloc_object
[i
].data
161 = ((hash_entry_t
*) malloc_object
[i
].data
)->data
;
164 *table
= TABLE (new);
169 scm_malloc_register (void *obj
, const char *what
)
172 GET_CREATE_HASH_ENTRY (type
, type
, what
, l1
);
173 type
->data
= (void *) ((int) type
->data
+ 1);
174 CREATE_HASH_ENTRY (object
, obj
, type
, l2
);
178 scm_malloc_unregister (void *obj
)
180 hash_entry_t
*object
, *type
;
181 GET_CREATE_HASH_ENTRY (object
, object
, obj
, l1
);
182 type
= (hash_entry_t
*) object
->data
;
186 "scm_gc_free called on object not allocated with scm_gc_malloc\n");
189 type
->data
= (void *) ((int) type
->data
- 1);
194 scm_malloc_reregister (void *old
, void *new, const char *newwhat
)
196 hash_entry_t
*object
, *type
;
199 scm_malloc_register (new, newwhat
);
202 GET_CREATE_HASH_ENTRY (object
, object
, old
, l1
);
203 type
= (hash_entry_t
*) object
->data
;
207 "scm_gc_realloc called on object not allocated "
208 "with scm_gc_malloc\n");
211 if (strcmp ((char *) type
->key
, newwhat
) != 0)
213 if (strcmp (newwhat
, "vector-set-length!") != 0)
216 "scm_gc_realloc called with arg %s, was %s\n",
225 CREATE_HASH_ENTRY (object
, new, type
, l2
);
230 SCM_DEFINE (scm_malloc_stats
, "malloc-stats", 0, 0, 0,
232 "Return an alist ((@var{what} . @var{n}) ...) describing number\n"
233 "of malloced objects.\n"
234 "@var{what} is the second argument to @code{scm_gc_malloc},\n"
235 "@var{n} is the number of objects of that type currently\n"
237 #define FUNC_NAME s_scm_malloc_stats
241 for (i
= 0; i
< malloc_type_size
+ N_SEEK
; ++i
)
242 if (malloc_type
[i
].key
)
243 res
= scm_acons (scm_makfrom0str ((char *) malloc_type
[i
].key
),
244 SCM_MAKINUM ((int) malloc_type
[i
].data
),
251 scm_debug_malloc_prehistory ()
253 malloc_type
= malloc (sizeof (hash_entry_t
)
254 * (malloc_type_size
+ N_SEEK
));
255 memset (malloc_type
, 0, sizeof (hash_entry_t
) * (malloc_type_size
+ N_SEEK
));
256 malloc_object
= malloc (sizeof (hash_entry_t
)
257 * (malloc_object_size
+ N_SEEK
));
258 memset (malloc_object
, 0, sizeof (hash_entry_t
) * (malloc_object_size
+ N_SEEK
));
262 scm_init_debug_malloc ()
264 #ifndef SCM_MAGIC_SNARFER
265 #include "libguile/debug-malloc.x"