amalloc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * debugging malloc()/realloc()/calloc()/free() that attempts
  3. * to keep track of just what's been allocated today.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "config.h"
  8. #define MAGIC 0x1f2e3d4c
  9. struct alist { int magic, size, index; int *end; struct alist *next, *last; };
  10. static struct alist list = { 0, 0, 0, 0 };
  11. static int mallocs=0;
  12. static int reallocs=0;
  13. static int frees=0;
  14. static int index = 0;
  15. static void
  16. die(char *msg, int index)
  17. {
  18. fprintf(stderr, msg, index);
  19. abort();
  20. }
  21. void *
  22. acalloc(int count, int size)
  23. {
  24. struct alist *ret;
  25. if ( size > 1 ) {
  26. count *= size;
  27. size = 1;
  28. }
  29. if ( ret = calloc(count + sizeof(struct alist) + sizeof(int), size) ) {
  30. ret->magic = MAGIC;
  31. ret->size = size * count;
  32. ret->index = index ++;
  33. ret->end = (int*)(count + (char*) (ret + 1));
  34. *(ret->end) = ~MAGIC;
  35. if ( list.next ) {
  36. ret->next = list.next;
  37. ret->last = &list;
  38. ret->next->last = ret;
  39. list.next = ret;
  40. }
  41. else {
  42. ret->last = ret->next = &list;
  43. list.next = list.last = ret;
  44. }
  45. ++mallocs;
  46. return ret+1;
  47. }
  48. return 0;
  49. }
  50. void*
  51. amalloc(int size)
  52. {
  53. return acalloc(size,1);
  54. }
  55. void
  56. afree(void *ptr)
  57. {
  58. struct alist *p2 = ((struct alist*)ptr)-1;
  59. if ( p2->magic == MAGIC ) {
  60. if ( ! (p2->end && *(p2->end) == ~MAGIC) )
  61. die("goddam: corrupted memory block %d in free()!\n", p2->index);
  62. p2->last->next = p2->next;
  63. p2->next->last = p2->last;
  64. ++frees;
  65. free(p2);
  66. }
  67. else
  68. free(ptr);
  69. }
  70. void *
  71. arealloc(void *ptr, int size)
  72. {
  73. struct alist *p2 = ((struct alist*)ptr)-1;
  74. struct alist save;
  75. if ( p2->magic == MAGIC ) {
  76. if ( ! (p2->end && *(p2->end) == ~MAGIC) )
  77. die("goddam: corrupted memory block %d in realloc()!\n", p2->index);
  78. save.next = p2->next;
  79. save.last = p2->last;
  80. p2 = realloc(p2, sizeof(int) + sizeof(*p2) + size);
  81. if ( p2 ) {
  82. p2->size = size;
  83. p2->end = (int*)(size + (char*) (p2 + 1));
  84. *(p2->end) = ~MAGIC;
  85. p2->next->last = p2;
  86. p2->last->next = p2;
  87. ++reallocs;
  88. return p2+1;
  89. }
  90. else {
  91. save.next->last = save.last;
  92. save.last->next = save.next;
  93. return 0;
  94. }
  95. }
  96. return realloc(ptr, size);
  97. }
  98. void
  99. adump()
  100. {
  101. struct alist *p;
  102. for ( p = list.next; p && (p != &list); p = p->next ) {
  103. fprintf(stderr, "allocated: %d byte%s\n", p->size, (p->size==1) ? "" : "s");
  104. fprintf(stderr, " [%.*s]\n", p->size, (char*)(p+1));
  105. }
  106. if ( getenv("AMALLOC_STATISTICS") ) {
  107. fprintf(stderr, "%d malloc%s\n", mallocs, (mallocs==1)?"":"s");
  108. fprintf(stderr, "%d realloc%s\n", reallocs, (reallocs==1)?"":"s");
  109. fprintf(stderr, "%d free%s\n", frees, (frees==1)?"":"s");
  110. }
  111. }