1 /* Test of allocating memory with given alignment.
3 Copyright (C) 2020-2021 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
[] =
36 { 13, 8, 17, 450, 320, 1, 99, 4, 15, 16, 2, 76, 37, 127, 2406, 641, 5781 };
37 void *aligned2_blocks
[SIZEOF (sizes
)];
38 void *aligned4_blocks
[SIZEOF (sizes
)];
39 void *aligned8_blocks
[SIZEOF (sizes
)];
40 void *aligned16_blocks
[SIZEOF (sizes
)];
41 void *aligned32_blocks
[SIZEOF (sizes
)];
42 void *aligned64_blocks
[SIZEOF (sizes
)];
45 for (i
= 0; i
< SIZEOF (sizes
); i
++)
47 size_t size
= sizes
[i
];
49 if (sizeof (void *) <= 2)
51 ASSERT (posix_memalign (&aligned2_blocks
[i
], 2, size
) == 0);
52 ASSERT (aligned2_blocks
[i
] != NULL
);
53 ASSERT (((uintptr_t) aligned2_blocks
[i
] % 2) == 0);
54 memset (aligned2_blocks
[i
], 'u', size
);
57 if (sizeof (void *) <= 4)
59 ASSERT (posix_memalign (&aligned4_blocks
[i
], 4, size
) == 0);
60 ASSERT (aligned4_blocks
[i
] != NULL
);
61 ASSERT (((uintptr_t) aligned4_blocks
[i
] % 4) == 0);
62 memset (aligned4_blocks
[i
], 'v', size
);
65 ASSERT (posix_memalign (&aligned8_blocks
[i
], 8, size
) == 0);
66 ASSERT (aligned8_blocks
[i
] != NULL
);
67 ASSERT (((uintptr_t) aligned8_blocks
[i
] % 8) == 0);
68 memset (aligned8_blocks
[i
], 'w', size
);
70 ASSERT (posix_memalign (&aligned16_blocks
[i
], 16, size
) == 0);
71 ASSERT (aligned16_blocks
[i
] != NULL
);
72 ASSERT (((uintptr_t) aligned16_blocks
[i
] % 16) == 0);
73 memset (aligned16_blocks
[i
], 'x', size
);
75 ASSERT (posix_memalign (&aligned32_blocks
[i
], 32, size
) == 0);
76 ASSERT (aligned32_blocks
[i
] != NULL
);
77 ASSERT (((uintptr_t) aligned32_blocks
[i
] % 32) == 0);
78 memset (aligned32_blocks
[i
], 'y', size
);
80 ASSERT (posix_memalign (&aligned64_blocks
[i
], 64, size
) == 0);
81 ASSERT (aligned64_blocks
[i
] != NULL
);
82 ASSERT (((uintptr_t) aligned64_blocks
[i
] % 64) == 0);
83 memset (aligned64_blocks
[i
], 'z', size
);
86 for (i
= 0; i
< SIZEOF (sizes
); i
++)
88 if (sizeof (void *) <= 2)
89 free (aligned2_blocks
[i
]);
90 if (sizeof (void *) <= 4)
91 free (aligned4_blocks
[i
]);
92 free (aligned8_blocks
[i
]);
93 free (aligned16_blocks
[i
]);
94 free (aligned32_blocks
[i
]);
95 free (aligned64_blocks
[i
]);
100 fputs ("Skipping test: function 'aligned_alloc' does not exist\n", stderr
);