tst-setjmp-check.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Check jmp_buf sizes, alignments and offsets.
  2. Copyright (C) 2021-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 <stddef.h>
  16. #include <setjmp.h>
  17. #include <jmp_buf-macros.h>
  18. #define SJSTR_HELPER(x) #x
  19. #define SJSTR(x) SJSTR_HELPER(x)
  20. #define TEST_SIZE(type, size) \
  21. _Static_assert (sizeof (type) == size, \
  22. "size of " #type " != " \
  23. SJSTR (size))
  24. #define TEST_ALIGN(type, align) \
  25. _Static_assert (__alignof__ (type) == align , \
  26. "align of " #type " != " \
  27. SJSTR (align))
  28. #define TEST_OFFSET(type, member, offset) \
  29. _Static_assert (offsetof (type, member) == offset, \
  30. "offset of " #member " field of " #type " != " \
  31. SJSTR (offset))
  32. /* Check if jmp_buf have the expected sizes. */
  33. TEST_SIZE (jmp_buf, JMP_BUF_SIZE);
  34. TEST_SIZE (sigjmp_buf, SIGJMP_BUF_SIZE);
  35. /* Check if jmp_buf have the expected alignments. */
  36. TEST_ALIGN (jmp_buf, JMP_BUF_ALIGN);
  37. TEST_ALIGN (sigjmp_buf, SIGJMP_BUF_ALIGN);
  38. /* Check if internal fields in jmp_buf have the expected offsets. */
  39. TEST_OFFSET (struct __jmp_buf_tag, __mask_was_saved,
  40. MASK_WAS_SAVED_OFFSET);
  41. TEST_OFFSET (struct __jmp_buf_tag, __saved_mask,
  42. SAVED_MASK_OFFSET);
  43. int
  44. main (int argc, char *argv[])
  45. {
  46. return 0;
  47. }