1 /* Safe automatic memory allocation.
2 Copyright (C) 2003-2007, 2009-2023 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
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 #ifndef _MALLOCA_H
19 #define _MALLOCA_H
20
21 /* This file uses _GL_ATTRIBUTE_ALLOC_SIZE, _GL_ATTRIBUTE_DEALLOC,
22 _GL_ATTRIBUTE_MALLOC, HAVE_ALLOCA. */
23 #if !_GL_CONFIG_H_INCLUDED
24 #error "Please include config.h first."
25 #endif
26
27 #include <alloca.h>
28 #include <stddef.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31
32 #include "xalloc-oversized.h"
33
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39
40 /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call
41 alloca(N); otherwise it returns NULL. It either returns N bytes of
42 memory allocated on the stack, that lasts until the function returns,
43 or NULL.
44 Use of safe_alloca should be avoided:
45 - inside arguments of function calls - undefined behaviour,
46 - in inline functions - the allocation may actually last until the
47 calling function returns.
48 */
49 #if HAVE_ALLOCA
50 /* The OS usually guarantees only one guard page at the bottom of the stack,
51 and a page size can be as small as 4096 bytes. So we cannot safely
52 allocate anything larger than 4096 bytes. Also care for the possibility
53 of a few compiler-allocated temporary stack slots.
54 This must be a macro, not a function. */
55 # define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL)
56 #else
57 # define safe_alloca(N) ((void) (N), NULL)
58 #endif
59
60 /* Free a block of memory allocated through malloca(). */
61 #if HAVE_ALLOCA
62 extern void freea (void *p);
63 #else
64 # define freea free
65 #endif
66
67 /* malloca(N) is a safe variant of alloca(N). It allocates N bytes of
68 memory allocated on the stack, that must be freed using freea() before
69 the function returns. Upon failure, it returns NULL. */
70 #if HAVE_ALLOCA
71 # define malloca(N) \
72 ((N) < 4032 - (2 * sa_alignment_max - 1) \
73 ? (void *) (((uintptr_t) (char *) alloca ((N) + 2 * sa_alignment_max - 1) \
74 + (2 * sa_alignment_max - 1)) \
75 & ~(uintptr_t)(2 * sa_alignment_max - 1)) \
76 : mmalloca (N))
77 #else
78 # define malloca(N) \
79 mmalloca (N)
80 #endif
81 extern void *mmalloca (size_t n)
82 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (freea, 1)
83 _GL_ATTRIBUTE_ALLOC_SIZE ((1));
84
85 /* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
86 It allocates an array of N objects, each with S bytes of memory,
87 on the stack. N and S should be nonnegative and free of side effects.
88 The array must be freed using freea() before the function returns. */
89 #define nmalloca(n, s) \
90 (xalloc_oversized (n, s) ? NULL : malloca ((n) * (size_t) (s)))
91
92
93 #ifdef __cplusplus
94 }
95 #endif
96
97
98 /* ------------------- Auxiliary, non-public definitions ------------------- */
99
100 /* Determine the alignment of a type at compile time. */
101 #if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__
102 # define sa_alignof __alignof__
103 #elif defined __cplusplus
104 template <class type> struct sa_alignof_helper { char __slot1; type __slot2; };
105 # define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2)
106 #elif defined __hpux
107 /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof
108 values. */
109 # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
110 #elif defined _AIX
111 /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof
112 values. */
113 # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
114 #else
115 # define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
116 #endif
117
118 enum
119 {
120 /* The desired alignment of memory allocations is the maximum alignment
121 among all elementary types. */
122 sa_alignment_long = sa_alignof (long),
123 sa_alignment_double = sa_alignof (double),
124 sa_alignment_longlong = sa_alignof (long long),
125 sa_alignment_longdouble = sa_alignof (long double),
126 sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1)
127 | (sa_alignment_longlong - 1)
128 | (sa_alignment_longdouble - 1)
129 ) + 1
130 };
131
132 #endif /* _MALLOCA_H */