memcheck.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Debug allocators for the Expat test suite
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 2017 Rhodri James <[email protected]>
  9. Copyright (c) 2017-2023 Sebastian Pipping <[email protected]>
  10. Copyright (c) 2022 Sean McBride <[email protected]>
  11. Licensed under the MIT license:
  12. Permission is hereby granted, free of charge, to any person obtaining
  13. a copy of this software and associated documentation files (the
  14. "Software"), to deal in the Software without restriction, including
  15. without limitation the rights to use, copy, modify, merge, publish,
  16. distribute, sublicense, and/or sell copies of the Software, and to permit
  17. persons to whom the Software is furnished to do so, subject to the
  18. following conditions:
  19. The above copyright notice and this permission notice shall be included
  20. in all copies or substantial portions of the Software.
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  24. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  25. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  26. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  27. USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include "memcheck.h"
  32. /* Structures to keep track of what has been allocated. Speed isn't a
  33. * big issue for the tests this is required for, so we will use a
  34. * doubly-linked list to make deletion easier.
  35. */
  36. typedef struct allocation_entry {
  37. struct allocation_entry *next;
  38. struct allocation_entry *prev;
  39. void *allocation;
  40. size_t num_bytes;
  41. } AllocationEntry;
  42. static AllocationEntry *alloc_head = NULL;
  43. static AllocationEntry *alloc_tail = NULL;
  44. static AllocationEntry *find_allocation(const void *ptr);
  45. /* Allocate some memory and keep track of it. */
  46. void *
  47. tracking_malloc(size_t size) {
  48. AllocationEntry *const entry
  49. = (AllocationEntry *)malloc(sizeof(AllocationEntry));
  50. if (entry == NULL) {
  51. printf("Allocator failure\n");
  52. return NULL;
  53. }
  54. entry->num_bytes = size;
  55. entry->allocation = malloc(size);
  56. if (entry->allocation == NULL) {
  57. free(entry);
  58. return NULL;
  59. }
  60. entry->next = NULL;
  61. /* Add to the list of allocations */
  62. if (alloc_head == NULL) {
  63. entry->prev = NULL;
  64. alloc_head = alloc_tail = entry;
  65. } else {
  66. entry->prev = alloc_tail;
  67. alloc_tail->next = entry;
  68. alloc_tail = entry;
  69. }
  70. return entry->allocation;
  71. }
  72. static AllocationEntry *
  73. find_allocation(const void *ptr) {
  74. AllocationEntry *entry;
  75. for (entry = alloc_head; entry != NULL; entry = entry->next) {
  76. if (entry->allocation == ptr) {
  77. return entry;
  78. }
  79. }
  80. return NULL;
  81. }
  82. /* Free some memory and remove the tracking for it */
  83. void
  84. tracking_free(void *ptr) {
  85. AllocationEntry *entry;
  86. if (ptr == NULL) {
  87. /* There won't be an entry for this */
  88. return;
  89. }
  90. entry = find_allocation(ptr);
  91. if (entry != NULL) {
  92. /* This is the relevant allocation. Unlink it */
  93. if (entry->prev != NULL)
  94. entry->prev->next = entry->next;
  95. else
  96. alloc_head = entry->next;
  97. if (entry->next != NULL)
  98. entry->next->prev = entry->prev;
  99. else
  100. alloc_tail = entry->next;
  101. free(entry);
  102. } else {
  103. printf("Attempting to free unallocated memory at %p\n", ptr);
  104. }
  105. free(ptr);
  106. }
  107. /* Reallocate some memory and keep track of it */
  108. void *
  109. tracking_realloc(void *ptr, size_t size) {
  110. AllocationEntry *entry;
  111. if (ptr == NULL) {
  112. /* By definition, this is equivalent to malloc(size) */
  113. return tracking_malloc(size);
  114. }
  115. if (size == 0) {
  116. /* By definition, this is equivalent to free(ptr) */
  117. tracking_free(ptr);
  118. return NULL;
  119. }
  120. /* Find the allocation entry for this memory */
  121. entry = find_allocation(ptr);
  122. if (entry == NULL) {
  123. printf("Attempting to realloc unallocated memory at %p\n", ptr);
  124. entry = (AllocationEntry *)malloc(sizeof(AllocationEntry));
  125. if (entry == NULL) {
  126. printf("Reallocator failure\n");
  127. return NULL;
  128. }
  129. entry->allocation = realloc(ptr, size);
  130. if (entry->allocation == NULL) {
  131. free(entry);
  132. return NULL;
  133. }
  134. /* Add to the list of allocations */
  135. entry->next = NULL;
  136. if (alloc_head == NULL) {
  137. entry->prev = NULL;
  138. alloc_head = alloc_tail = entry;
  139. } else {
  140. entry->prev = alloc_tail;
  141. alloc_tail->next = entry;
  142. alloc_tail = entry;
  143. }
  144. } else {
  145. void *const reallocated = realloc(ptr, size);
  146. if (reallocated == NULL) {
  147. return NULL;
  148. }
  149. entry->allocation = reallocated;
  150. }
  151. entry->num_bytes = size;
  152. return entry->allocation;
  153. }
  154. int
  155. tracking_report(void) {
  156. AllocationEntry *entry;
  157. if (alloc_head == NULL)
  158. return 1;
  159. /* Otherwise we have allocations that haven't been freed */
  160. for (entry = alloc_head; entry != NULL; entry = entry->next) {
  161. printf("Allocated %lu bytes at %p\n", (long unsigned)entry->num_bytes,
  162. entry->allocation);
  163. }
  164. return 0;
  165. }