aligned_alloc.c revision 1.1.1.1 1 1.1 christos #include "test/jemalloc_test.h"
2 1.1 christos
3 1.1 christos #define MAXALIGN (((size_t)1) << 23)
4 1.1 christos
5 1.1 christos /*
6 1.1 christos * On systems which can't merge extents, tests that call this function generate
7 1.1 christos * a lot of dirty memory very quickly. Purging between cycles mitigates
8 1.1 christos * potential OOM on e.g. 32-bit Windows.
9 1.1 christos */
10 1.1 christos static void
11 1.1 christos purge(void) {
12 1.1 christos assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
13 1.1 christos "Unexpected mallctl error");
14 1.1 christos }
15 1.1 christos
16 1.1 christos TEST_BEGIN(test_alignment_errors) {
17 1.1 christos size_t alignment;
18 1.1 christos void *p;
19 1.1 christos
20 1.1 christos alignment = 0;
21 1.1 christos set_errno(0);
22 1.1 christos p = aligned_alloc(alignment, 1);
23 1.1 christos assert_false(p != NULL || get_errno() != EINVAL,
24 1.1 christos "Expected error for invalid alignment %zu", alignment);
25 1.1 christos
26 1.1 christos for (alignment = sizeof(size_t); alignment < MAXALIGN;
27 1.1 christos alignment <<= 1) {
28 1.1 christos set_errno(0);
29 1.1 christos p = aligned_alloc(alignment + 1, 1);
30 1.1 christos assert_false(p != NULL || get_errno() != EINVAL,
31 1.1 christos "Expected error for invalid alignment %zu",
32 1.1 christos alignment + 1);
33 1.1 christos }
34 1.1 christos }
35 1.1 christos TEST_END
36 1.1 christos
37 1.1 christos TEST_BEGIN(test_oom_errors) {
38 1.1 christos size_t alignment, size;
39 1.1 christos void *p;
40 1.1 christos
41 1.1 christos #if LG_SIZEOF_PTR == 3
42 1.1 christos alignment = UINT64_C(0x8000000000000000);
43 1.1 christos size = UINT64_C(0x8000000000000000);
44 1.1 christos #else
45 1.1 christos alignment = 0x80000000LU;
46 1.1 christos size = 0x80000000LU;
47 1.1 christos #endif
48 1.1 christos set_errno(0);
49 1.1 christos p = aligned_alloc(alignment, size);
50 1.1 christos assert_false(p != NULL || get_errno() != ENOMEM,
51 1.1 christos "Expected error for aligned_alloc(%zu, %zu)",
52 1.1 christos alignment, size);
53 1.1 christos
54 1.1 christos #if LG_SIZEOF_PTR == 3
55 1.1 christos alignment = UINT64_C(0x4000000000000000);
56 1.1 christos size = UINT64_C(0xc000000000000001);
57 1.1 christos #else
58 1.1 christos alignment = 0x40000000LU;
59 1.1 christos size = 0xc0000001LU;
60 1.1 christos #endif
61 1.1 christos set_errno(0);
62 1.1 christos p = aligned_alloc(alignment, size);
63 1.1 christos assert_false(p != NULL || get_errno() != ENOMEM,
64 1.1 christos "Expected error for aligned_alloc(%zu, %zu)",
65 1.1 christos alignment, size);
66 1.1 christos
67 1.1 christos alignment = 0x10LU;
68 1.1 christos #if LG_SIZEOF_PTR == 3
69 1.1 christos size = UINT64_C(0xfffffffffffffff0);
70 1.1 christos #else
71 1.1 christos size = 0xfffffff0LU;
72 1.1 christos #endif
73 1.1 christos set_errno(0);
74 1.1 christos p = aligned_alloc(alignment, size);
75 1.1 christos assert_false(p != NULL || get_errno() != ENOMEM,
76 1.1 christos "Expected error for aligned_alloc(&p, %zu, %zu)",
77 1.1 christos alignment, size);
78 1.1 christos }
79 1.1 christos TEST_END
80 1.1 christos
81 1.1 christos TEST_BEGIN(test_alignment_and_size) {
82 1.1 christos #define NITER 4
83 1.1 christos size_t alignment, size, total;
84 1.1 christos unsigned i;
85 1.1 christos void *ps[NITER];
86 1.1 christos
87 1.1 christos for (i = 0; i < NITER; i++) {
88 1.1 christos ps[i] = NULL;
89 1.1 christos }
90 1.1 christos
91 1.1 christos for (alignment = 8;
92 1.1 christos alignment <= MAXALIGN;
93 1.1 christos alignment <<= 1) {
94 1.1 christos total = 0;
95 1.1 christos for (size = 1;
96 1.1 christos size < 3 * alignment && size < (1U << 31);
97 1.1 christos size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
98 1.1 christos for (i = 0; i < NITER; i++) {
99 1.1 christos ps[i] = aligned_alloc(alignment, size);
100 1.1 christos if (ps[i] == NULL) {
101 1.1 christos char buf[BUFERROR_BUF];
102 1.1 christos
103 1.1 christos buferror(get_errno(), buf, sizeof(buf));
104 1.1 christos test_fail(
105 1.1 christos "Error for alignment=%zu, "
106 1.1 christos "size=%zu (%#zx): %s",
107 1.1 christos alignment, size, size, buf);
108 1.1 christos }
109 1.1 christos total += malloc_usable_size(ps[i]);
110 1.1 christos if (total >= (MAXALIGN << 1)) {
111 1.1 christos break;
112 1.1 christos }
113 1.1 christos }
114 1.1 christos for (i = 0; i < NITER; i++) {
115 1.1 christos if (ps[i] != NULL) {
116 1.1 christos free(ps[i]);
117 1.1 christos ps[i] = NULL;
118 1.1 christos }
119 1.1 christos }
120 1.1 christos }
121 1.1 christos purge();
122 1.1 christos }
123 1.1 christos #undef NITER
124 1.1 christos }
125 1.1 christos TEST_END
126 1.1 christos
127 1.1 christos int
128 1.1 christos main(void) {
129 1.1 christos return test(
130 1.1 christos test_alignment_errors,
131 1.1 christos test_oom_errors,
132 1.1 christos test_alignment_and_size);
133 1.1 christos }
134