diff options
Diffstat (limited to 'gl/malloca.c')
-rw-r--r-- | gl/malloca.c | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/gl/malloca.c b/gl/malloca.c index 21bbc1f..311be56 100644 --- a/gl/malloca.c +++ b/gl/malloca.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* Safe automatic memory allocation. | 1 | /* Safe automatic memory allocation. |
2 | Copyright (C) 2003, 2006-2007, 2009-2010 Free Software Foundation, Inc. | 2 | Copyright (C) 2003, 2006-2007, 2009-2013 Free Software Foundation, Inc. |
3 | Written by Bruno Haible <bruno@clisp.org>, 2003. | 3 | Written by Bruno Haible <bruno@clisp.org>, 2003. |
4 | 4 | ||
5 | This program is free software; you can redistribute it and/or modify | 5 | This program is free software; you can redistribute it and/or modify |
@@ -13,16 +13,17 @@ | |||
13 | GNU General Public License for more details. | 13 | GNU General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU General Public License | 15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software Foundation, | 16 | along with this program; if not, see <http://www.gnu.org/licenses/>. */ |
17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
18 | 17 | ||
18 | #define _GL_USE_STDLIB_ALLOC 1 | ||
19 | #include <config.h> | 19 | #include <config.h> |
20 | 20 | ||
21 | /* Specification. */ | 21 | /* Specification. */ |
22 | #include "malloca.h" | 22 | #include "malloca.h" |
23 | 23 | ||
24 | /* Use the system functions, not the gnulib overrides in this file. */ | 24 | #include <stdint.h> |
25 | #undef malloc | 25 | |
26 | #include "verify.h" | ||
26 | 27 | ||
27 | /* The speed critical point in this file is freea() applied to an alloca() | 28 | /* The speed critical point in this file is freea() applied to an alloca() |
28 | result: it must be fast, to match the speed of alloca(). The speed of | 29 | result: it must be fast, to match the speed of alloca(). The speed of |
@@ -48,13 +49,18 @@ | |||
48 | #define MAGIC_SIZE sizeof (int) | 49 | #define MAGIC_SIZE sizeof (int) |
49 | /* This is how the header info would look like without any alignment | 50 | /* This is how the header info would look like without any alignment |
50 | considerations. */ | 51 | considerations. */ |
51 | struct preliminary_header { void *next; char room[MAGIC_SIZE]; }; | 52 | struct preliminary_header { void *next; int magic; }; |
52 | /* But the header's size must be a multiple of sa_alignment_max. */ | 53 | /* But the header's size must be a multiple of sa_alignment_max. */ |
53 | #define HEADER_SIZE \ | 54 | #define HEADER_SIZE \ |
54 | (((sizeof (struct preliminary_header) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max) | 55 | (((sizeof (struct preliminary_header) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max) |
55 | struct header { void *next; char room[HEADER_SIZE - sizeof (struct preliminary_header) + MAGIC_SIZE]; }; | 56 | union header { |
56 | /* Verify that HEADER_SIZE == sizeof (struct header). */ | 57 | void *next; |
57 | typedef int verify1[2 * (HEADER_SIZE == sizeof (struct header)) - 1]; | 58 | struct { |
59 | char room[HEADER_SIZE - MAGIC_SIZE]; | ||
60 | int word; | ||
61 | } magic; | ||
62 | }; | ||
63 | verify (HEADER_SIZE == sizeof (union header)); | ||
58 | /* We make the hash table quite big, so that during lookups the probability | 64 | /* We make the hash table quite big, so that during lookups the probability |
59 | of empty hash buckets is quite high. There is no need to make the hash | 65 | of empty hash buckets is quite high. There is no need to make the hash |
60 | table resizable, because when the hash table gets filled so much that the | 66 | table resizable, because when the hash table gets filled so much that the |
@@ -74,20 +80,21 @@ mmalloca (size_t n) | |||
74 | 80 | ||
75 | if (nplus >= n) | 81 | if (nplus >= n) |
76 | { | 82 | { |
77 | char *p = (char *) malloc (nplus); | 83 | void *p = malloc (nplus); |
78 | 84 | ||
79 | if (p != NULL) | 85 | if (p != NULL) |
80 | { | 86 | { |
81 | size_t slot; | 87 | size_t slot; |
88 | union header *h = p; | ||
82 | 89 | ||
83 | p += HEADER_SIZE; | 90 | p = h + 1; |
84 | 91 | ||
85 | /* Put a magic number into the indicator word. */ | 92 | /* Put a magic number into the indicator word. */ |
86 | ((int *) p)[-1] = MAGIC_NUMBER; | 93 | h->magic.word = MAGIC_NUMBER; |
87 | 94 | ||
88 | /* Enter p into the hash table. */ | 95 | /* Enter p into the hash table. */ |
89 | slot = (unsigned long) p % HASH_TABLE_SIZE; | 96 | slot = (uintptr_t) p % HASH_TABLE_SIZE; |
90 | ((struct header *) (p - HEADER_SIZE))->next = mmalloca_results[slot]; | 97 | h->next = mmalloca_results[slot]; |
91 | mmalloca_results[slot] = p; | 98 | mmalloca_results[slot] = p; |
92 | 99 | ||
93 | return p; | 100 | return p; |
@@ -119,19 +126,21 @@ freea (void *p) | |||
119 | { | 126 | { |
120 | /* Looks like a mmalloca() result. To see whether it really is one, | 127 | /* Looks like a mmalloca() result. To see whether it really is one, |
121 | perform a lookup in the hash table. */ | 128 | perform a lookup in the hash table. */ |
122 | size_t slot = (unsigned long) p % HASH_TABLE_SIZE; | 129 | size_t slot = (uintptr_t) p % HASH_TABLE_SIZE; |
123 | void **chain = &mmalloca_results[slot]; | 130 | void **chain = &mmalloca_results[slot]; |
124 | for (; *chain != NULL;) | 131 | for (; *chain != NULL;) |
125 | { | 132 | { |
133 | union header *h = p; | ||
126 | if (*chain == p) | 134 | if (*chain == p) |
127 | { | 135 | { |
128 | /* Found it. Remove it from the hash table and free it. */ | 136 | /* Found it. Remove it from the hash table and free it. */ |
129 | char *p_begin = (char *) p - HEADER_SIZE; | 137 | union header *p_begin = h - 1; |
130 | *chain = ((struct header *) p_begin)->next; | 138 | *chain = p_begin->next; |
131 | free (p_begin); | 139 | free (p_begin); |
132 | return; | 140 | return; |
133 | } | 141 | } |
134 | chain = &((struct header *) ((char *) *chain - HEADER_SIZE))->next; | 142 | h = *chain; |
143 | chain = &h[-1].next; | ||
135 | } | 144 | } |
136 | } | 145 | } |
137 | /* At this point, we know it was not a mmalloca() result. */ | 146 | /* At this point, we know it was not a mmalloca() result. */ |