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