annotate lib/allocsa.c @ 6075:ea0e673b670d

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