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. */
32 main (int argc
, char *argv
[])
34 #if HAVE_POSIX_MEMALIGN
35 static size_t sizes
[] =
37 13, 8, 17, 450, 320, 1, 99, 4, 15, 16, 2, 76, 37, 127, 2406, 641, 5781,
38 /* Test also a zero size. */
41 void *aligned2_blocks
[SIZEOF (sizes
)];
42 void *aligned4_blocks
[SIZEOF (sizes
)];
43 void *aligned8_blocks
[SIZEOF (sizes
)];
44 void *aligned16_blocks
[SIZEOF (sizes
)];
45 void *aligned32_blocks
[SIZEOF (sizes
)];
46 void *aligned64_blocks
[SIZEOF (sizes
)];
49 for (i
= 0; i
< SIZEOF (sizes
); i
++)
51 size_t size
= sizes
[i
];
53 if (sizeof (void *) <= 2)
55 ASSERT (posix_memalign (&aligned2_blocks
[i
], 2, size
) == 0);
56 ASSERT (aligned2_blocks
[i
] != NULL
);
57 ASSERT (((uintptr_t) aligned2_blocks
[i
] % 2) == 0);
58 memset (aligned2_blocks
[i
], 'u', size
);
61 if (sizeof (void *) <= 4)
63 ASSERT (posix_memalign (&aligned4_blocks
[i
], 4, size
) == 0);
64 ASSERT (aligned4_blocks
[i
] != NULL
);
65 ASSERT (((uintptr_t) aligned4_blocks
[i
] % 4) == 0);
66 memset (aligned4_blocks
[i
], 'v', size
);
69 if (sizeof (void *) <= 8)
71 ASSERT (posix_memalign (&aligned8_blocks
[i
], 8, size
) == 0);
72 ASSERT (aligned8_blocks
[i
] != NULL
);
73 ASSERT (((uintptr_t) aligned8_blocks
[i
] % 8) == 0);
74 memset (aligned8_blocks
[i
], 'w', size
);
77 ASSERT (posix_memalign (&aligned16_blocks
[i
], 16, size
) == 0);
78 ASSERT (aligned16_blocks
[i
] != NULL
);
79 ASSERT (((uintptr_t) aligned16_blocks
[i
] % 16) == 0);
80 memset (aligned16_blocks
[i
], 'x', size
);
82 ASSERT (posix_memalign (&aligned32_blocks
[i
], 32, size
) == 0);
83 ASSERT (aligned32_blocks
[i
] != NULL
);
84 ASSERT (((uintptr_t) aligned32_blocks
[i
] % 32) == 0);
85 memset (aligned32_blocks
[i
], 'y', size
);
87 ASSERT (posix_memalign (&aligned64_blocks
[i
], 64, size
) == 0);
88 ASSERT (aligned64_blocks
[i
] != NULL
);
89 ASSERT (((uintptr_t) aligned64_blocks
[i
] % 64) == 0);
90 memset (aligned64_blocks
[i
], 'z', size
);
93 for (i
= 0; i
< SIZEOF (sizes
); i
++)
95 if (sizeof (void *) <= 2)
96 free (aligned2_blocks
[i
]);
97 if (sizeof (void *) <= 4)
98 free (aligned4_blocks
[i
]);
99 if (sizeof (void *) <= 8)
100 free (aligned8_blocks
[i
]);
101 free (aligned16_blocks
[i
]);
102 free (aligned32_blocks
[i
]);
103 free (aligned64_blocks
[i
]);
106 return test_exit_status
;
108 fputs ("Skipping test: function 'posix_memalign' does not exist\n", stderr
);