1/* Test for C17 alignment requirements.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <malloc.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <libc-diag.h>
26#include <support/check.h>
27
28static int
29do_test (void)
30{
31 void *p1;
32 void *p2;
33 void *p3;
34 void *p4;
35 void *p5;
36
37 errno = 0;
38
39 /* The implementation supports alignments that are non-negative powers of 2.
40 We test 5 distinct conditions here:
41 - A non-negative power of 2 alignment e.g. 64.
42 - A degenerate zero power of 2 alignment e.g. 1.
43 - A non-power-of-2 alignment e.g. 65.
44 - A zero alignment.
45 - A corner case SIZE_MAX / 2 + 1 alignment.
46 */
47
48 p1 = aligned_alloc (alignment: 64, size: 64);
49
50 if (p1 == NULL)
51 FAIL_EXIT1 ("aligned_alloc(64, 64) failed");
52
53 p2 = aligned_alloc (alignment: 1, size: 64);
54
55 if (p2 == NULL)
56 FAIL_EXIT1 ("aligned_alloc(1, 64) failed");
57
58 p3 = aligned_alloc (alignment: 65, size: 64);
59
60 if (p3 != NULL)
61 FAIL_EXIT1 ("aligned_alloc(65, 64) did not fail");
62
63 p4 = aligned_alloc (alignment: 0, size: 64);
64
65 if (p4 != NULL)
66 FAIL_EXIT1 ("aligned_alloc(0, 64) did not fail");
67
68 /* This is an alignment like 0x80000000...UL */
69 p5 = aligned_alloc (SIZE_MAX / 2 + 1, size: 64);
70
71 if (p5 != NULL)
72 FAIL_EXIT1 ("aligned_alloc(SIZE_MAX/2+1, 64) did not fail");
73
74 free (ptr: p1);
75 free (ptr: p2);
76 return 0;
77}
78
79#define TEST_FUNCTION do_test ()
80#include "../test-skeleton.c"
81

source code of glibc/malloc/tst-aligned-alloc.c