debug-malloc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 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. #ifdef MISSING_BZERO_DECL
  108. extern void bzero (void *, size_t);
  109. #endif
  110. static void
  111. grow (hash_entry_t **table, int *size)
  112. {
  113. hash_entry_t *oldtable = *table;
  114. int oldsize = *size + N_SEEK;
  115. hash_entry_t *TABLE (new) = 0;
  116. int SIZE (new);
  117. int i, j;
  118. SIZE (new) = 2 * (oldsize - N_SEEK + 1) - 1;
  119. again:
  120. TABLE (new) = realloc (TABLE (new),
  121. sizeof (hash_entry_t) * (SIZE (new) + N_SEEK));
  122. bzero (TABLE (new), sizeof (hash_entry_t) * (SIZE (new) + N_SEEK));
  123. for (i = 0; i < oldsize; ++i)
  124. if (oldtable[i].key)
  125. {
  126. hash_entry_t *h = HASH (new, oldtable[i].key);
  127. for (j = 0; j < N_SEEK; ++j)
  128. if (h[j].key == 0)
  129. {
  130. h[j] = oldtable[i];
  131. goto next;
  132. }
  133. SIZE (new) *= 2;
  134. goto again;
  135. next:
  136. ;
  137. }
  138. if (table == &malloc_type)
  139. {
  140. /* relocate malloc_object entries */
  141. for (i = 0; i < oldsize; ++i)
  142. if (oldtable[i].key)
  143. {
  144. hash_entry_t *h = HASH (new, oldtable[i].key);
  145. while (h->key != oldtable[i].key)
  146. ++h;
  147. oldtable[i].data = h;
  148. }
  149. for (i = 0; i < malloc_object_size + N_SEEK; ++i)
  150. if (malloc_object[i].key)
  151. malloc_object[i].data
  152. = ((hash_entry_t *) malloc_object[i].data)->data;
  153. }
  154. free (*table);
  155. *table = TABLE (new);
  156. *size = SIZE (new);
  157. }
  158. void
  159. scm_malloc_register (void *obj, const char *what)
  160. {
  161. hash_entry_t *type;
  162. GET_CREATE_HASH_ENTRY (type, type, what, l1);
  163. type->data = (void *) ((int) type->data + 1);
  164. CREATE_HASH_ENTRY (object, obj, type, l2);
  165. }
  166. void
  167. scm_malloc_unregister (void *obj)
  168. {
  169. hash_entry_t *object, *type;
  170. GET_CREATE_HASH_ENTRY (object, object, obj, l1);
  171. type = (hash_entry_t *) object->data;
  172. if (type == 0)
  173. {
  174. fprintf (stderr,
  175. "scm_must_free called on object not allocated with scm_must_malloc\n");
  176. abort ();
  177. }
  178. type->data = (void *) ((int) type->data - 1);
  179. object->key = 0;
  180. }
  181. void
  182. scm_malloc_reregister (void *old, void *new, const char *newwhat)
  183. {
  184. hash_entry_t *object, *type;
  185. GET_CREATE_HASH_ENTRY (object, object, old, l1);
  186. type = (hash_entry_t *) object->data;
  187. if (type == 0)
  188. {
  189. fprintf (stderr,
  190. "scm_must_realloc called on object not allocated with scm_must_malloc\n");
  191. abort ();
  192. }
  193. if (strcmp ((char *) type->key, newwhat) != 0)
  194. {
  195. if (strcmp (newwhat, "vector-set-length!") != 0)
  196. {
  197. fprintf (stderr,
  198. "scm_must_realloc called with arg %s, was %s\n",
  199. newwhat,
  200. (char *) type->key);
  201. abort ();
  202. }
  203. }
  204. if (new != old)
  205. {
  206. object->key = 0;
  207. CREATE_HASH_ENTRY (object, new, type, l2);
  208. }
  209. }
  210. SCM_DEFINE (scm_malloc_stats, "malloc-stats", 0, 0, 0,
  211. (),
  212. "Return an alist ((WHAT . N) ...) describing number of malloced objects.\n"
  213. "WHAT is the second argument to scm_must_malloc, N is the number of objects\n"
  214. "of that type currently allocated.")
  215. #define FUNC_NAME s_scm_malloc_stats
  216. {
  217. SCM res = SCM_EOL;
  218. int i;
  219. for (i = 0; i < malloc_type_size + N_SEEK; ++i)
  220. if (malloc_type[i].key)
  221. res = scm_acons (scm_makfrom0str ((char *) malloc_type[i].key),
  222. SCM_MAKINUM ((int) malloc_type[i].data),
  223. res);
  224. return res;
  225. }
  226. #undef FUNC_NAME
  227. void
  228. scm_debug_malloc_prehistory ()
  229. {
  230. malloc_type = malloc (sizeof (hash_entry_t)
  231. * (malloc_type_size + N_SEEK));
  232. bzero (malloc_type, sizeof (hash_entry_t) * (malloc_type_size + N_SEEK));
  233. malloc_object = malloc (sizeof (hash_entry_t)
  234. * (malloc_object_size + N_SEEK));
  235. bzero (malloc_object, sizeof (hash_entry_t) * (malloc_object_size + N_SEEK));
  236. }
  237. void
  238. scm_init_debug_malloc ()
  239. {
  240. #include "libguile/debug-malloc.x"
  241. }