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