debug-malloc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Copyright (C) 2000 Free Software Foundation, Inc.
  2. *
  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)
  6. * any later version.
  7. *
  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.
  12. *
  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., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  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.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  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.
  37. *
  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. */
  41. #include <string.h>
  42. #include <stdio.h>
  43. #include "libguile/_scm.h"
  44. #include "libguile/alist.h"
  45. #include "libguile/strings.h"
  46. #include "libguile/debug-malloc.h"
  47. /*
  48. * The following code is a hack which I wrote quickly in order to
  49. * solve a memory leak problem. Since I wanted to have the
  50. * application running at close to normal speed, I prioritized speed
  51. * over maintainability. /mdj
  52. */
  53. typedef struct hash_entry {
  54. const void *key;
  55. const void *data;
  56. } hash_entry_t;
  57. #define N_SEEK 8
  58. static int malloc_type_size = 31;
  59. static hash_entry_t *malloc_type = 0;
  60. static int malloc_object_size = 8191;
  61. static hash_entry_t *malloc_object = 0;
  62. #define TABLE(table) malloc_ ## table
  63. #define SIZE(table) malloc_ ## table ## _size
  64. #define HASH(table, key) \
  65. &TABLE (table)[((unsigned long) key >> 4UL) * 2654435761UL % SIZE (table)]
  66. #define CREATE_HASH_ENTRY_AT(entry, table, h, k, done) \
  67. { \
  68. int i; \
  69. do \
  70. { \
  71. for (i = 0; i < N_SEEK; ++i) \
  72. if (h[i].key == 0) \
  73. goto done; \
  74. grow (&TABLE (table), &SIZE (table)); \
  75. h = HASH (table, k); \
  76. } \
  77. while (1); \
  78. done: \
  79. (entry) = &h[i]; \
  80. }
  81. #define CREATE_HASH_ENTRY(table, k, d, done) \
  82. do \
  83. { \
  84. hash_entry_t *h = HASH (table, k); \
  85. hash_entry_t *entry; \
  86. CREATE_HASH_ENTRY_AT (entry, table, h, k, done); \
  87. entry->key = (k); \
  88. entry->data = (d); \
  89. } \
  90. while (0)
  91. #define GET_CREATE_HASH_ENTRY(entry, table, k, done) \
  92. do \
  93. { \
  94. hash_entry_t *h = HASH (table, k); \
  95. int i; \
  96. for (i = 0; i < N_SEEK; ++i) \
  97. if (h[i].key == (void *) (k)) \
  98. goto done; \
  99. CREATE_HASH_ENTRY_AT (entry, table, h, k, gche ## done); \
  100. entry->key = (k); \
  101. entry->data = 0; \
  102. break; \
  103. done: \
  104. (entry) = &h[i]; \
  105. } \
  106. while (0)
  107. static void
  108. grow (hash_entry_t **table, int *size)
  109. {
  110. hash_entry_t *oldtable = *table;
  111. int oldsize = *size + N_SEEK;
  112. hash_entry_t *TABLE (new) = 0;
  113. int SIZE (new);
  114. int i, j;
  115. SIZE (new) = 2 * (oldsize - N_SEEK + 1) - 1;
  116. again:
  117. TABLE (new) = realloc (TABLE (new),
  118. sizeof (hash_entry_t) * (SIZE (new) + N_SEEK));
  119. memset (TABLE (new), 0, sizeof (hash_entry_t) * (SIZE (new) + N_SEEK));
  120. for (i = 0; i < oldsize; ++i)
  121. if (oldtable[i].key)
  122. {
  123. hash_entry_t *h = HASH (new, oldtable[i].key);
  124. for (j = 0; j < N_SEEK; ++j)
  125. if (h[j].key == 0)
  126. {
  127. h[j] = oldtable[i];
  128. goto next;
  129. }
  130. SIZE (new) *= 2;
  131. goto again;
  132. next:
  133. ;
  134. }
  135. if (table == &malloc_type)
  136. {
  137. /* relocate malloc_object entries */
  138. for (i = 0; i < oldsize; ++i)
  139. if (oldtable[i].key)
  140. {
  141. hash_entry_t *h = HASH (new, oldtable[i].key);
  142. while (h->key != oldtable[i].key)
  143. ++h;
  144. oldtable[i].data = h;
  145. }
  146. for (i = 0; i < malloc_object_size + N_SEEK; ++i)
  147. if (malloc_object[i].key)
  148. malloc_object[i].data
  149. = ((hash_entry_t *) malloc_object[i].data)->data;
  150. }
  151. free (*table);
  152. *table = TABLE (new);
  153. *size = SIZE (new);
  154. }
  155. void
  156. scm_malloc_register (void *obj, const char *what)
  157. {
  158. hash_entry_t *type;
  159. GET_CREATE_HASH_ENTRY (type, type, what, l1);
  160. type->data = (void *) ((int) type->data + 1);
  161. CREATE_HASH_ENTRY (object, obj, type, l2);
  162. }
  163. void
  164. scm_malloc_unregister (void *obj)
  165. {
  166. hash_entry_t *object, *type;
  167. GET_CREATE_HASH_ENTRY (object, object, obj, l1);
  168. type = (hash_entry_t *) object->data;
  169. if (type == 0)
  170. {
  171. fprintf (stderr,
  172. "scm_must_free called on object not allocated with scm_must_malloc\n");
  173. abort ();
  174. }
  175. type->data = (void *) ((int) type->data - 1);
  176. object->key = 0;
  177. }
  178. void
  179. scm_malloc_reregister (void *old, void *new, const char *newwhat)
  180. {
  181. hash_entry_t *object, *type;
  182. GET_CREATE_HASH_ENTRY (object, object, old, l1);
  183. type = (hash_entry_t *) object->data;
  184. if (type == 0)
  185. {
  186. fprintf (stderr,
  187. "scm_must_realloc called on object not allocated with scm_must_malloc\n");
  188. abort ();
  189. }
  190. if (strcmp ((char *) type->key, newwhat) != 0)
  191. {
  192. if (strcmp (newwhat, "vector-set-length!") != 0)
  193. {
  194. fprintf (stderr,
  195. "scm_must_realloc called with arg %s, was %s\n",
  196. newwhat,
  197. (char *) type->key);
  198. abort ();
  199. }
  200. }
  201. if (new != old)
  202. {
  203. object->key = 0;
  204. CREATE_HASH_ENTRY (object, new, type, l2);
  205. }
  206. }
  207. SCM_DEFINE (scm_malloc_stats, "malloc-stats", 0, 0, 0,
  208. (),
  209. "Return an alist ((@var{what} . @var{n}) ...) describing number\n"
  210. "of malloced objects.\n"
  211. "@var{what} is the second argument to @code{scm_must_malloc},\n"
  212. "@var{n} is the number of objects of that type currently\n"
  213. "allocated.")
  214. #define FUNC_NAME s_scm_malloc_stats
  215. {
  216. SCM res = SCM_EOL;
  217. int i;
  218. for (i = 0; i < malloc_type_size + N_SEEK; ++i)
  219. if (malloc_type[i].key)
  220. res = scm_acons (scm_makfrom0str ((char *) malloc_type[i].key),
  221. SCM_MAKINUM ((int) malloc_type[i].data),
  222. res);
  223. return res;
  224. }
  225. #undef FUNC_NAME
  226. void
  227. scm_debug_malloc_prehistory ()
  228. {
  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));
  235. }
  236. void
  237. scm_init_debug_malloc ()
  238. {
  239. #include "libguile/debug-malloc.x"
  240. }