1 #include <ccan/alloc/alloc.h>
2 #include <ccan/tap/tap.h>
3 #include <ccan/alloc/alloc.c>
9 #define POOL_SIZE (1 << POOL_ORD)
11 static void invert_bytes(unsigned char *p, unsigned long size)
15 for (i = 0; i < size; i++)
19 static bool sizes_ok(void *mem, unsigned long poolsize, void *p[], unsigned num)
23 for (i = 0; i < num; i++)
24 if (alloc_size(mem, poolsize, p[i]) < i)
29 int main(int argc, char *argv[])
37 /* FIXME: Needs to be page aligned for now. */
38 if (posix_memalign(&mem, 1 << POOL_ORD, POOL_SIZE) != 0)
39 errx(1, "Failed allocating aligned memory");
41 alloc_init(mem, POOL_SIZE);
43 /* Check that alloc_size() gives reasonable answers. */
44 for (i = 0; i < POOL_SIZE; i++) {
45 p[i] = alloc_get(mem, POOL_SIZE, i, 1);
48 invert_bytes(p[i], alloc_size(mem, POOL_SIZE, p[i]));
52 ok1(alloc_check(mem, POOL_SIZE));
53 ok1(sizes_ok(mem, POOL_SIZE, p, num));
55 /* Free every second one. */
56 for (i = 0; i < num; i+=2) {
57 alloc_free(mem, POOL_SIZE, p[i]);
61 invert_bytes(p[i/2], alloc_size(mem,POOL_SIZE,p[i/2]));
65 ok1(alloc_check(mem, POOL_SIZE));
66 ok1(sizes_ok(mem, POOL_SIZE, p, num));