changeset 17418:b876bfd31d0f

malloca: port to compilers that reject size-zero arrays This fixes a bug introduced in my previous patch. * lib/malloca.c (struct preliminary_header): Use an int rather than a character array of size int; that's simpler. (struct header): Remove, replacing with ... (union header): New type. This avoids the need for declaring a character array of size zero, which is not allowed on some platforms. All uses changed.
author Paul Eggert <eggert@cs.ucla.edu>
date Sun, 19 May 2013 12:58:53 -0700
parents 3e765e2f4c46
children 020c917cba9d
files ChangeLog lib/malloca.c
diffstat 2 files changed, 24 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2013-05-19  Paul Eggert  <eggert@cs.ucla.edu>
+
+	malloca: port to compilers that reject size-zero arrays
+	This fixes a bug introduced in my previous patch.
+	* lib/malloca.c (struct preliminary_header): Use an int
+	rather than a character array of size int; that's simpler.
+	(struct header): Remove, replacing with ...
+	(union header): New type.  This avoids the need for declaring a
+	character array of size zero, which is not allowed on some platforms.
+	All uses changed.
+
 2013-05-18  Paul Eggert  <eggert@cs.ucla.edu>
 
 	parse-datetime, tests: don't use "string" + int
--- a/lib/malloca.c
+++ b/lib/malloca.c
@@ -49,12 +49,18 @@
 #define MAGIC_SIZE sizeof (int)
 /* This is how the header info would look like without any alignment
    considerations.  */
-struct preliminary_header { void *next; char room[MAGIC_SIZE]; };
+struct preliminary_header { void *next; int magic; };
 /* But the header's size must be a multiple of sa_alignment_max.  */
 #define HEADER_SIZE \
   (((sizeof (struct preliminary_header) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max)
-struct header { void *next; char room[HEADER_SIZE - sizeof (struct preliminary_header)]; int magic; };
-verify (HEADER_SIZE == sizeof (struct header));
+union header {
+  void *next;
+  struct {
+    char room[HEADER_SIZE - MAGIC_SIZE];
+    int word;
+  } magic;
+};
+verify (HEADER_SIZE == sizeof (union header));
 /* We make the hash table quite big, so that during lookups the probability
    of empty hash buckets is quite high.  There is no need to make the hash
    table resizable, because when the hash table gets filled so much that the
@@ -79,12 +85,12 @@
       if (p != NULL)
         {
           size_t slot;
-          struct header *h = p;
+          union header *h = p;
 
           p = h + 1;
 
           /* Put a magic number into the indicator word.  */
-          h->magic = MAGIC_NUMBER;
+          h->magic.word = MAGIC_NUMBER;
 
           /* Enter p into the hash table.  */
           slot = (uintptr_t) p % HASH_TABLE_SIZE;
@@ -124,11 +130,11 @@
           void **chain = &mmalloca_results[slot];
           for (; *chain != NULL;)
             {
-              struct header *h = p;
+              union header *h = p;
               if (*chain == p)
                 {
                   /* Found it.  Remove it from the hash table and free it.  */
-                  struct header *p_begin = h - 1;
+                  union header *p_begin = h - 1;
                   *chain = p_begin->next;
                   free (p_begin);
                   return;