1 /* Copyright (C) 2000, 2006, 2008 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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * 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
26 #include "libguile/_scm.h"
27 #include "libguile/alist.h"
28 #include "libguile/strings.h"
30 #include "libguile/debug-malloc.h"
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
39 typedef struct hash_entry
{
46 static int malloc_type_size
= 31;
47 static hash_entry_t
*malloc_type
= 0;
48 static int malloc_object_size
= 8191;
49 static hash_entry_t
*malloc_object
= 0;
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)]
56 #define CREATE_HASH_ENTRY_AT(entry, table, h, k, done) \
61 for (i = 0; i < N_SEEK; ++i) \
64 grow (&TABLE (table), &SIZE (table)); \
65 h = HASH (table, k); \
72 #define CREATE_HASH_ENTRY(table, k, d, done) \
75 hash_entry_t *h = HASH (table, k); \
76 hash_entry_t *entry; \
77 CREATE_HASH_ENTRY_AT (entry, table, h, k, done); \
83 #define GET_CREATE_HASH_ENTRY(entry, table, k, done) \
86 hash_entry_t *h = HASH (table, k); \
88 for (i = 0; i < N_SEEK; ++i) \
89 if (h[i].key == (void *) (k)) \
91 CREATE_HASH_ENTRY_AT (entry, table, h, k, gche ## done); \
101 grow (hash_entry_t
**table
, int *size
)
103 hash_entry_t
*oldtable
= *table
;
104 int oldsize
= *size
+ N_SEEK
;
105 hash_entry_t
*TABLE (new) = 0;
108 SIZE (new) = 2 * (oldsize
- N_SEEK
+ 1) - 1;
110 TABLE (new) = realloc (TABLE (new),
111 sizeof (hash_entry_t
) * (SIZE (new) + N_SEEK
));
112 memset (TABLE (new), 0, sizeof (hash_entry_t
) * (SIZE (new) + N_SEEK
));
113 for (i
= 0; i
< oldsize
; ++i
)
116 hash_entry_t
*h
= HASH (new, oldtable
[i
].key
);
117 for (j
= 0; j
< N_SEEK
; ++j
)
128 if (table
== &malloc_type
)
130 /* relocate malloc_object entries */
131 for (i
= 0; i
< oldsize
; ++i
)
134 hash_entry_t
*h
= HASH (new, oldtable
[i
].key
);
135 while (h
->key
!= oldtable
[i
].key
)
137 oldtable
[i
].data
= h
;
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
;
145 *table
= TABLE (new);
150 scm_malloc_register (void *obj
, const char *what
)
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
);
159 scm_malloc_unregister (void *obj
)
161 hash_entry_t
*object
, *type
;
162 GET_CREATE_HASH_ENTRY (object
, object
, obj
, l1
);
163 type
= (hash_entry_t
*) object
->data
;
167 "scm_gc_free called on object not allocated with scm_gc_malloc\n");
170 type
->data
= (void *) ((int) type
->data
- 1);
175 scm_malloc_reregister (void *old
, void *new, const char *newwhat
)
177 hash_entry_t
*object
, *type
;
180 scm_malloc_register (new, newwhat
);
183 GET_CREATE_HASH_ENTRY (object
, object
, old
, l1
);
184 type
= (hash_entry_t
*) object
->data
;
188 "scm_gc_realloc called on object not allocated "
189 "with scm_gc_malloc\n");
192 if (strcmp ((char *) type
->key
, newwhat
) != 0)
194 if (strcmp (newwhat
, "vector-set-length!") != 0)
197 "scm_gc_realloc called with arg %s, was %s\n",
206 CREATE_HASH_ENTRY (object
, new, type
, l2
);
211 SCM_DEFINE (scm_malloc_stats
, "malloc-stats", 0, 0, 0,
213 "Return an alist ((@var{what} . @var{n}) ...) describing number\n"
214 "of malloced objects.\n"
215 "@var{what} is the second argument to @code{scm_gc_malloc},\n"
216 "@var{n} is the number of objects of that type currently\n"
218 #define FUNC_NAME s_scm_malloc_stats
222 for (i
= 0; i
< malloc_type_size
+ N_SEEK
; ++i
)
223 if (malloc_type
[i
].key
)
224 res
= scm_acons (scm_from_locale_string ((char *) malloc_type
[i
].key
),
225 scm_from_int ((int) malloc_type
[i
].data
),
232 scm_debug_malloc_prehistory ()
234 malloc_type
= malloc (sizeof (hash_entry_t
)
235 * (malloc_type_size
+ N_SEEK
));
236 memset (malloc_type
, 0, sizeof (hash_entry_t
) * (malloc_type_size
+ N_SEEK
));
237 malloc_object
= malloc (sizeof (hash_entry_t
)
238 * (malloc_object_size
+ N_SEEK
));
239 memset (malloc_object
, 0, sizeof (hash_entry_t
) * (malloc_object_size
+ N_SEEK
));
243 scm_init_debug_malloc ()
245 #include "libguile/debug-malloc.x"