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. */
26 #define aligned_malloc aligned4_malloc
27 #define aligned_free aligned4_free
28 #include "aligned-malloc.h"
32 void *(*volatile my_aligned4_malloc
) (size_t) = aligned4_malloc
;
33 #define aligned4_malloc my_aligned4_malloc
34 void (*volatile my_aligned4_free
) (void *) = aligned4_free
;
35 #define aligned4_free my_aligned4_free
38 #define aligned_malloc aligned8_malloc
39 #define aligned_free aligned8_free
40 #include "aligned-malloc.h"
44 void *(*volatile my_aligned8_malloc
) (size_t) = aligned8_malloc
;
45 #define aligned8_malloc my_aligned8_malloc
46 void (*volatile my_aligned8_free
) (void *) = aligned8_free
;
47 #define aligned8_free my_aligned8_free
50 #define aligned_malloc aligned16_malloc
51 #define aligned_free aligned16_free
52 #include "aligned-malloc.h"
56 void *(*volatile my_aligned16_malloc
) (size_t) = aligned16_malloc
;
57 #define aligned16_malloc my_aligned16_malloc
58 void (*volatile my_aligned16_free
) (void *) = aligned16_free
;
59 #define aligned16_free my_aligned16_free
62 #define aligned_malloc aligned32_malloc
63 #define aligned_free aligned32_free
64 #include "aligned-malloc.h"
68 void *(*volatile my_aligned32_malloc
) (size_t) = aligned32_malloc
;
69 #define aligned32_malloc my_aligned32_malloc
70 void (*volatile my_aligned32_free
) (void *) = aligned32_free
;
71 #define aligned32_free my_aligned32_free
78 main (int argc
, char *argv
[])
80 static size_t sizes
[] =
81 { 13, 8, 17, 450, 320, 1, 99, 4, 15, 16, 2, 76, 37, 127, 2406, 641 };
82 void *aligned4_blocks
[SIZEOF (sizes
)];
83 void *aligned8_blocks
[SIZEOF (sizes
)];
84 void *aligned16_blocks
[SIZEOF (sizes
)];
85 void *aligned32_blocks
[SIZEOF (sizes
)];
88 for (i
= 0; i
< SIZEOF (sizes
); i
++)
90 size_t size
= sizes
[i
];
92 aligned4_blocks
[i
] = aligned4_malloc (size
);
93 ASSERT (((uintptr_t) aligned4_blocks
[i
] % 4) == 0);
94 memset (aligned4_blocks
[i
], 'w', size
);
96 aligned8_blocks
[i
] = aligned8_malloc (size
);
97 ASSERT (((uintptr_t) aligned8_blocks
[i
] % 8) == 0);
98 memset (aligned8_blocks
[i
], 'x', size
);
100 aligned16_blocks
[i
] = aligned16_malloc (size
);
101 ASSERT (((uintptr_t) aligned16_blocks
[i
] % 16) == 0);
102 memset (aligned16_blocks
[i
], 'y', size
);
104 aligned32_blocks
[i
] = aligned32_malloc (size
);
105 ASSERT (((uintptr_t) aligned32_blocks
[i
] % 32) == 0);
106 memset (aligned32_blocks
[i
], 'z', size
);
109 for (i
= 0; i
< SIZEOF (sizes
); i
++)
111 aligned4_free (aligned4_blocks
[i
]);
112 aligned8_free (aligned8_blocks
[i
]);
113 aligned16_free (aligned16_blocks
[i
]);
114 aligned32_free (aligned32_blocks
[i
]);
117 return test_exit_status
;