flexmember.m4 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # serial 5
  2. # Check for flexible array member support.
  3. # Copyright (C) 2006, 2009-2023 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 m { struct m *next, **list; char name[]; };
  18. struct s { struct s *p; struct m *m; int n; double d[]; };]],
  19. [[int m = getchar ();
  20. size_t nbytes = offsetof (struct s, d) + m * sizeof (double);
  21. nbytes += sizeof (struct s) - 1;
  22. nbytes -= nbytes % sizeof (struct s);
  23. struct s *p = malloc (nbytes);
  24. p->p = p;
  25. p->m = NULL;
  26. p->d[0] = 0.0;
  27. return p->d != (double *) NULL;]])],
  28. [ac_cv_c_flexmember=yes],
  29. [ac_cv_c_flexmember=no])])
  30. if test $ac_cv_c_flexmember = yes; then
  31. AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [],
  32. [Define to nothing if C supports flexible array members, and to
  33. 1 if it does not. That way, with a declaration like 'struct s
  34. { int n; short d@<:@FLEXIBLE_ARRAY_MEMBER@:>@; };', the struct hack
  35. can be used with pre-C99 compilers.
  36. Use 'FLEXSIZEOF (struct s, d, N * sizeof (short))' to calculate
  37. the size in bytes of such a struct containing an N-element array.])
  38. else
  39. AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [1])
  40. fi
  41. ])