flexmember.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* Sizes of structs with flexible array members.
  2. Copyright 2016-2017 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. Written by Paul Eggert. */
  14. #include <stddef.h>
  15. /* Nonzero multiple of alignment of TYPE, suitable for FLEXSIZEOF below.
  16. On older platforms without _Alignof, use a pessimistic bound that is
  17. safe in practice even if FLEXIBLE_ARRAY_MEMBER is 1.
  18. On newer platforms, use _Alignof to get a tighter bound. */
  19. #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
  20. # define FLEXALIGNOF(type) (sizeof (type) & ~ (sizeof (type) - 1))
  21. #else
  22. # define FLEXALIGNOF(type) _Alignof (type)
  23. #endif
  24. /* Upper bound on the size of a struct of type TYPE with a flexible
  25. array member named MEMBER that is followed by N bytes of other data.
  26. This is not simply sizeof (TYPE) + N, since it may require
  27. alignment on unusually picky C11 platforms, and
  28. FLEXIBLE_ARRAY_MEMBER may be 1 on pre-C11 platforms.
  29. Yield a value less than N if and only if arithmetic overflow occurs. */
  30. #define FLEXSIZEOF(type, member, n) \
  31. ((offsetof (type, member) + FLEXALIGNOF (type) - 1 + (n)) \
  32. & ~ (FLEXALIGNOF (type) - 1))