debug-malloc.c 6.3 KB

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