4924
|
1 /* Safe automatic memory allocation. |
|
2 Copyright (C) 2003 Free Software Foundation, Inc. |
|
3 Written by Bruno Haible <bruno@clisp.org>, 2003. |
|
4 |
|
5 This program is free software; you can redistribute it and/or modify |
|
6 it under the terms of the GNU General Public License as published by |
|
7 the Free Software Foundation; either version 2, or (at your option) |
|
8 any later version. |
|
9 |
|
10 This program is distributed in the hope that it will be useful, |
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 GNU General Public License for more details. |
|
14 |
|
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, |
|
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
|
18 |
|
19 #ifdef HAVE_CONFIG_H |
|
20 # include <config.h> |
|
21 #endif |
|
22 |
|
23 /* Specification. */ |
|
24 #include "allocsa.h" |
|
25 |
|
26 /* The speed critical point in this file is freesa() applied to an alloca() |
|
27 result: it must be fast, to match the speed of alloca(). The speed of |
|
28 mallocsa() and freesa() in the other case are not critical, because they |
|
29 are only invoked for big memory sizes. */ |
|
30 |
|
31 #if HAVE_ALLOCA |
|
32 |
|
33 /* Store the mallocsa() results in a hash table. This is needed to reliably |
|
34 distinguish a mallocsa() result and an alloca() result. |
|
35 |
|
36 Although it is possible that the same pointer is returned by alloca() and |
|
37 by mallocsa() at different times in the same application, it does not lead |
|
38 to a bug in freesa(), because: |
|
39 - Before a pointer returned by alloca() can point into malloc()ed memory, |
|
40 the function must return, and once this has happened the programmer must |
|
41 not call freesa() on it anyway. |
|
42 - Before a pointer returned by mallocsa() can point into the stack, it |
|
43 must be freed. The only function that can free it is freesa(), and |
|
44 when freesa() frees it, it also removes it from the hash table. */ |
|
45 |
|
46 #define MAGIC_NUMBER 0x1415fb4a |
|
47 #define MAGIC_SIZE sizeof (int) |
|
48 /* This is how the header info would look like without any alignment |
|
49 considerations. */ |
|
50 struct preliminary_header { void *next; char room[MAGIC_SIZE]; }; |
|
51 /* But the header's size must be a multiple of sa_alignment_max. */ |
|
52 #define HEADER_SIZE \ |
|
53 (((sizeof (struct preliminary_header) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max) |
|
54 struct header { void *next; char room[HEADER_SIZE - sizeof (struct preliminary_header) + MAGIC_SIZE]; }; |
|
55 /* Verify that HEADER_SIZE == sizeof (struct header). */ |
|
56 typedef int verify1[2 * (HEADER_SIZE == sizeof (struct header)) - 1]; |
|
57 /* We make the hash table quite big, so that during lookups the probability |
|
58 of empty hash buckets is quite high. There is no need to make the hash |
|
59 table resizable, because when the hash table gets filled so much that the |
|
60 lookup becomes slow, it means that the application has memory leaks. */ |
|
61 #define HASH_TABLE_SIZE 257 |
|
62 static void * mallocsa_results[HASH_TABLE_SIZE]; |
|
63 |
|
64 #endif |
|
65 |
|
66 void * |
|
67 mallocsa (size_t n) |
|
68 { |
|
69 #if HAVE_ALLOCA |
|
70 /* Allocate one more word, that serves as an indicator for malloc()ed |
|
71 memory, so that freesa() of an alloca() result is fast. */ |
|
72 size_t nplus = n + HEADER_SIZE; |
|
73 |
|
74 if (nplus >= n) |
|
75 { |
|
76 char *p = (char *) malloc (nplus); |
|
77 |
|
78 if (p != NULL) |
|
79 { |
|
80 size_t slot; |
|
81 |
|
82 p += HEADER_SIZE; |
|
83 |
|
84 /* Put a magic number into the indicator word. */ |
|
85 ((int *) p)[-1] = MAGIC_NUMBER; |
|
86 |
|
87 /* Enter p into the hash table. */ |
|
88 slot = (unsigned long) p % HASH_TABLE_SIZE; |
|
89 ((struct header *) (p - HEADER_SIZE))->next = mallocsa_results[slot]; |
|
90 mallocsa_results[slot] = p; |
|
91 |
|
92 return p; |
|
93 } |
|
94 } |
|
95 /* Out of memory. */ |
|
96 return NULL; |
|
97 #else |
|
98 # if !MALLOC_0_IS_NONNULL |
|
99 if (n == 0) |
|
100 n = 1; |
|
101 # endif |
|
102 return malloc (n); |
|
103 #endif |
|
104 } |
|
105 |
|
106 #if HAVE_ALLOCA |
|
107 void |
|
108 freesa (void *p) |
|
109 { |
|
110 /* mallocsa() may have returned NULL. */ |
|
111 if (p != NULL) |
|
112 { |
|
113 /* Attempt to quickly distinguish the mallocsa() result - which has |
|
114 a magic indicator word - and the alloca() result - which has an |
|
115 uninitialized indicator word. It is for this test that sa_increment |
|
116 additional bytes are allocated in the alloca() case. */ |
|
117 if (((int *) p)[-1] == MAGIC_NUMBER) |
|
118 { |
|
119 /* Looks like a mallocsa() result. To see whether it really is one, |
|
120 perform a lookup in the hash table. */ |
|
121 size_t slot = (unsigned long) p % HASH_TABLE_SIZE; |
|
122 void **chain = &mallocsa_results[slot]; |
|
123 for (; *chain != NULL;) |
|
124 { |
|
125 if (*chain == p) |
|
126 { |
|
127 /* Found it. Remove it from the hash table and free it. */ |
|
128 char *p_begin = (char *) p - HEADER_SIZE; |
|
129 *chain = ((struct header *) p_begin)->next; |
|
130 free (p_begin); |
|
131 return; |
|
132 } |
|
133 chain = &((struct header *) ((char *) *chain - HEADER_SIZE))->next; |
|
134 } |
|
135 } |
|
136 /* At this point, we know it was not a mallocsa() result. */ |
|
137 } |
|
138 } |
|
139 #endif |