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 | |
28 | #include "tst-malloc-aux.h" |
29 | |
30 | static int |
31 | do_test (void) |
32 | { |
33 | void *p1; |
34 | void *p2; |
35 | void *p3; |
36 | void *p4; |
37 | void *p5; |
38 | |
39 | errno = 0; |
40 | |
41 | /* The implementation supports alignments that are non-negative powers of 2. |
42 | We test 5 distinct conditions here: |
43 | - A non-negative power of 2 alignment e.g. 64. |
44 | - A degenerate zero power of 2 alignment e.g. 1. |
45 | - A non-power-of-2 alignment e.g. 65. |
46 | - A zero alignment. |
47 | - A corner case SIZE_MAX / 2 + 1 alignment. |
48 | */ |
49 | |
50 | p1 = aligned_alloc (64, 64); |
51 | |
52 | if (p1 == NULL) |
53 | FAIL_EXIT1 ("aligned_alloc(64, 64) failed" ); |
54 | |
55 | p2 = aligned_alloc (1, 64); |
56 | |
57 | if (p2 == NULL) |
58 | FAIL_EXIT1 ("aligned_alloc(1, 64) failed" ); |
59 | |
60 | p3 = aligned_alloc (65, 64); |
61 | |
62 | if (p3 != NULL) |
63 | FAIL_EXIT1 ("aligned_alloc(65, 64) did not fail" ); |
64 | |
65 | p4 = aligned_alloc (0, 64); |
66 | |
67 | if (p4 != NULL) |
68 | FAIL_EXIT1 ("aligned_alloc(0, 64) did not fail" ); |
69 | |
70 | /* This is an alignment like 0x80000000...UL */ |
71 | p5 = aligned_alloc (SIZE_MAX / 2 + 1, 64); |
72 | |
73 | if (p5 != NULL) |
74 | FAIL_EXIT1 ("aligned_alloc(SIZE_MAX/2+1, 64) did not fail" ); |
75 | |
76 | free (ptr: p1); |
77 | free (ptr: p2); |
78 | return 0; |
79 | } |
80 | |
81 | #define TEST_FUNCTION do_test () |
82 | #include "../test-skeleton.c" |
83 | |