headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / system / libroot / posix / memalign_test.cpp
blob3908e05f245b727332b3db66d8674bca0e5dbaab
1 #include <SupportDefs.h>
2 #include <OS.h>
4 #include <malloc.h>
5 #include <stdio.h>
6 #include <stdlib.h>
9 #ifdef MALLOC_DEBUG
10 void dump_heap_list(int argc, char **argv);
11 void dump_allocations(bool statsOnly, thread_id thread);
12 #endif
15 inline uint8
16 sum(addr_t address)
18 return (address >> 24) | (address >> 16) | (address >> 8) | address;
22 inline void
23 write_test_pattern(void *address, size_t size)
25 for (size_t i = 0; i < size; i++)
26 *((uint8 *)address + i) = sum((addr_t)address + i);
30 inline void
31 verify_test_pattern(void *address, size_t size)
33 for (size_t i = 0; i < size; i++) {
34 if (*((uint8 *)address + i) != sum((addr_t)address + i)) {
35 printf("test patern invalid at %p: %u vs. %u\n",
36 (uint8 *)address + i, *((uint8 *)address + i),
37 sum((addr_t)address + i));
38 exit(1);
44 void
45 allocate_random_no_alignment(int32 count, size_t maxSize)
47 void **allocations = new void *[count];
48 size_t *sizes = new size_t[count];
49 for (int32 i = 0; i < count; i++) {
50 sizes[i] = rand() % maxSize;
51 allocations[i] = malloc(sizes[i]);
52 if (allocations[i] == NULL) {
53 printf("allocation of %lu bytes failed\n", sizes[i]);
54 exit(1);
56 #ifdef __x86_64__
57 if (((addr_t)allocations[i] & 0xf) != 0) {
58 printf("allocation %p not aligned failed\n",
59 allocations[i]);
60 exit(1);
62 #endif
63 write_test_pattern(allocations[i], sizes[i]);
66 for (int32 i = count - 1; i >= 0; i--) {
67 verify_test_pattern(allocations[i], sizes[i]);
68 free(allocations[i]);
71 delete[] allocations;
72 delete[] sizes;
76 void
77 allocate_random_fixed_alignment(int32 count, size_t maxSize, size_t alignment)
79 void **allocations = new void *[count];
80 size_t *sizes = new size_t[count];
81 for (int32 i = 0; i < count; i++) {
82 sizes[i] = rand() % maxSize;
83 allocations[i] = memalign(alignment, sizes[i]);
84 if (allocations[i] == NULL) {
85 printf("allocation of %lu bytes failed\n", sizes[i]);
86 exit(1);
89 if ((addr_t)allocations[i] % alignment != 0) {
90 printf("allocation of %lu bytes misaligned: %p -> 0x%08lx "
91 " with alignment %lu (0x%08lx)\n", sizes[i],
92 allocations[i], (addr_t)allocations[i] % alignment, alignment,
93 alignment);
94 exit(1);
97 write_test_pattern(allocations[i], sizes[i]);
100 for (int32 i = count - 1; i >= 0; i--) {
101 verify_test_pattern(allocations[i], sizes[i]);
102 free(allocations[i]);
105 delete[] allocations;
106 delete[] sizes;
110 void
111 allocate_random_random_alignment(int32 count, size_t maxSize)
113 for (int32 i = 0; i < count / 128; i++)
114 allocate_random_fixed_alignment(128, maxSize, 1 << (rand() % 18));
119 main(int argc, char *argv[])
121 allocate_random_no_alignment(1024, B_PAGE_SIZE * 128);
122 allocate_random_random_alignment(1024, B_PAGE_SIZE * 128);
124 #ifdef MALLOC_DEBUG
125 dump_heap_list(0, NULL);
126 dump_allocations(false, -1);
127 #endif
129 printf("tests succeeded\n");
130 return 0;