1 #include <SupportDefs.h>
10 void dump_heap_list(int argc
, char **argv
);
11 void dump_allocations(bool statsOnly
, thread_id thread
);
18 return (address
>> 24) | (address
>> 16) | (address
>> 8) | address
;
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
);
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
));
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
]);
57 if (((addr_t
)allocations
[i
] & 0xf) != 0) {
58 printf("allocation %p not aligned failed\n",
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
]);
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
]);
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
,
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
;
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);
125 dump_heap_list(0, NULL
);
126 dump_allocations(false, -1);
129 printf("tests succeeded\n");