From: Paul Eggert Date: Sun, 19 May 2013 19:58:53 +0000 (-0700) Subject: malloca: port to compilers that reject size-zero arrays X-Git-Tag: v0.1~122 X-Git-Url: http://erislabs.net/gitweb/?p=gnulib.git;a=commitdiff_plain;h=571f20db52d2165d6cce4b2aacdedb123330d90f 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. --- diff --git a/ChangeLog b/ChangeLog index 69639940e..08042c5fa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2013-05-19 Paul Eggert + + 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 parse-datetime, tests: don't use "string" + int diff --git a/lib/malloca.c b/lib/malloca.c index 53ecb8122..7a4b0cd5a 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;