bug-gnulib
[Top][All Lists]
Advanced

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

[PATCH] malloca: port to compilers that reject size-zero arrays


From: Paul Eggert
Subject: [PATCH] malloca: port to compilers that reject size-zero arrays
Date: Sun, 19 May 2013 13:06:39 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6

---
 ChangeLog     | 11 +++++++++++
 lib/malloca.c | 20 +++++++++++++-------
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 6963994..08042c5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2013-05-19  Paul Eggert  <address@hidden>
+
+       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  <address@hidden>
 
        parse-datetime, tests: don't use "string" + int
diff --git a/lib/malloca.c b/lib/malloca.c
index 53ecb81..7a4b0cd 100644
--- 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 @@ 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;
-- 
1.7.11.7




reply via email to

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