8 unsigned long size = 1048576;
9 unsigned long chunk = 4096;
10 unsigned long nr_threads = 4;
11 unsigned long nr_frees;
14 #define full_size(_x_) ((_x_) + sizeof(struct xheap_header))
15 void print_ptr(void *ptr)
17 uint64_t size = xheap_get_chunk_size(ptr);
18 printf("ptr: %lx, bytes: %llu (actual: %llu)\n",
20 (unsigned long long) size,
21 (unsigned long long) full_size(size));
24 unsigned long test(struct xheap *heap)
26 int r = xheap_init(heap, size, al_unit, mem);
28 printf("xheap init error\n");
32 uint64_t alloc_size = 0, start = heap->cur;
33 printf("heap starts with %llu\n", start);
36 ptr = xheap_allocate(heap, chunk);
39 if ((unsigned long) ptr & ((1 << al_unit) -1)) {
40 printf("ptr %x not aligned with al_unit %lu\n",
44 alloc_size += xheap_get_chunk_size(ptr);
47 printf("Allocated %lu chunks of size %lu with allocation unit %lu (%lu bytes)\n",
48 i, chunk, al_unit, (1<<al_unit));
49 printf("Total allocated space: %llu\n", (unsigned long long) alloc_size);
50 alloc_size += i*sizeof(struct xheap_header);
51 printf("Total allocated space plus headers: %llu\n",
52 (unsigned long long) alloc_size);
53 alloc_size += start + 2 * sizeof(struct xheap_header);
54 printf("Total allocated space plus headers and start/end padding: %llu\n",
55 (unsigned long long) alloc_size);
56 printf("Lost space: %llu\n", (unsigned long long) (heap->size - alloc_size));
60 int test_align(struct xheap *heap)
62 uint64_t bytes = heap->size;
63 int m = sizeof(bytes)*8 - __builtin_clzl(bytes -1);
66 for (i = 5; i < m; i++){
67 int r = xheap_init(heap, size, i, mem);
69 printf("align: xheap init error\n");
73 ptr = xheap_allocate(heap, chunk);
75 if ((unsigned long) ptr & ((1 << i) -1)) {
76 printf("ptr %x not aligned with al_unit %lu\n",
86 int test_reuse(struct xheap *heap)
88 int r = xheap_init(heap, size, al_unit, mem);
90 printf("xheap init error\n");
94 ptr1 = xheap_allocate(heap, chunk);
96 printf("couldn't allocate\n");
100 ptr2 = xheap_allocate(heap, chunk);
102 printf("couldn't allocate\n");
106 printf("alloc-free-alloc return diffrent ptr\n");
116 unsigned long allocations;
120 void *thread_test(void *arg)
122 struct thread_arg *targ = (struct thread_arg *) arg;
123 struct xheap *heap = targ->heap;
125 unsigned long c = targ->c;
129 ptr = xheap_allocate(heap, chunk);
132 memset(ptr, 1, xheap_get_chunk_size(ptr));
138 }while (ptr != NULL);
140 targ->allocations = i;
141 targ->frees = targ->c - c;
145 unsigned long test_threads(struct xheap *heap)
148 unsigned long allocations = 0;
151 int r = xheap_init(heap, size, al_unit, mem);
153 printf("threads: xheap init error\n");
157 struct thread_arg *targs = malloc(sizeof(struct thread_arg) * nr_threads);
159 printf("error malloc\n");
163 pthread_t *threads = malloc(sizeof(pthread_t) * nr_threads);
165 printf("error malloc\n");
169 for (i = 0; i < nr_threads; i++) {
171 targs[i].heap = heap;
175 for (i = 0; i < nr_threads; i++) {
176 r = pthread_create(&threads[i], NULL, thread_test, &targs[i]);
178 printf("error pthread_create\n");
183 for (i = 0; i < nr_threads; i++) {
184 pthread_join(threads[i], NULL);
185 allocations += targs[i].allocations;
186 nr_frees += targs[i].frees;
191 int main(int argc, const char *argv[])
193 unsigned long alloc, expected, i =0;
196 printf("Usage: %s al_unit size chunk_size nr_threads\n", argv[0]);
199 al_unit = atol(argv[1]);
200 size = atol(argv[2]);
201 chunk = atol(argv[3]);
202 nr_threads = atol(argv[4]);
207 printf("Testing align: ");
208 r = test_align(&heap);
214 printf("Testing reuse: ");
215 r= test_reuse(&heap);
220 expected = alloc + nr_frees;
222 while(r == expected && i < 100) {
223 r = test_threads(&heap);
224 expected = alloc + nr_frees;
226 printf("test_threads failed r: %lu vs expected %lu\n", r, expected);