flexmember.m4 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # serial 4
  2. # Check for flexible array member support.
  3. # Copyright (C) 2006, 2009-2017 Free Software Foundation, Inc.
  4. # This file is free software; the Free Software Foundation
  5. # gives unlimited permission to copy and/or distribute it,
  6. # with or without modifications, as long as this notice is preserved.
  7. # Written by Paul Eggert.
  8. AC_DEFUN([AC_C_FLEXIBLE_ARRAY_MEMBER],
  9. [
  10. AC_CACHE_CHECK([for flexible array member],
  11. ac_cv_c_flexmember,
  12. [AC_COMPILE_IFELSE(
  13. [AC_LANG_PROGRAM(
  14. [[#include <stdlib.h>
  15. #include <stdio.h>
  16. #include <stddef.h>
  17. struct s { int n; double d[]; };]],
  18. [[int m = getchar ();
  19. size_t nbytes = offsetof (struct s, d) + m * sizeof (double);
  20. nbytes += sizeof (struct s) - 1;
  21. nbytes -= nbytes % sizeof (struct s);
  22. struct s *p = malloc (nbytes);
  23. p->d[0] = 0.0;
  24. return p->d != (double *) NULL;]])],
  25. [ac_cv_c_flexmember=yes],
  26. [ac_cv_c_flexmember=no])])
  27. if test $ac_cv_c_flexmember = yes; then
  28. AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [],
  29. [Define to nothing if C supports flexible array members, and to
  30. 1 if it does not. That way, with a declaration like 'struct s
  31. { int n; double d@<:@FLEXIBLE_ARRAY_MEMBER@:>@; };', the struct hack
  32. can be used with pre-C99 compilers.
  33. When computing the size of such an object, don't use 'sizeof (struct s)'
  34. as it overestimates the size. Use 'offsetof (struct s, d)' instead.
  35. Don't use 'offsetof (struct s, d@<:@0@:>@)', as this doesn't work with
  36. MSVC and with C++ compilers.])
  37. else
  38. AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [1])
  39. fi
  40. ])