1 // commit: 3e936ce81bbbcc968f576aedbd5203621839f152 2014-09-19
2 // flockfile linked list handling was broken
9 #define t_fatal(...) (t_error(__VA_ARGS__), _Exit(t_status))
10 #define length(a) (sizeof(a)/sizeof*(a))
12 // interpose malloc functions
13 // freed memory is not reused, it is checked for clobber.
15 static unsigned char buf[1<<20];
24 void *malloc(size_t n)
27 if (n > sizeof buf - pos)
28 t_fatal("test buffer is small, pos: %zu, need: %zu\n", pos, n);
29 if (idx >= length(alloc))
30 t_fatal("test buffer is small, idx: %d\n", idx);
39 void *calloc(size_t n, size_t m)
41 return memset(malloc(n*m), 0, n*m);
44 void *aligned_alloc(size_t a, size_t n)
46 t_fatal("aligned_alloc is unsupported\n");
49 static int findidx(void *p)
51 size_t pos = (unsigned char *)p - buf;
52 for (int i=0; i<idx; i++)
53 if (alloc[i].pos == pos)
55 t_fatal("%p is not an allocated pointer\n", p);
59 void *realloc(void *p, size_t n)
62 size_t m = alloc[findidx(p)].n;
63 memcpy(q, p, m < n ? m : n);
72 memset(p, 42, alloc[i].n);
76 static void checkfreed(void)
78 for (int i=0; i<idx; i++)
80 for (size_t j=0; j<alloc[i].n; j++)
81 if (buf[alloc[i].pos + j] != 42) {
82 t_error("freed allocation %d (pos: %zu, len: %zu) is clobbered\n", i, alloc[i].pos, alloc[i].n);
95 /* may corrupt memory */