malloca: port to compilers that reject size-zero arrays
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 19 May 2013 19:58:53 +0000 (12:58 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 19 May 2013 19:59:23 +0000 (12:59 -0700)
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.

ChangeLog
lib/malloca.c

index 6963994..08042c5 100644 (file)
--- 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
index 53ecb81..7a4b0cd 100644 (file)
 #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 @@ mmalloca (size_t n)
       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 @@ freea (void *p)
           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;