tst-aligned-alloc.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Test for C17 alignment requirements.
  2. Copyright (C) 2023-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <malloc.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <libc-diag.h>
  22. #include <support/check.h>
  23. #include "tst-malloc-aux.h"
  24. static int
  25. do_test (void)
  26. {
  27. void *p1;
  28. void *p2;
  29. void *p3;
  30. void *p4;
  31. void *p5;
  32. errno = 0;
  33. /* The implementation supports alignments that are non-negative powers of 2.
  34. We test 5 distinct conditions here:
  35. - A non-negative power of 2 alignment e.g. 64.
  36. - A degenerate zero power of 2 alignment e.g. 1.
  37. - A non-power-of-2 alignment e.g. 65.
  38. - A zero alignment.
  39. - A corner case SIZE_MAX / 2 + 1 alignment.
  40. */
  41. p1 = aligned_alloc (64, 64);
  42. if (p1 == NULL)
  43. FAIL_EXIT1 ("aligned_alloc(64, 64) failed");
  44. p2 = aligned_alloc (1, 64);
  45. if (p2 == NULL)
  46. FAIL_EXIT1 ("aligned_alloc(1, 64) failed");
  47. p3 = aligned_alloc (65, 64);
  48. if (p3 != NULL)
  49. FAIL_EXIT1 ("aligned_alloc(65, 64) did not fail");
  50. p4 = aligned_alloc (0, 64);
  51. if (p4 != NULL)
  52. FAIL_EXIT1 ("aligned_alloc(0, 64) did not fail");
  53. /* This is an alignment like 0x80000000...UL */
  54. p5 = aligned_alloc (SIZE_MAX / 2 + 1, 64);
  55. if (p5 != NULL)
  56. FAIL_EXIT1 ("aligned_alloc(SIZE_MAX/2+1, 64) did not fail");
  57. free (p1);
  58. free (p2);
  59. return 0;
  60. }
  61. #define TEST_FUNCTION do_test ()
  62. #include "../test-skeleton.c"