1 /* Test of allocating memory with given alignment.
3 Copyright (C) 2020-2025 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2020. */
31 #if HAVE_ALIGNED_ALLOC
32 void *(*volatile my_aligned_alloc
) (size_t, size_t) = aligned_alloc
;
34 # define aligned_alloc my_aligned_alloc
37 #define ROUNDUP(x,y) (((x) + (y) - 1) & - (y))
40 main (int argc
, char *argv
[])
42 #if HAVE_ALIGNED_ALLOC
43 static size_t sizes
[] =
45 13, 8, 17, 450, 320, 1, 99, 4, 15, 16, 2, 76, 37, 127, 2406, 641, 5781,
46 /* Test also a zero size. */
49 void *aligned2_blocks
[SIZEOF (sizes
)];
50 void *aligned4_blocks
[SIZEOF (sizes
)];
51 void *aligned8_blocks
[SIZEOF (sizes
)];
52 void *aligned16_blocks
[SIZEOF (sizes
)];
53 void *aligned32_blocks
[SIZEOF (sizes
)];
54 void *aligned64_blocks
[SIZEOF (sizes
)];
57 for (i
= 0; i
< SIZEOF (sizes
); i
++)
59 size_t size
= sizes
[i
];
61 aligned2_blocks
[i
] = aligned_alloc (2, ROUNDUP (size
, 2));
62 ASSERT (aligned2_blocks
[i
] != NULL
);
63 ASSERT (((uintptr_t) aligned2_blocks
[i
] % 2) == 0);
64 memset (aligned2_blocks
[i
], 'u', size
);
66 aligned4_blocks
[i
] = aligned_alloc (4, ROUNDUP (size
, 4));
67 ASSERT (aligned4_blocks
[i
] != NULL
);
68 ASSERT (((uintptr_t) aligned4_blocks
[i
] % 4) == 0);
69 memset (aligned4_blocks
[i
], 'v', size
);
71 aligned8_blocks
[i
] = aligned_alloc (8, ROUNDUP (size
, 8));
72 ASSERT (aligned8_blocks
[i
] != NULL
);
73 ASSERT (((uintptr_t) aligned8_blocks
[i
] % 8) == 0);
74 memset (aligned8_blocks
[i
], 'w', size
);
76 aligned16_blocks
[i
] = aligned_alloc (16, ROUNDUP (size
, 16));
77 ASSERT (aligned16_blocks
[i
] != NULL
);
78 ASSERT (((uintptr_t) aligned16_blocks
[i
] % 16) == 0);
79 memset (aligned16_blocks
[i
], 'x', size
);
81 aligned32_blocks
[i
] = aligned_alloc (32, ROUNDUP (size
, 32));
82 ASSERT (aligned32_blocks
[i
] != NULL
);
83 ASSERT (((uintptr_t) aligned32_blocks
[i
] % 32) == 0);
84 memset (aligned32_blocks
[i
], 'y', size
);
86 aligned64_blocks
[i
] = aligned_alloc (64, ROUNDUP (size
, 64));
87 ASSERT (aligned64_blocks
[i
] != NULL
);
88 ASSERT (((uintptr_t) aligned64_blocks
[i
] % 64) == 0);
89 memset (aligned64_blocks
[i
], 'z', size
);
92 for (i
= 0; i
< SIZEOF (sizes
); i
++)
94 free (aligned2_blocks
[i
]);
95 free (aligned4_blocks
[i
]);
96 free (aligned8_blocks
[i
]);
97 free (aligned16_blocks
[i
]);
98 free (aligned32_blocks
[i
]);
99 free (aligned64_blocks
[i
]);
102 return test_exit_status
;
104 fputs ("Skipping test: function 'aligned_alloc' does not exist\n", stderr
);