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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 #include <sys/cdefs.h>
19 __RCSID("$NetBSD: allocsa.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
20
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 /* Specification. */
27 #include "allocsa.h"
28
29 /* The speed critical point in this file is freesa() applied to an alloca()
30 result: it must be fast, to match the speed of alloca(). The speed of
31 mallocsa() and freesa() in the other case are not critical, because they
32 are only invoked for big memory sizes. */
33
34 #if HAVE_ALLOCA
35
36 /* Store the mallocsa() results in a hash table. This is needed to reliably
37 distinguish a mallocsa() result and an alloca() result.
38
39 Although it is possible that the same pointer is returned by alloca() and
40 by mallocsa() at different times in the same application, it does not lead
41 to a bug in freesa(), because:
42 - Before a pointer returned by alloca() can point into malloc()ed memory,
43 the function must return, and once this has happened the programmer must
44 not call freesa() on it anyway.
45 - Before a pointer returned by mallocsa() can point into the stack, it
46 must be freed. The only function that can free it is freesa(), and
47 when freesa() frees it, it also removes it from the hash table. */
48
49 #define MAGIC_NUMBER 0x1415fb4a
50 #define MAGIC_SIZE sizeof (int)
51 /* This is how the header info would look like without any alignment
52 considerations. */
53 struct preliminary_header { void *next; char room[MAGIC_SIZE]; };
54 /* But the header's size must be a multiple of sa_alignment_max. */
55 #define HEADER_SIZE \
56 (((sizeof (struct preliminary_header) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max)
57 struct header { void *next; char room[HEADER_SIZE - sizeof (struct preliminary_header) + MAGIC_SIZE]; };
58 /* Verify that HEADER_SIZE == sizeof (struct header). */
59 typedef int verify1[2 * (HEADER_SIZE == sizeof (struct header)) - 1];
60 /* We make the hash table quite big, so that during lookups the probability
61 of empty hash buckets is quite high. There is no need to make the hash
62 table resizable, because when the hash table gets filled so much that the
63 lookup becomes slow, it means that the application has memory leaks. */
64 #define HASH_TABLE_SIZE 257
65 static void * mallocsa_results[HASH_TABLE_SIZE];
66
67 #endif
68
69 void *
mallocsa(size_t n)70 mallocsa (size_t n)
71 {
72 #if HAVE_ALLOCA
73 /* Allocate one more word, that serves as an indicator for malloc()ed
74 memory, so that freesa() of an alloca() result is fast. */
75 size_t nplus = n + HEADER_SIZE;
76
77 if (nplus >= n)
78 {
79 char *p = (char *) malloc (nplus);
80
81 if (p != NULL)
82 {
83 size_t slot;
84
85 p += HEADER_SIZE;
86
87 /* Put a magic number into the indicator word. */
88 ((int *) p)[-1] = MAGIC_NUMBER;
89
90 /* Enter p into the hash table. */
91 slot = (unsigned long) p % HASH_TABLE_SIZE;
92 ((struct header *) (p - HEADER_SIZE))->next = mallocsa_results[slot];
93 mallocsa_results[slot] = p;
94
95 return p;
96 }
97 }
98 /* Out of memory. */
99 return NULL;
100 #else
101 # if !MALLOC_0_IS_NONNULL
102 if (n == 0)
103 n = 1;
104 # endif
105 return malloc (n);
106 #endif
107 }
108
109 #if HAVE_ALLOCA
110 void
freesa(void * p)111 freesa (void *p)
112 {
113 /* mallocsa() may have returned NULL. */
114 if (p != NULL)
115 {
116 /* Attempt to quickly distinguish the mallocsa() result - which has
117 a magic indicator word - and the alloca() result - which has an
118 uninitialized indicator word. It is for this test that sa_increment
119 additional bytes are allocated in the alloca() case. */
120 if (((int *) p)[-1] == MAGIC_NUMBER)
121 {
122 /* Looks like a mallocsa() result. To see whether it really is one,
123 perform a lookup in the hash table. */
124 size_t slot = (unsigned long) p % HASH_TABLE_SIZE;
125 void **chain = &mallocsa_results[slot];
126 for (; *chain != NULL;)
127 {
128 if (*chain == p)
129 {
130 /* Found it. Remove it from the hash table and free it. */
131 char *p_begin = (char *) p - HEADER_SIZE;
132 *chain = ((struct header *) p_begin)->next;
133 free (p_begin);
134 return;
135 }
136 chain = &((struct header *) ((char *) *chain - HEADER_SIZE))->next;
137 }
138 }
139 /* At this point, we know it was not a mallocsa() result. */
140 }
141 }
142 #endif
143