xref: /haiku/src/tests/system/libroot/posix/memalign_test.cpp (revision 4466b89c65970de4c7236ac87faa2bee4589f413)
1 #include <SupportDefs.h>
2 #include <OS.h>
3 
4 #include <malloc.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 
8 
9 #ifdef MALLOC_DEBUG
10 void dump_heap_list(int argc, char **argv);
11 void dump_allocations(bool statsOnly, thread_id thread);
12 #endif
13 
14 
15 inline uint8
16 sum(addr_t address)
17 {
18 	return (address >> 24) | (address >> 16) | (address >> 8) | address;
19 }
20 
21 
22 inline void
23 write_test_pattern(void *address, size_t size)
24 {
25 	for (size_t i = 0; i < size; i++)
26 		*((uint8 *)address + i) = sum((addr_t)address + i);
27 }
28 
29 
30 inline void
31 verify_test_pattern(void *address, size_t size)
32 {
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);
39 		}
40 	}
41 }
42 
43 
44 void
45 allocate_random_no_alignment(int32 count, size_t maxSize)
46 {
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);
55 		}
56 
57 		write_test_pattern(allocations[i], sizes[i]);
58 	}
59 
60 	for (int32 i = count - 1; i >= 0; i--) {
61 		verify_test_pattern(allocations[i], sizes[i]);
62 		free(allocations[i]);
63 	}
64 
65 	delete[] allocations;
66 	delete[] sizes;
67 }
68 
69 
70 void
71 allocate_random_fixed_alignment(int32 count, size_t maxSize, size_t alignment)
72 {
73 	void **allocations = new void *[count];
74 	size_t *sizes = new size_t[count];
75 	for (int32 i = 0; i < count; i++) {
76 		sizes[i] = rand() % maxSize;
77 		allocations[i] = memalign(alignment, sizes[i]);
78 		if (allocations[i] == NULL) {
79 			printf("allocation of %lu bytes failed\n", sizes[i]);
80 			exit(1);
81 		}
82 
83 		if ((addr_t)allocations[i] % alignment != 0) {
84 			printf("allocation of %lu bytes misaligned: %p -> 0x%08lx "
85 				" with alignment %lu (0x%08lx)\n", sizes[i],
86 				allocations[i], (addr_t)allocations[i] % alignment, alignment,
87 				alignment);
88 			exit(1);
89 		}
90 
91 		write_test_pattern(allocations[i], sizes[i]);
92 	}
93 
94 	for (int32 i = count - 1; i >= 0; i--) {
95 		verify_test_pattern(allocations[i], sizes[i]);
96 		free(allocations[i]);
97 	}
98 
99 	delete[] allocations;
100 	delete[] sizes;
101 }
102 
103 
104 void
105 allocate_random_random_alignment(int32 count, size_t maxSize)
106 {
107 	for (int32 i = 0; i < count / 128; i++)
108 		allocate_random_fixed_alignment(128, maxSize, 1 << (rand() % 18));
109 }
110 
111 
112 int
113 main(int argc, char *argv[])
114 {
115 	allocate_random_no_alignment(1024, B_PAGE_SIZE * 128);
116 	allocate_random_random_alignment(1024, B_PAGE_SIZE * 128);
117 
118 #ifdef MALLOC_DEBUG
119 	dump_heap_list(0, NULL);
120 	dump_allocations(false, -1);
121 #endif
122 
123 	printf("tests succeeded\n");
124 	return 0;
125 }
126