1 #include "alloc/alloc.h"
3 #include "alloc/alloc.c"
7 #define POOL_SIZE (1 << POOL_ORD)
9 #define sort(p, num, cmp) \
10 qsort((p), (num), sizeof(*p), (int(*)(const void *, const void *))cmp)
12 static int addr_cmp(void **a, void **b)
17 static bool unique(void *p[], unsigned int num)
21 for (i = 1; i < num; i++)
27 static bool free_every_second_one(void *mem, unsigned int num, void *p[])
31 /* Free every second one. */
32 for (i = 0; i < num; i += 2) {
33 alloc_free(mem, POOL_SIZE, p[i]);
34 if (!alloc_check(mem, POOL_SIZE))
37 for (i = 1; i < num; i += 2) {
38 alloc_free(mem, POOL_SIZE, p[i]);
39 if (!alloc_check(mem, POOL_SIZE))
46 int main(int argc, char *argv[])
49 unsigned int i, num, max_size;
54 /* FIXME: Needs to be page aligned for now. */
55 posix_memalign(&mem, 1 << POOL_ORD, POOL_SIZE);
57 /* Small pool, all allocs fail, even 0-length. */
59 ok1(alloc_check(mem, 0));
60 ok1(alloc_get(mem, 0, 1, 1) == NULL);
61 ok1(alloc_get(mem, 0, 128, 1) == NULL);
62 ok1(alloc_get(mem, 0, 0, 1) == NULL);
64 /* Free of NULL should work. */
65 alloc_free(mem, 0, NULL);
67 alloc_init(mem, POOL_SIZE);
68 ok1(alloc_check(mem, POOL_SIZE));
69 /* Find largest allocation which works. */
70 for (max_size = POOL_SIZE * 2; max_size; max_size--) {
71 p[0] = alloc_get(mem, POOL_SIZE, max_size, 1);
75 ok1(max_size < POOL_SIZE);
77 ok1(alloc_check(mem, POOL_SIZE));
79 /* Free it, should be able to reallocate it. */
80 alloc_free(mem, POOL_SIZE, p[0]);
81 ok1(alloc_check(mem, POOL_SIZE));
83 p[0] = alloc_get(mem, POOL_SIZE, max_size, 1);
85 ok1(alloc_check(mem, POOL_SIZE));
86 alloc_free(mem, POOL_SIZE, p[0]);
87 ok1(alloc_check(mem, POOL_SIZE));
89 /* Allocate a whole heap. */
90 for (i = 0; i < POOL_SIZE; i++) {
91 p[i] = alloc_get(mem, POOL_SIZE, 1, 1);
96 /* Uncomment this for a more intuitive view of what the
97 * allocator looks like after all these 1 byte allocs. */
99 alloc_visualize(stderr, mem, POOL_SIZE);
103 /* Can't allocate this many. */
104 ok1(num != POOL_SIZE);
105 ok1(alloc_check(mem, POOL_SIZE));
108 sort(p, num, addr_cmp);
110 /* Uniqueness check */
113 ok1(free_every_second_one(mem, num, p));
114 ok1(alloc_check(mem, POOL_SIZE));
116 /* Should be able to reallocate max size. */
117 p[0] = alloc_get(mem, POOL_SIZE, max_size, 1);
119 ok1(alloc_check(mem, POOL_SIZE));
121 /* Re-initializing should be the same as freeing everything */
122 alloc_init(mem, POOL_SIZE);
123 ok1(alloc_check(mem, POOL_SIZE));
124 p[0] = alloc_get(mem, POOL_SIZE, max_size, 1);
126 ok1(alloc_check(mem, POOL_SIZE));
127 alloc_free(mem, POOL_SIZE, p[0]);
128 ok1(alloc_check(mem, POOL_SIZE));
130 /* Alignment constraints should be met, as long as powers of two */
131 for (i = 0; i < POOL_ORD-1; i++) {
132 p[i] = alloc_get(mem, POOL_SIZE, i, 1 << i);
134 ok1(((unsigned long)p[i] % (1 << i)) == 0);
135 ok1(alloc_check(mem, POOL_SIZE));
138 for (i = 0; i < POOL_ORD-1; i++) {
139 alloc_free(mem, POOL_SIZE, p[i]);
140 ok1(alloc_check(mem, POOL_SIZE));
143 /* Alignment constraints for a single-byte allocation. */
144 for (i = 0; i < POOL_ORD; i++) {
145 p[0] = alloc_get(mem, POOL_SIZE, 1, 1 << i);
147 ok1(alloc_check(mem, POOL_SIZE));
148 alloc_free(mem, POOL_SIZE, p[0]);
149 ok1(alloc_check(mem, POOL_SIZE));
152 /* Alignment check for a 0-byte allocation. Corner case. */
153 p[0] = alloc_get(mem, POOL_SIZE, 0, 1 << (POOL_ORD - 1));
154 ok1(alloc_check(mem, POOL_SIZE));
155 alloc_free(mem, POOL_SIZE, p[0]);
156 ok1(alloc_check(mem, POOL_SIZE));
158 /* Force the testing of split metadata. */
159 alloc_init(mem, POOL_SIZE);
160 for (i = 0; i < POOL_SIZE; i++) {
161 p[i] = alloc_get(mem, POOL_SIZE, getpagesize(), getpagesize());
165 ok1(alloc_check(mem, POOL_SIZE));
168 sort(p, i-1, addr_cmp);
170 /* Free all but the one next to the metadata. */
171 for (i = 1; p[i]; i++)
172 alloc_free(mem, POOL_SIZE, p[i]);
173 ok1(alloc_check(mem, POOL_SIZE));
175 /* Now do a whole heap of subpage allocs. */
176 for (i = 1; i < POOL_SIZE; i++) {
177 p[i] = alloc_get(mem, POOL_SIZE, 1, 1);
181 ok1(alloc_check(mem, POOL_SIZE));
183 /* Free up our page next to metadata, and should be able to alloc */
184 alloc_free(mem, POOL_SIZE, p[0]);
185 ok1(alloc_check(mem, POOL_SIZE));
186 p[0] = alloc_get(mem, POOL_SIZE, 1, 1);
190 for (i = 0; p[i]; i++)
191 alloc_free(mem, POOL_SIZE, p[i]);
192 ok1(alloc_check(mem, POOL_SIZE));
194 return exit_status();