summaryrefslogtreecommitdiff
path: root/lib/malloca.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/malloca.c')
-rw-r--r--lib/malloca.c44
1 files changed, 26 insertions, 18 deletions
diff --git a/lib/malloca.c b/lib/malloca.c
index 47a8d7a..311be56 100644
--- a/lib/malloca.c
+++ b/lib/malloca.c
@@ -1,5 +1,5 @@
/* Safe automatic memory allocation.
- Copyright (C) 2003, 2006-2007, 2009-2011 Free Software Foundation, Inc.
+ Copyright (C) 2003, 2006-2007, 2009-2013 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>, 2003.
This program is free software; you can redistribute it and/or modify
@@ -13,18 +13,17 @@
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software Foundation,
- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+ along with this program; if not, see <http://www.gnu.org/licenses/>. */
+#define _GL_USE_STDLIB_ALLOC 1
#include <config.h>
/* Specification. */
#include "malloca.h"
-#include "verify.h"
+#include <stdint.h>
-/* Use the system functions, not the gnulib overrides in this file. */
-#undef malloc
+#include "verify.h"
/* The speed critical point in this file is freea() applied to an alloca()
result: it must be fast, to match the speed of alloca(). The speed of
@@ -50,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) + MAGIC_SIZE]; };
-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
@@ -75,20 +80,21 @@ mmalloca (size_t n)
if (nplus >= n)
{
- char *p = (char *) malloc (nplus);
+ void *p = malloc (nplus);
if (p != NULL)
{
size_t slot;
+ union header *h = p;
- p += HEADER_SIZE;
+ p = h + 1;
/* Put a magic number into the indicator word. */
- ((int *) p)[-1] = MAGIC_NUMBER;
+ h->magic.word = MAGIC_NUMBER;
/* Enter p into the hash table. */
- slot = (unsigned long) p % HASH_TABLE_SIZE;
- ((struct header *) (p - HEADER_SIZE))->next = mmalloca_results[slot];
+ slot = (uintptr_t) p % HASH_TABLE_SIZE;
+ h->next = mmalloca_results[slot];
mmalloca_results[slot] = p;
return p;
@@ -120,19 +126,21 @@ freea (void *p)
{
/* Looks like a mmalloca() result. To see whether it really is one,
perform a lookup in the hash table. */
- size_t slot = (unsigned long) p % HASH_TABLE_SIZE;
+ size_t slot = (uintptr_t) p % HASH_TABLE_SIZE;
void **chain = &mmalloca_results[slot];
for (; *chain != NULL;)
{
+ union header *h = p;
if (*chain == p)
{
/* Found it. Remove it from the hash table and free it. */
- char *p_begin = (char *) p - HEADER_SIZE;
- *chain = ((struct header *) p_begin)->next;
+ union header *p_begin = h - 1;
+ *chain = p_begin->next;
free (p_begin);
return;
}
- chain = &((struct header *) ((char *) *chain - HEADER_SIZE))->next;
+ h = *chain;
+ chain = &h[-1].next;
}
}
/* At this point, we know it was not a mmalloca() result. */