1 /* Debug allocators for the Expat test suite
4 / _ \\ /| '_ \ / _` | __|
5 | __// \| |_) | (_| | |_
6 \___/_/\_\ .__/ \__,_|\__|
9 Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10 Copyright (c) 2000-2017 Expat development team
11 Licensed under the MIT license:
13 Permission is hereby granted, free of charge, to any person obtaining
14 a copy of this software and associated documentation files (the
15 "Software"), to deal in the Software without restriction, including
16 without limitation the rights to use, copy, modify, merge, publish,
17 distribute, sublicense, and/or sell copies of the Software, and to permit
18 persons to whom the Software is furnished to do so, subject to the
21 The above copyright notice and this permission notice shall be included
22 in all copies or substantial portions of the Software.
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
29 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30 USE OR OTHER DEALINGS IN THE SOFTWARE.
38 /* Structures to keep track of what has been allocated. Speed isn't a
39 * big issue for the tests this is required for, so we will use a
40 * doubly-linked list to make deletion easier.
43 typedef struct allocation_entry {
44 struct allocation_entry * next;
45 struct allocation_entry * prev;
50 static AllocationEntry *alloc_head = NULL;
51 static AllocationEntry *alloc_tail = NULL;
53 static AllocationEntry *find_allocation(void *ptr);
56 /* Allocate some memory and keep track of it. */
58 tracking_malloc(size_t size)
60 AllocationEntry *entry = malloc(sizeof(AllocationEntry));
63 printf("Allocator failure\n");
66 entry->num_bytes = size;
67 entry->allocation = malloc(size);
68 if (entry->allocation == NULL) {
74 /* Add to the list of allocations */
75 if (alloc_head == NULL) {
77 alloc_head = alloc_tail = entry;
79 entry->prev = alloc_tail;
80 alloc_tail->next = entry;
84 return entry->allocation;
87 static AllocationEntry *
88 find_allocation(void *ptr)
90 AllocationEntry *entry;
92 for (entry = alloc_head; entry != NULL; entry = entry->next) {
93 if (entry->allocation == ptr) {
100 /* Free some memory and remove the tracking for it */
102 tracking_free(void *ptr)
104 AllocationEntry *entry;
107 /* There won't be an entry for this */
111 entry = find_allocation(ptr);
113 /* This is the relevant allocation. Unlink it */
114 if (entry->prev != NULL)
115 entry->prev->next = entry->next;
117 alloc_head = entry->next;
118 if (entry->next != NULL)
119 entry->next->prev = entry->prev;
121 alloc_tail = entry->next;
124 printf("Attempting to free unallocated memory at %p\n", ptr);
129 /* Reallocate some memory and keep track of it */
131 tracking_realloc(void *ptr, size_t size)
133 AllocationEntry *entry;
136 /* By definition, this is equivalent to malloc(size) */
137 return tracking_malloc(size);
140 /* By definition, this is equivalent to free(ptr) */
145 /* Find the allocation entry for this memory */
146 entry = find_allocation(ptr);
148 printf("Attempting to realloc unallocated memory at %p\n", ptr);
149 entry = malloc(sizeof(AllocationEntry));
151 printf("Reallocator failure\n");
154 entry->allocation = realloc(ptr, size);
155 if (entry->allocation == NULL) {
160 /* Add to the list of allocations */
162 if (alloc_head == NULL) {
164 alloc_head = alloc_tail = entry;
166 entry->prev = alloc_tail;
167 alloc_tail->next = entry;
171 entry->allocation = realloc(ptr, size);
172 if (entry->allocation == NULL) {
173 /* Realloc semantics say the original is still allocated */
174 entry->allocation = ptr;
179 entry->num_bytes = size;
180 return entry->allocation;
184 tracking_report(void)
186 AllocationEntry *entry;
188 if (alloc_head == NULL)
191 /* Otherwise we have allocations that haven't been freed */
192 for (entry = alloc_head; entry != NULL; entry = entry->next)
194 printf("Allocated %lu bytes at %p\n",
195 (long unsigned)entry->num_bytes, entry->allocation);