dynamic_mem_debug.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2000,2001 Florian Sander
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #define DYNAMIC_MEM_DEBUG 1
  19. #ifndef __stdio_h__
  20. #include <stdio.h>
  21. #endif
  22. #ifndef __string_h__
  23. #include <string.h>
  24. #endif
  25. #include <errno.h>
  26. #ifdef malloc
  27. #undef malloc
  28. #endif
  29. #ifdef free
  30. #undef free
  31. #endif
  32. #ifdef nmalloc
  33. #undef nmalloc
  34. #endif
  35. #define nmalloc(x) dmd_malloc((x),__FILE__,__LINE__)
  36. #ifdef nrealloc
  37. #undef nrealloc
  38. #endif
  39. #define nrealloc(x,y) dmd_realloc((x),(y),__FILE__,__LINE__)
  40. #ifdef nfree
  41. #undef nfree
  42. #endif
  43. #define nfree(x) dmd_free((x),__FILE__,__LINE__)
  44. #ifndef GENERIC_BINARY_TREE
  45. #include "generic_binary_tree.c"
  46. #endif
  47. #define DMD_FILE_SIZE 20
  48. struct dmd_memblock {
  49. void *ptr;
  50. int size;
  51. int line;
  52. char file[DMD_FILE_SIZE + 1];
  53. int expmem;
  54. };
  55. static int dmd_compare(void *data1, void *data2);
  56. static void dmd_freeblock(void *data);
  57. static void dmd_checkblock(void *data);
  58. static struct generic_binary_tree dmd_tree = {NULL, dmd_compare, NULL, dmd_freeblock};
  59. static void (*dmd_expmem_func) () = NULL;
  60. static struct dmd_memblock *dmd_create(void *ptr, int size, int line, const char *file)
  61. {
  62. struct dmd_memblock *nb;
  63. char *p;
  64. nb = malloc(sizeof(struct dmd_memblock));
  65. nb->ptr = ptr;
  66. nb->size = size;
  67. nb->line = line;
  68. p = strrchr(file, '/');
  69. strncpy(nb->file, p ? p + 1 : file, DMD_FILE_SIZE);
  70. nb->file[DMD_FILE_SIZE] = 0;
  71. return nb;
  72. }
  73. static void dmd_freeblock(void *data)
  74. {
  75. free((struct dmd_memblock *)data);
  76. }
  77. static int dmd_compare(void *data1, void *data2)
  78. {
  79. struct dmd_memblock *b1 = (struct dmd_memblock *) data1, *b2 = (struct dmd_memblock *) data2;
  80. unsigned long v1, v2;
  81. v1 = (unsigned long) b1->ptr;
  82. v2 = (unsigned long) b2->ptr;
  83. if (v1 > v2)
  84. return 1;
  85. else if (v1 < v2)
  86. return -1;
  87. else
  88. return 0;
  89. }
  90. static void *dmd_malloc(int size, const char *file, int line)
  91. {
  92. struct dmd_memblock *mb;
  93. void *ptr;
  94. // debug2("malloc %s:%d", file, line);
  95. ptr = malloc(size);
  96. // debug1("ptr: %u", (unsigned long) ptr);
  97. if (!ptr) {
  98. putlog(LOG_MISC, "*", "*** DMD: FAILED MALLOC %s (%d) (%d): %s", file, line, size, strerror(errno));
  99. fatal("Memory allocation failed", 0);
  100. }
  101. mb = dmd_create(ptr, size, line, file);
  102. if (btree_get(&dmd_tree, mb))
  103. putlog(LOG_MISC, "*", "*** DMD: DOUBLED POINTER?!?!?");
  104. btree_add(&dmd_tree, (void *) mb);
  105. return mb->ptr;
  106. }
  107. static void *dmd_realloc(void *old, int size, const char *file, int line)
  108. {
  109. struct dmd_memblock *mb, sb;
  110. void *p;
  111. // debug4("realloc %s:%d (%ul -> %i)", file, line, (unsigned long) old, size);
  112. sb.ptr = old;
  113. mb = btree_get(&dmd_tree, &sb);
  114. if (!mb) {
  115. putlog(LOG_MISC, "*", "*** DMD: FAILED REALLOC %s:%d (%d). Old pointer not found. This is probably fatal!", file, line, size);
  116. return NULL;
  117. }
  118. p = mb->ptr;
  119. p = realloc(p, size);
  120. if (!p) {
  121. putlog(LOG_MISC, "*", "*** DMD: FAILED REALLOC %s:%d (%d). realloc() returned NULL", file, line, size);
  122. // fatal("Memory re-allocation failed", 0);
  123. }
  124. if (((unsigned long) p) != ((unsigned long) old)) {
  125. // debug0("newpointer");
  126. btree_remove(&dmd_tree, mb);
  127. mb = dmd_create(p, size, line, file);
  128. btree_add(&dmd_tree, (void *) mb);
  129. } else {
  130. // debug0("oldpointer");
  131. mb->size = size;
  132. /* p = strrchr(file, '/');
  133. strncpy(mb->file, p ? p + 1 : file, DMD_FILE_SIZE);
  134. mb->file[DMD_FILE_SIZE] = 0;
  135. mb->line = line;*/
  136. }
  137. return p;
  138. }
  139. static void dmd_free(void *p, const char *file, int line)
  140. {
  141. struct dmd_memblock *mb, sb;
  142. // debug2("free %s:%d", file, line);
  143. sb.ptr = p;
  144. mb = btree_get(&dmd_tree, &sb);
  145. if (!mb) {
  146. putlog(LOG_MISC, "*", "*** DMD: ATTEMPTING TO FREE NON-MALLOC'D PTR: %s:%d",
  147. file, line);
  148. return;
  149. }
  150. btree_remove(&dmd_tree, mb);
  151. free(p);
  152. }
  153. static void dmd_reset(void *data)
  154. {
  155. struct dmd_memblock *b = (struct dmd_memblock *)data;
  156. b->expmem = -1;
  157. }
  158. static void dmd_init()
  159. {
  160. btree_freetree(&dmd_tree);
  161. }
  162. static FILE *dmd_filepointer;
  163. static int dmd_founderrors;
  164. static void dmd_expmem()
  165. {
  166. dmd_founderrors = 0;
  167. btree_getall(&dmd_tree, dmd_reset);
  168. if (dmd_expmem_func)
  169. dmd_expmem_func();
  170. dmd_filepointer = fopen("MEMDEBUG", "w");
  171. btree_getall(&dmd_tree, dmd_checkblock);
  172. fclose(dmd_filepointer);
  173. if (dmd_founderrors)
  174. putlog(LOG_MISC, "*", "*** DMD: %d EXPMEM ERRORS!", dmd_founderrors);
  175. }
  176. static void dmd_checkblock(void *data)
  177. {
  178. struct dmd_memblock *b = (struct dmd_memblock *)data;
  179. if (b->expmem == -1) {
  180. fprintf(dmd_filepointer, "LEAK : %d bytes in %s:%d\n", b->size, b->file, b->line);
  181. dmd_founderrors++;
  182. } else if (b->expmem != b->size) {
  183. fprintf(dmd_filepointer, "wrong size: %d bytes allocated, %d bytes expected in %s:%d\n",
  184. b->size, b->expmem, b->file, b->line);
  185. dmd_founderrors++;
  186. }
  187. }
  188. static void dmd_unload()
  189. {
  190. dmd_expmem();
  191. btree_freetree(&dmd_tree);
  192. }