tst-mallocalign1.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Verify that MALLOC_ALIGNMENT is honored by malloc.
  2. Copyright (C) 2012-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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <inttypes.h>
  18. #include <malloc-size.h>
  19. #include <support/check.h>
  20. static void *
  21. test (size_t s)
  22. {
  23. void *p = malloc (s);
  24. printf ("malloc: %zu, %p: %zu\n", s, p,
  25. ((uintptr_t) p) & MALLOC_ALIGN_MASK);
  26. return p;
  27. }
  28. #define ALIGNED(p) (((uintptr_t) p & MALLOC_ALIGN_MASK) == 0)
  29. static int
  30. do_test (void)
  31. {
  32. void *p;
  33. p = test (2);
  34. TEST_VERIFY (ALIGNED (p));
  35. free (p);
  36. p = test (8);
  37. TEST_VERIFY (ALIGNED (p));
  38. free (p);
  39. p = test (13);
  40. TEST_VERIFY (ALIGNED (p));
  41. free (p);
  42. p = test (16);
  43. TEST_VERIFY (ALIGNED (p));
  44. free (p);
  45. p = test (23);
  46. TEST_VERIFY (ALIGNED (p));
  47. free (p);
  48. p = test (43);
  49. TEST_VERIFY (ALIGNED (p));
  50. free (p);
  51. p = test (123);
  52. TEST_VERIFY (ALIGNED (p));
  53. free (p);
  54. return 0;
  55. }
  56. #include <support/test-driver.c>