bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new Autoconf macros AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS


From: Paul Eggert
Subject: new Autoconf macros AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS
Date: Wed, 08 Nov 2006 00:29:07 -0800
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

Ralf Wildenhues <address@hidden> writes:

> Why not go all the way and put it in Autoconf proper?

OK, I did so, by installing the following patch into Autoconf.
I figured flexible array members are in the same category, so
this patch adds them too.

2006-11-08  Paul Eggert  <address@hidden>

        * NEWS: New macros AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS.
        * doc/autoconf.texi (C Compiler): Document them.
        * lib/autoconf/c.m4 (AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS):
        New macros, taken from gnulib.

--- NEWS        23 Oct 2006 07:18:28 -0000      1.408
+++ NEWS        8 Nov 2006 08:26:25 -0000
@@ -19,6 +19,8 @@

 ** AC_COMPUTE_INT no longer caches or reports results.

+** New macros AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS.
+
 ** AC_CHECK_DECL now also works with aggregate objects.

 ** AC_USE_SYSTEM_EXTENSIONS now defines _TANDEM_SOURCE for NonStop platforms.
--- doc/autoconf.texi   27 Oct 2006 23:01:06 -0000      1.1104
+++ doc/autoconf.texi   8 Nov 2006 08:26:26 -0000
@@ -6588,6 +6588,47 @@
 stringizing operator.  New programs need not use this macro.
 @end defmac

address@hidden AC_C_FLEXIBLE_ARRAY_MEMBER
address@hidden
address@hidden FLEXIBLE_ARRAY_MEMBER
+If the C compiler supports flexible array members, define
address@hidden to nothing; otherwise define it to 1.
+That way, a declaration like this:
+
address@hidden
+struct s
+  @{
+    size_t n_vals;
+    double val[FLEXIBLE_ARRAY_MEMBER];
+  @};
address@hidden example
+
address@hidden
+will let applications use the ``struct hack'' even with compilers that
+do not support flexible array members.  To allocate and use such an
+object, you can use code like this:
+
address@hidden
+size_t i;
+size_t n = compute_value_count ();
+struct s *p =
+   malloc (offsetof (struct s, val)
+           + n * sizeof (double));
+p->n_vals = n;
+for (i = 0; i < n; i++)
+  p->val[i] = compute_value (i);
address@hidden example
address@hidden defmac
+
address@hidden AC_C_VARARRAYS
address@hidden
address@hidden HAVE_C_VARARRAYS
+If the C compiler supports variable-length arrays, define
address@hidden  A variable-length array is an array of automatic
+storage duration whose length is determined at run time, when the array
+is declared.
address@hidden defmac
+
 @defmac AC_C_TYPEOF
 @acindex{C_TYPEOF}
 @cvindex HAVE_TYPEOF
--- lib/autoconf/c.m4   12 Oct 2006 17:01:45 -0000      1.237
+++ lib/autoconf/c.m4   8 Nov 2006 08:26:27 -0000
@@ -1643,6 +1643,61 @@
 ])# AC_C_PROTOTYPES


+# AC_C_FLEXIBLE_ARRAY_MEMBER
+# --------------------------
+# Check whether the C compiler supports flexible array members.
+AC_DEFUN([AC_C_FLEXIBLE_ARRAY_MEMBER],
+[
+  AC_CACHE_CHECK([for flexible array members],
+    ac_cv_c_flexmember,
+    [AC_COMPILE_IFELSE(
+       [AC_LANG_PROGRAM(
+         [[#include <stdlib.h>
+           #include <stdio.h>
+           #include <stddef.h>
+           struct s { int n; double d[]; };]],
+         [[int m = getchar ();
+           struct s *p = malloc (offsetof (struct s, d)
+                                 + m * sizeof (double));
+           p->d[0] = 0.0;
+           return p->d != (double *) NULL;]])],
+       [ac_cv_c_flexmember=yes],
+       [ac_cv_c_flexmember=no])])
+  if test $ac_cv_c_flexmember = yes; then
+    AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [],
+      [Define to nothing if C supports flexible array members, and to
+       1 if it does not.  That way, with a declaration like `struct s
+       { int n; double d@<:@FLEXIBLE_ARRAY_MEMBER@:>@; };', the struct hack
+       can be used with pre-C99 compilers.
+       When computing the size of such an object, don't use 'sizeof (struct s)'
+       as it overestimates the size.  Use 'offsetof (struct s, d)' instead.
+       Don't use 'offsetof (struct s, d@<:@0@:>@)', as this doesn't work with
+       MSVC and with C++ compilers.])
+  else
+    AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], 1)
+  fi
+])
+
+
+# AC_C_VARARRAYS
+# --------------
+# Check whether the C compiler supports variable-length arrays.
+AC_DEFUN([AC_C_VARARRAYS],
+[
+  AC_CACHE_CHECK([for variable-length arrays],
+    ac_cv_c_vararrays,
+    [AC_COMPILE_IFELSE(
+       [AC_LANG_PROGRAM([],
+         [[static int x; char a[++x]; a[sizeof a - 1] = 0; return a[0];]])],
+       [ac_cv_c_vararrays=yes],
+       [ac_cv_c_vararrays=no])])
+  if test $ac_cv_c_vararrays = yes; then
+    AC_DEFINE([HAVE_C_VARARRAYS], 1,
+      [Define to 1 if C supports variable-length arrays.])
+  fi
+])
+
+
 # AC_C_TYPEOF
 # -----------
 # Check if the C compiler supports GCC's typeof syntax.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]