1 /* Safe automatic memory allocation.
2 Copyright (C) 2003, 2006-2007, 2009-2021 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003, 2018.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file 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
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #define _GL_USE_STDLIB_ALLOC 1
19 #include <config.h>
20
21 /* Specification. */
22 #include "malloca.h"
23
24 #include "idx.h"
25 #include "intprops.h"
26 #include "verify.h"
27
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 Here we use a bit in the address as an indicator, an idea by Ondřej Bílka.
33 malloca() can return three types of pointers:
34 - Pointers ≡ 0 mod 2*sa_alignment_max come from stack allocation.
35 - Pointers ≡ sa_alignment_max mod 2*sa_alignment_max come from heap
36 allocation.
37 - NULL comes from a failed heap allocation. */
38
39 /* Type for holding very small pointer differences. */
40 typedef unsigned char small_t;
41 /* Verify that it is wide enough. */
42 verify (2 * sa_alignment_max - 1 <= (small_t) -1);
43
44 void *
mmalloca(size_t n)45 mmalloca (size_t n)
46 {
47 #if HAVE_ALLOCA
48 /* Allocate one more word, used to determine the address to pass to freea(),
49 and room for the alignment ≡ sa_alignment_max mod 2*sa_alignment_max. */
50 int plus = sizeof (small_t) + 2 * sa_alignment_max - 1;
51 idx_t nplus;
52 if (!INT_ADD_WRAPV (n, plus, &nplus) && !xalloc_oversized (nplus, 1))
53 {
54 char *mem = (char *) malloc (nplus);
55
56 if (mem != NULL)
57 {
58 char *p =
59 (char *)((((uintptr_t)mem + sizeof (small_t) + sa_alignment_max - 1)
60 & ~(uintptr_t)(2 * sa_alignment_max - 1))
61 + sa_alignment_max);
62 /* Here p >= mem + sizeof (small_t),
63 and p <= mem + sizeof (small_t) + 2 * sa_alignment_max - 1
64 hence p + n <= mem + nplus.
65 So, the memory range [p, p+n) lies in the allocated memory range
66 [mem, mem + nplus). */
67 ((small_t *) p)[-1] = p - mem;
68 /* p ≡ sa_alignment_max mod 2*sa_alignment_max. */
69 return p;
70 }
71 }
72 /* Out of memory. */
73 return NULL;
74 #else
75 # if !MALLOC_0_IS_NONNULL
76 if (n == 0)
77 n = 1;
78 # endif
79 return malloc (n);
80 #endif
81 }
82
83 #if HAVE_ALLOCA
84 void
freea(void * p)85 freea (void *p)
86 {
87 /* Check argument. */
88 if ((uintptr_t) p & (sa_alignment_max - 1))
89 {
90 /* p was not the result of a malloca() call. Invalid argument. */
91 abort ();
92 }
93 /* Determine whether p was a non-NULL pointer returned by mmalloca(). */
94 if ((uintptr_t) p & sa_alignment_max)
95 {
96 void *mem = (char *) p - ((small_t *) p)[-1];
97 free (mem);
98 }
99 }
100 #endif
101
102 /*
103 * Hey Emacs!
104 * Local Variables:
105 * coding: utf-8
106 * End:
107 */
108