]>
Commit | Line | Data |
---|---|---|
a78e148b | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <stdint.h> | |
4 | ||
5 | #define JEMALLOC_MANGLE | |
6 | #include "jemalloc_test.h" | |
7 | ||
8 | #define CHUNK 0x400000 | |
9 | /* #define MAXALIGN ((size_t)0x80000000000LLU) */ | |
10 | #define MAXALIGN ((size_t)0x2000000LLU) | |
11 | #define NITER 4 | |
12 | ||
13 | int | |
14 | main(void) | |
15 | { | |
16 | int r; | |
17 | void *p; | |
18 | size_t sz, alignment, total, tsz; | |
19 | unsigned i; | |
20 | void *ps[NITER]; | |
21 | ||
22 | fprintf(stderr, "Test begin\n"); | |
23 | ||
24 | sz = 0; | |
25 | r = JEMALLOC_P(allocm)(&p, &sz, 42, 0); | |
26 | if (r != ALLOCM_SUCCESS) { | |
27 | fprintf(stderr, "Unexpected allocm() error\n"); | |
28 | abort(); | |
29 | } | |
30 | if (sz < 42) | |
31 | fprintf(stderr, "Real size smaller than expected\n"); | |
32 | if (JEMALLOC_P(dallocm)(p, 0) != ALLOCM_SUCCESS) | |
33 | fprintf(stderr, "Unexpected dallocm() error\n"); | |
34 | ||
35 | r = JEMALLOC_P(allocm)(&p, NULL, 42, 0); | |
36 | if (r != ALLOCM_SUCCESS) { | |
37 | fprintf(stderr, "Unexpected allocm() error\n"); | |
38 | abort(); | |
39 | } | |
40 | if (JEMALLOC_P(dallocm)(p, 0) != ALLOCM_SUCCESS) | |
41 | fprintf(stderr, "Unexpected dallocm() error\n"); | |
42 | ||
43 | r = JEMALLOC_P(allocm)(&p, NULL, 42, ALLOCM_ZERO); | |
44 | if (r != ALLOCM_SUCCESS) { | |
45 | fprintf(stderr, "Unexpected allocm() error\n"); | |
46 | abort(); | |
47 | } | |
48 | if (JEMALLOC_P(dallocm)(p, 0) != ALLOCM_SUCCESS) | |
49 | fprintf(stderr, "Unexpected dallocm() error\n"); | |
50 | ||
51 | #if LG_SIZEOF_PTR == 3 | |
52 | alignment = 0x8000000000000000LLU; | |
53 | sz = 0x8000000000000000LLU; | |
54 | #else | |
55 | alignment = 0x80000000LU; | |
56 | sz = 0x80000000LU; | |
57 | #endif | |
58 | r = JEMALLOC_P(allocm)(&p, NULL, sz, ALLOCM_ALIGN(alignment)); | |
59 | if (r == ALLOCM_SUCCESS) { | |
60 | fprintf(stderr, | |
61 | "Expected error for allocm(&p, %zu, 0x%x)\n", | |
62 | sz, ALLOCM_ALIGN(alignment)); | |
63 | } | |
64 | ||
65 | #if LG_SIZEOF_PTR == 3 | |
66 | alignment = 0x4000000000000000LLU; | |
67 | sz = 0x8400000000000001LLU; | |
68 | #else | |
69 | alignment = 0x40000000LU; | |
70 | sz = 0x84000001LU; | |
71 | #endif | |
72 | r = JEMALLOC_P(allocm)(&p, NULL, sz, ALLOCM_ALIGN(alignment)); | |
73 | if (r == ALLOCM_SUCCESS) { | |
74 | fprintf(stderr, | |
75 | "Expected error for allocm(&p, %zu, 0x%x)\n", | |
76 | sz, ALLOCM_ALIGN(alignment)); | |
77 | } | |
78 | ||
79 | alignment = 0x10LLU; | |
80 | #if LG_SIZEOF_PTR == 3 | |
81 | sz = 0xfffffffffffffff0LLU; | |
82 | #else | |
83 | sz = 0xfffffff0LU; | |
84 | #endif | |
85 | r = JEMALLOC_P(allocm)(&p, NULL, sz, ALLOCM_ALIGN(alignment)); | |
86 | if (r == ALLOCM_SUCCESS) { | |
87 | fprintf(stderr, | |
88 | "Expected error for allocm(&p, %zu, 0x%x)\n", | |
89 | sz, ALLOCM_ALIGN(alignment)); | |
90 | } | |
91 | ||
92 | for (i = 0; i < NITER; i++) | |
93 | ps[i] = NULL; | |
94 | ||
95 | for (alignment = 8; | |
96 | alignment <= MAXALIGN; | |
97 | alignment <<= 1) { | |
98 | total = 0; | |
99 | fprintf(stderr, "Alignment: %zu\n", alignment); | |
100 | for (sz = 1; | |
101 | sz < 3 * alignment && sz < (1U << 31); | |
102 | sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { | |
103 | for (i = 0; i < NITER; i++) { | |
104 | r = JEMALLOC_P(allocm)(&ps[i], NULL, sz, | |
105 | ALLOCM_ALIGN(alignment) | ALLOCM_ZERO); | |
106 | if (r != ALLOCM_SUCCESS) { | |
107 | fprintf(stderr, | |
108 | "Error for size %zu (0x%zx): %d\n", | |
109 | sz, sz, r); | |
110 | exit(1); | |
111 | } | |
112 | if ((uintptr_t)p & (alignment-1)) { | |
113 | fprintf(stderr, | |
114 | "%p inadequately aligned for" | |
115 | " alignment: %zu\n", p, alignment); | |
116 | } | |
117 | JEMALLOC_P(sallocm)(ps[i], &tsz, 0); | |
118 | total += tsz; | |
119 | if (total >= (MAXALIGN << 1)) | |
120 | break; | |
121 | } | |
122 | for (i = 0; i < NITER; i++) { | |
123 | if (ps[i] != NULL) { | |
124 | JEMALLOC_P(dallocm)(ps[i], 0); | |
125 | ps[i] = NULL; | |
126 | } | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
131 | fprintf(stderr, "Test end\n"); | |
132 | return (0); | |
133 | } |