1 /*****************************************************************************
2 * aligned_alloc test case
3 *****************************************************************************
4 * Copyright © 2019 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
30 struct big_align_struct
{
35 /* Supported alignments. Others are undefined (ISO C11 §7.22.3, §J.2). */
36 static const size_t alignv
[] = {
44 alignof (struct big_align_struct
),
46 alignof (max_align_t
),
49 static const size_t alignc
= sizeof (alignv
) / sizeof (alignv
[0]);
51 static void test_posix_memalign(size_t align
, size_t size
)
54 int val
= posix_memalign(&p
, align
, size
);
56 if (align
>= sizeof (void *)) {
58 assert(((uintptr_t)p
& (align
- 1)) == 0);
71 for (size_t i
= 0; i
< alignc
; i
++) {
72 size_t align
= alignv
[i
];
74 assert((align
& (align
- 1)) == 0); /* must be a power of two */
76 p
= aligned_alloc(alignv
[i
], 0);
77 free(p
); /* must free {aligned_,c,m,c,re}alloc() allocations */
79 for (size_t j
= 0; j
< alignc
; j
++) {
80 size_t size
= alignv
[j
];
83 continue; /* size must be a multiple of alignment */
85 p
= aligned_alloc(align
, size
);
86 assert(p
!= NULL
); /* small non-zero bytes allocation */
87 assert(((uintptr_t)p
& (align
- 1)) == 0);
92 /* posix_memalign() */
94 for (size_t i
= 0; i
< 21; i
++) {
95 size_t align
= (size_t)1 << i
;
97 test_posix_memalign(align
, 0);
98 test_posix_memalign(align
, 1);
99 test_posix_memalign(align
, align
- 1);
100 test_posix_memalign(align
, align
);
101 test_posix_memalign(align
, align
* 2);