]>
Commit | Line | Data |
---|---|---|
1 | #define JEMALLOC_MANGLE | |
2 | #include "jemalloc_test.h" | |
3 | ||
4 | #define CHUNK 0x400000 | |
5 | /* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */ | |
6 | #define MAXALIGN ((size_t)0x2000000LU) | |
7 | #define NITER 4 | |
8 | ||
9 | int | |
10 | main(void) | |
11 | { | |
12 | size_t alignment, size, total; | |
13 | unsigned i; | |
14 | int err; | |
15 | void *p, *ps[NITER]; | |
16 | ||
17 | malloc_printf("Test begin\n"); | |
18 | ||
19 | /* Test error conditions. */ | |
20 | for (alignment = 0; alignment < sizeof(void *); alignment++) { | |
21 | err = posix_memalign(&p, alignment, 1); | |
22 | if (err != EINVAL) { | |
23 | malloc_printf( | |
24 | "Expected error for invalid alignment %zu\n", | |
25 | alignment); | |
26 | } | |
27 | } | |
28 | ||
29 | for (alignment = sizeof(size_t); alignment < MAXALIGN; | |
30 | alignment <<= 1) { | |
31 | err = posix_memalign(&p, alignment + 1, 1); | |
32 | if (err == 0) { | |
33 | malloc_printf( | |
34 | "Expected error for invalid alignment %zu\n", | |
35 | alignment + 1); | |
36 | } | |
37 | } | |
38 | ||
39 | #if LG_SIZEOF_PTR == 3 | |
40 | alignment = UINT64_C(0x8000000000000000); | |
41 | size = UINT64_C(0x8000000000000000); | |
42 | #else | |
43 | alignment = 0x80000000LU; | |
44 | size = 0x80000000LU; | |
45 | #endif | |
46 | err = posix_memalign(&p, alignment, size); | |
47 | if (err == 0) { | |
48 | malloc_printf( | |
49 | "Expected error for posix_memalign(&p, %zu, %zu)\n", | |
50 | alignment, size); | |
51 | } | |
52 | ||
53 | #if LG_SIZEOF_PTR == 3 | |
54 | alignment = UINT64_C(0x4000000000000000); | |
55 | size = UINT64_C(0x8400000000000001); | |
56 | #else | |
57 | alignment = 0x40000000LU; | |
58 | size = 0x84000001LU; | |
59 | #endif | |
60 | err = posix_memalign(&p, alignment, size); | |
61 | if (err == 0) { | |
62 | malloc_printf( | |
63 | "Expected error for posix_memalign(&p, %zu, %zu)\n", | |
64 | alignment, size); | |
65 | } | |
66 | ||
67 | alignment = 0x10LU; | |
68 | #if LG_SIZEOF_PTR == 3 | |
69 | size = UINT64_C(0xfffffffffffffff0); | |
70 | #else | |
71 | size = 0xfffffff0LU; | |
72 | #endif | |
73 | err = posix_memalign(&p, alignment, size); | |
74 | if (err == 0) { | |
75 | malloc_printf( | |
76 | "Expected error for posix_memalign(&p, %zu, %zu)\n", | |
77 | alignment, size); | |
78 | } | |
79 | ||
80 | for (i = 0; i < NITER; i++) | |
81 | ps[i] = NULL; | |
82 | ||
83 | for (alignment = 8; | |
84 | alignment <= MAXALIGN; | |
85 | alignment <<= 1) { | |
86 | total = 0; | |
87 | malloc_printf("Alignment: %zu\n", alignment); | |
88 | for (size = 1; | |
89 | size < 3 * alignment && size < (1U << 31); | |
90 | size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { | |
91 | for (i = 0; i < NITER; i++) { | |
92 | err = posix_memalign(&ps[i], | |
93 | alignment, size); | |
94 | if (err) { | |
95 | malloc_printf( | |
96 | "Error for size %zu (%#zx): %s\n", | |
97 | size, size, strerror(err)); | |
98 | exit(1); | |
99 | } | |
100 | total += malloc_usable_size(ps[i]); | |
101 | if (total >= (MAXALIGN << 1)) | |
102 | break; | |
103 | } | |
104 | for (i = 0; i < NITER; i++) { | |
105 | if (ps[i] != NULL) { | |
106 | free(ps[i]); | |
107 | ps[i] = NULL; | |
108 | } | |
109 | } | |
110 | } | |
111 | } | |
112 | ||
113 | malloc_printf("Test end\n"); | |
114 | return (0); | |
115 | } |