define-module for elisp special modules
[bpt/guile.git] / lib / malloca.c
CommitLineData
ffca4c22 1/* Safe automatic memory allocation.
5e69ceb7 2 Copyright (C) 2003, 2006-2007, 2009-2014 Free Software Foundation, Inc.
ffca4c22
AW
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 Lesser 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
005de2e8 16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
ffca4c22 17
dd7d0148 18#define _GL_USE_STDLIB_ALLOC 1
ffca4c22
AW
19#include <config.h>
20
21/* Specification. */
22#include "malloca.h"
23
005de2e8
LC
24#include <stdint.h>
25
0f00f2c3
LC
26#include "verify.h"
27
ffca4c22
AW
28/* The speed critical point in this file is freea() applied to an alloca()
29 result: it must be fast, to match the speed of alloca(). The speed of
30 mmalloca() and freea() in the other case are not critical, because they
31 are only invoked for big memory sizes. */
32
33#if HAVE_ALLOCA
34
35/* Store the mmalloca() results in a hash table. This is needed to reliably
36 distinguish a mmalloca() result and an alloca() result.
37
38 Although it is possible that the same pointer is returned by alloca() and
39 by mmalloca() at different times in the same application, it does not lead
40 to a bug in freea(), because:
41 - Before a pointer returned by alloca() can point into malloc()ed memory,
42 the function must return, and once this has happened the programmer must
43 not call freea() on it anyway.
44 - Before a pointer returned by mmalloca() can point into the stack, it
45 must be freed. The only function that can free it is freea(), and
46 when freea() frees it, it also removes it from the hash table. */
47
48#define MAGIC_NUMBER 0x1415fb4a
49#define MAGIC_SIZE sizeof (int)
50/* This is how the header info would look like without any alignment
51 considerations. */
5e69ceb7 52struct preliminary_header { void *next; int magic; };
ffca4c22
AW
53/* But the header's size must be a multiple of sa_alignment_max. */
54#define HEADER_SIZE \
55 (((sizeof (struct preliminary_header) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max)
5e69ceb7
MW
56union header {
57 void *next;
58 struct {
59 char room[HEADER_SIZE - MAGIC_SIZE];
60 int word;
61 } magic;
62};
63verify (HEADER_SIZE == sizeof (union header));
ffca4c22
AW
64/* We make the hash table quite big, so that during lookups the probability
65 of empty hash buckets is quite high. There is no need to make the hash
66 table resizable, because when the hash table gets filled so much that the
67 lookup becomes slow, it means that the application has memory leaks. */
68#define HASH_TABLE_SIZE 257
69static void * mmalloca_results[HASH_TABLE_SIZE];
70
71#endif
72
73void *
74mmalloca (size_t n)
75{
76#if HAVE_ALLOCA
77 /* Allocate one more word, that serves as an indicator for malloc()ed
78 memory, so that freea() of an alloca() result is fast. */
79 size_t nplus = n + HEADER_SIZE;
80
81 if (nplus >= n)
82 {
5e69ceb7 83 void *p = malloc (nplus);
ffca4c22
AW
84
85 if (p != NULL)
1cd4fffc
LC
86 {
87 size_t slot;
5e69ceb7 88 union header *h = p;
ffca4c22 89
5e69ceb7 90 p = h + 1;
ffca4c22 91
1cd4fffc 92 /* Put a magic number into the indicator word. */
5e69ceb7 93 h->magic.word = MAGIC_NUMBER;
ffca4c22 94
1cd4fffc 95 /* Enter p into the hash table. */
005de2e8 96 slot = (uintptr_t) p % HASH_TABLE_SIZE;
5e69ceb7 97 h->next = mmalloca_results[slot];
1cd4fffc 98 mmalloca_results[slot] = p;
ffca4c22 99
1cd4fffc
LC
100 return p;
101 }
ffca4c22
AW
102 }
103 /* Out of memory. */
104 return NULL;
105#else
106# if !MALLOC_0_IS_NONNULL
107 if (n == 0)
108 n = 1;
109# endif
110 return malloc (n);
111#endif
112}
113
114#if HAVE_ALLOCA
115void
116freea (void *p)
117{
118 /* mmalloca() may have returned NULL. */
119 if (p != NULL)
120 {
121 /* Attempt to quickly distinguish the mmalloca() result - which has
1cd4fffc
LC
122 a magic indicator word - and the alloca() result - which has an
123 uninitialized indicator word. It is for this test that sa_increment
124 additional bytes are allocated in the alloca() case. */
ffca4c22 125 if (((int *) p)[-1] == MAGIC_NUMBER)
1cd4fffc
LC
126 {
127 /* Looks like a mmalloca() result. To see whether it really is one,
128 perform a lookup in the hash table. */
005de2e8 129 size_t slot = (uintptr_t) p % HASH_TABLE_SIZE;
1cd4fffc
LC
130 void **chain = &mmalloca_results[slot];
131 for (; *chain != NULL;)
132 {
5e69ceb7 133 union header *h = p;
1cd4fffc
LC
134 if (*chain == p)
135 {
136 /* Found it. Remove it from the hash table and free it. */
5e69ceb7
MW
137 union header *p_begin = h - 1;
138 *chain = p_begin->next;
1cd4fffc
LC
139 free (p_begin);
140 return;
141 }
5e69ceb7
MW
142 h = *chain;
143 chain = &h[-1].next;
1cd4fffc
LC
144 }
145 }
ffca4c22
AW
146 /* At this point, we know it was not a mmalloca() result. */
147 }
148}
149#endif