4d04df5db1f2cbdc0028eb253ccd4a326d9f208b
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
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
25 #include "libguile/_scm.h"
26 #include "libguile/alist.h"
27 #include "libguile/strings.h"
29 #include "libguile/debug-malloc.h"
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
38 typedef struct hash_entry
{
45 static int malloc_type_size
= 31;
46 static hash_entry_t
*malloc_type
= 0;
47 static int malloc_object_size
= 8191;
48 static hash_entry_t
*malloc_object
= 0;
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)]
55 #define CREATE_HASH_ENTRY_AT(entry, table, h, k, done) \
60 for (i = 0; i < N_SEEK; ++i) \
63 grow (&TABLE (table), &SIZE (table)); \
64 h = HASH (table, k); \
71 #define CREATE_HASH_ENTRY(table, k, d, done) \
74 hash_entry_t *h = HASH (table, k); \
75 hash_entry_t *entry; \
76 CREATE_HASH_ENTRY_AT (entry, table, h, k, done); \
82 #define GET_CREATE_HASH_ENTRY(entry, table, k, done) \
85 hash_entry_t *h = HASH (table, k); \
87 for (i = 0; i < N_SEEK; ++i) \
88 if (h[i].key == (void *) (k)) \
90 CREATE_HASH_ENTRY_AT (entry, table, h, k, gche ## done); \
100 grow (hash_entry_t
**table
, int *size
)
102 hash_entry_t
*oldtable
= *table
;
103 int oldsize
= *size
+ N_SEEK
;
104 hash_entry_t
*TABLE (new) = 0;
107 SIZE (new) = 2 * (oldsize
- N_SEEK
+ 1) - 1;
109 TABLE (new) = realloc (TABLE (new),
110 sizeof (hash_entry_t
) * (SIZE (new) + N_SEEK
));
111 memset (TABLE (new), 0, sizeof (hash_entry_t
) * (SIZE (new) + N_SEEK
));
112 for (i
= 0; i
< oldsize
; ++i
)
115 hash_entry_t
*h
= HASH (new, oldtable
[i
].key
);
116 for (j
= 0; j
< N_SEEK
; ++j
)
127 if (table
== &malloc_type
)
129 /* relocate malloc_object entries */
130 for (i
= 0; i
< oldsize
; ++i
)
133 hash_entry_t
*h
= HASH (new, oldtable
[i
].key
);
134 while (h
->key
!= oldtable
[i
].key
)
136 oldtable
[i
].data
= h
;
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
;
144 *table
= TABLE (new);
149 scm_malloc_register (void *obj
, const char *what
)
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
);
158 scm_malloc_unregister (void *obj
)
160 hash_entry_t
*object
, *type
;
161 GET_CREATE_HASH_ENTRY (object
, object
, obj
, l1
);
162 type
= (hash_entry_t
*) object
->data
;
166 "scm_gc_free called on object not allocated with scm_gc_malloc\n");
169 type
->data
= (void *) ((int) type
->data
- 1);
174 scm_malloc_reregister (void *old
, void *new, const char *newwhat
)
176 hash_entry_t
*object
, *type
;
179 scm_malloc_register (new, newwhat
);
182 GET_CREATE_HASH_ENTRY (object
, object
, old
, l1
);
183 type
= (hash_entry_t
*) object
->data
;
187 "scm_gc_realloc called on object not allocated "
188 "with scm_gc_malloc\n");
191 if (strcmp ((char *) type
->key
, newwhat
) != 0)
193 if (strcmp (newwhat
, "vector-set-length!") != 0)
196 "scm_gc_realloc called with arg %s, was %s\n",
205 CREATE_HASH_ENTRY (object
, new, type
, l2
);
210 SCM_DEFINE (scm_malloc_stats
, "malloc-stats", 0, 0, 0,
212 "Return an alist ((@var{what} . @var{n}) ...) describing number\n"
213 "of malloced objects.\n"
214 "@var{what} is the second argument to @code{scm_gc_malloc},\n"
215 "@var{n} is the number of objects of that type currently\n"
217 #define FUNC_NAME s_scm_malloc_stats
221 for (i
= 0; i
< malloc_type_size
+ N_SEEK
; ++i
)
222 if (malloc_type
[i
].key
)
223 res
= scm_acons (scm_from_locale_string ((char *) malloc_type
[i
].key
),
224 scm_from_int ((int) malloc_type
[i
].data
),
231 scm_debug_malloc_prehistory ()
233 malloc_type
= malloc (sizeof (hash_entry_t
)
234 * (malloc_type_size
+ N_SEEK
));
235 memset (malloc_type
, 0, sizeof (hash_entry_t
) * (malloc_type_size
+ N_SEEK
));
236 malloc_object
= malloc (sizeof (hash_entry_t
)
237 * (malloc_object_size
+ N_SEEK
));
238 memset (malloc_object
, 0, sizeof (hash_entry_t
) * (malloc_object_size
+ N_SEEK
));
242 scm_init_debug_malloc ()
244 #include "libguile/debug-malloc.x"