debug-malloc.c 6.4 KB

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