2 * Allocation wrapper for test purposes.
4 * Copyright © 2024 Nick Bowler
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 #include "cdecl-internal.h"
26 static union test_alloc {
39 #define ALLOC_STATE_ALLOCATED 0x7143u
40 #define ALLOC_STATE_FREED 0xdeadu
43 * Hook for testing allocation behaviour of the library, enabling a basic
44 * verification in test cases that the library does not leak memory.
46 * The library sources must be recompiled with -DTEST_MALLOC_HOOK to
47 * make use of this functionality.
49 * In this implementation, allocated memory is never freed, instead just
50 * marked with an indication that the memory is no longer live.
52 void *test_realloc_hook(void *p, size_t n)
54 union test_alloc *old_alloc = p, *alloc;
57 old_alloc = &old_alloc[-ALLOC_PAYLOAD];
58 if (old_alloc[ALLOC_STATE].v != ALLOC_STATE_ALLOCATED) {
59 printf("Bail out! %p is not a live allocation!\n", p);
63 old_alloc[ALLOC_STATE].v = ALLOC_STATE_FREED;
64 printf("# %p freed\n", p);
70 n = (n + sizeof *alloc - 1) / sizeof *alloc;
71 alloc = (malloc)((n + ALLOC_PAYLOAD) * sizeof *alloc);
75 alloc[ALLOC_NEXT_PTR].p = alloc_head;
78 alloc[ALLOC_STATE].v = ALLOC_STATE_ALLOCATED;
79 alloc[ALLOC_SIZE].v = n * sizeof *alloc;
82 n = old_alloc[ALLOC_SIZE].v;
83 memcpy(&alloc[ALLOC_PAYLOAD], &old_alloc[ALLOC_PAYLOAD], n);
86 p = &alloc[ALLOC_PAYLOAD];
87 printf("# %p allocated\n", p);
92 * Returns the total number of allocations that have not yet been freed.
94 size_t test_live_allocations(void)
99 for (a = alloc_head; a; a = a[ALLOC_NEXT_PTR].p) {
100 void *p = &a[ALLOC_PAYLOAD];
102 switch (a[ALLOC_STATE].v) {
103 case ALLOC_STATE_ALLOCATED:
104 printf("# %p still live\n", p);
107 case ALLOC_STATE_FREED:
110 printf("Bail out! detected %p state corruption\n", p);