debug-malloc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Copyright (C) 2000, 2006, 2008 Free Software Foundation, Inc.
  2. *
  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.
  7. *
  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.
  12. *
  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
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/alist.h"
  25. #include "libguile/strings.h"
  26. #include "libguile/debug-malloc.h"
  27. /*
  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
  32. */
  33. typedef struct hash_entry {
  34. const void *key;
  35. const void *data;
  36. } hash_entry_t;
  37. #define N_SEEK 8
  38. static int malloc_type_size = 31;
  39. static hash_entry_t *malloc_type = 0;
  40. static int malloc_object_size = 8191;
  41. static hash_entry_t *malloc_object = 0;
  42. #define TABLE(table) malloc_ ## table
  43. #define SIZE(table) malloc_ ## table ## _size
  44. #define HASH(table, key) \
  45. &TABLE (table)[((unsigned long) key >> 4UL) * 2654435761UL % SIZE (table)]
  46. #define CREATE_HASH_ENTRY_AT(entry, table, h, k, done) \
  47. { \
  48. int i; \
  49. do \
  50. { \
  51. for (i = 0; i < N_SEEK; ++i) \
  52. if (h[i].key == 0) \
  53. goto done; \
  54. grow (&TABLE (table), &SIZE (table)); \
  55. h = HASH (table, k); \
  56. } \
  57. while (1); \
  58. done: \
  59. (entry) = &h[i]; \
  60. }
  61. #define CREATE_HASH_ENTRY(table, k, d, done) \
  62. do \
  63. { \
  64. hash_entry_t *h = HASH (table, k); \
  65. hash_entry_t *entry; \
  66. CREATE_HASH_ENTRY_AT (entry, table, h, k, done); \
  67. entry->key = (k); \
  68. entry->data = (d); \
  69. } \
  70. while (0)
  71. #define GET_CREATE_HASH_ENTRY(entry, table, k, done) \
  72. do \
  73. { \
  74. hash_entry_t *h = HASH (table, k); \
  75. int i; \
  76. for (i = 0; i < N_SEEK; ++i) \
  77. if (h[i].key == (void *) (k)) \
  78. goto done; \
  79. CREATE_HASH_ENTRY_AT (entry, table, h, k, gche ## done); \
  80. entry->key = (k); \
  81. entry->data = 0; \
  82. break; \
  83. done: \
  84. (entry) = &h[i]; \
  85. } \
  86. while (0)
  87. static void
  88. grow (hash_entry_t **table, int *size)
  89. {
  90. hash_entry_t *oldtable = *table;
  91. int oldsize = *size + N_SEEK;
  92. hash_entry_t *TABLE (new) = 0;
  93. int SIZE (new);
  94. int i, j;
  95. SIZE (new) = 2 * (oldsize - N_SEEK + 1) - 1;
  96. again:
  97. TABLE (new) = realloc (TABLE (new),
  98. sizeof (hash_entry_t) * (SIZE (new) + N_SEEK));
  99. memset (TABLE (new), 0, sizeof (hash_entry_t) * (SIZE (new) + N_SEEK));
  100. for (i = 0; i < oldsize; ++i)
  101. if (oldtable[i].key)
  102. {
  103. hash_entry_t *h = HASH (new, oldtable[i].key);
  104. for (j = 0; j < N_SEEK; ++j)
  105. if (h[j].key == 0)
  106. {
  107. h[j] = oldtable[i];
  108. goto next;
  109. }
  110. SIZE (new) *= 2;
  111. goto again;
  112. next:
  113. ;
  114. }
  115. if (table == &malloc_type)
  116. {
  117. /* relocate malloc_object entries */
  118. for (i = 0; i < oldsize; ++i)
  119. if (oldtable[i].key)
  120. {
  121. hash_entry_t *h = HASH (new, oldtable[i].key);
  122. while (h->key != oldtable[i].key)
  123. ++h;
  124. oldtable[i].data = h;
  125. }
  126. for (i = 0; i < malloc_object_size + N_SEEK; ++i)
  127. if (malloc_object[i].key)
  128. malloc_object[i].data
  129. = ((hash_entry_t *) malloc_object[i].data)->data;
  130. }
  131. free (*table);
  132. *table = TABLE (new);
  133. *size = SIZE (new);
  134. }
  135. void
  136. scm_malloc_register (void *obj, const char *what)
  137. {
  138. hash_entry_t *type;
  139. GET_CREATE_HASH_ENTRY (type, type, what, l1);
  140. type->data = (void *) ((int) type->data + 1);
  141. CREATE_HASH_ENTRY (object, obj, type, l2);
  142. }
  143. void
  144. scm_malloc_unregister (void *obj)
  145. {
  146. hash_entry_t *object, *type;
  147. GET_CREATE_HASH_ENTRY (object, object, obj, l1);
  148. type = (hash_entry_t *) object->data;
  149. if (type == 0)
  150. {
  151. fprintf (stderr,
  152. "scm_gc_free called on object not allocated with scm_gc_malloc\n");
  153. abort ();
  154. }
  155. type->data = (void *) ((int) type->data - 1);
  156. object->key = 0;
  157. }
  158. void
  159. scm_malloc_reregister (void *old, void *new, const char *newwhat)
  160. {
  161. hash_entry_t *object, *type;
  162. if (old == NULL)
  163. scm_malloc_register (new, newwhat);
  164. else
  165. {
  166. GET_CREATE_HASH_ENTRY (object, object, old, l1);
  167. type = (hash_entry_t *) object->data;
  168. if (type == 0)
  169. {
  170. fprintf (stderr,
  171. "scm_gc_realloc called on object not allocated "
  172. "with scm_gc_malloc\n");
  173. abort ();
  174. }
  175. if (strcmp ((char *) type->key, newwhat) != 0)
  176. {
  177. if (strcmp (newwhat, "vector-set-length!") != 0)
  178. {
  179. fprintf (stderr,
  180. "scm_gc_realloc called with arg %s, was %s\n",
  181. newwhat,
  182. (char *) type->key);
  183. abort ();
  184. }
  185. }
  186. if (new != old)
  187. {
  188. object->key = 0;
  189. CREATE_HASH_ENTRY (object, new, type, l2);
  190. }
  191. }
  192. }
  193. SCM_DEFINE (scm_malloc_stats, "malloc-stats", 0, 0, 0,
  194. (),
  195. "Return an alist ((@var{what} . @var{n}) ...) describing number\n"
  196. "of malloced objects.\n"
  197. "@var{what} is the second argument to @code{scm_gc_malloc},\n"
  198. "@var{n} is the number of objects of that type currently\n"
  199. "allocated.")
  200. #define FUNC_NAME s_scm_malloc_stats
  201. {
  202. SCM res = SCM_EOL;
  203. int i;
  204. for (i = 0; i < malloc_type_size + N_SEEK; ++i)
  205. if (malloc_type[i].key)
  206. res = scm_acons (scm_from_locale_string ((char *) malloc_type[i].key),
  207. scm_from_int ((int) malloc_type[i].data),
  208. res);
  209. return res;
  210. }
  211. #undef FUNC_NAME
  212. void
  213. scm_debug_malloc_prehistory ()
  214. {
  215. malloc_type = malloc (sizeof (hash_entry_t)
  216. * (malloc_type_size + N_SEEK));
  217. memset (malloc_type, 0, sizeof (hash_entry_t) * (malloc_type_size + N_SEEK));
  218. malloc_object = malloc (sizeof (hash_entry_t)
  219. * (malloc_object_size + N_SEEK));
  220. memset (malloc_object, 0, sizeof (hash_entry_t) * (malloc_object_size + N_SEEK));
  221. }
  222. void
  223. scm_init_debug_malloc ()
  224. {
  225. #include "libguile/debug-malloc.x"
  226. }