1*56bb7041Schristos /* Safe automatic memory allocation.
2*56bb7041Schristos    Copyright (C) 2003-2007, 2009-2020 Free Software Foundation, Inc.
3*56bb7041Schristos    Written by Bruno Haible <bruno@clisp.org>, 2003.
4*56bb7041Schristos 
5*56bb7041Schristos    This program is free software; you can redistribute it and/or modify
6*56bb7041Schristos    it under the terms of the GNU General Public License as published by
7*56bb7041Schristos    the Free Software Foundation; either version 3, or (at your option)
8*56bb7041Schristos    any later version.
9*56bb7041Schristos 
10*56bb7041Schristos    This program is distributed in the hope that it will be useful,
11*56bb7041Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*56bb7041Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*56bb7041Schristos    GNU General Public License for more details.
14*56bb7041Schristos 
15*56bb7041Schristos    You should have received a copy of the GNU General Public License
16*56bb7041Schristos    along with this program; if not, see <https://www.gnu.org/licenses/>.  */
17*56bb7041Schristos 
18*56bb7041Schristos #ifndef _MALLOCA_H
19*56bb7041Schristos #define _MALLOCA_H
20*56bb7041Schristos 
21*56bb7041Schristos #include <alloca.h>
22*56bb7041Schristos #include <stddef.h>
23*56bb7041Schristos #include <stdlib.h>
24*56bb7041Schristos #include <stdint.h>
25*56bb7041Schristos 
26*56bb7041Schristos #include "xalloc-oversized.h"
27*56bb7041Schristos 
28*56bb7041Schristos 
29*56bb7041Schristos #ifdef __cplusplus
30*56bb7041Schristos extern "C" {
31*56bb7041Schristos #endif
32*56bb7041Schristos 
33*56bb7041Schristos 
34*56bb7041Schristos /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call
35*56bb7041Schristos    alloca(N); otherwise it returns NULL.  It either returns N bytes of
36*56bb7041Schristos    memory allocated on the stack, that lasts until the function returns,
37*56bb7041Schristos    or NULL.
38*56bb7041Schristos    Use of safe_alloca should be avoided:
39*56bb7041Schristos      - inside arguments of function calls - undefined behaviour,
40*56bb7041Schristos      - in inline functions - the allocation may actually last until the
41*56bb7041Schristos        calling function returns.
42*56bb7041Schristos */
43*56bb7041Schristos #if HAVE_ALLOCA
44*56bb7041Schristos /* The OS usually guarantees only one guard page at the bottom of the stack,
45*56bb7041Schristos    and a page size can be as small as 4096 bytes.  So we cannot safely
46*56bb7041Schristos    allocate anything larger than 4096 bytes.  Also care for the possibility
47*56bb7041Schristos    of a few compiler-allocated temporary stack slots.
48*56bb7041Schristos    This must be a macro, not a function.  */
49*56bb7041Schristos # define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL)
50*56bb7041Schristos #else
51*56bb7041Schristos # define safe_alloca(N) ((void) (N), NULL)
52*56bb7041Schristos #endif
53*56bb7041Schristos 
54*56bb7041Schristos /* malloca(N) is a safe variant of alloca(N).  It allocates N bytes of
55*56bb7041Schristos    memory allocated on the stack, that must be freed using freea() before
56*56bb7041Schristos    the function returns.  Upon failure, it returns NULL.  */
57*56bb7041Schristos #if HAVE_ALLOCA
58*56bb7041Schristos # define malloca(N) \
59*56bb7041Schristos   ((N) < 4032 - (2 * sa_alignment_max - 1)                                   \
60*56bb7041Schristos    ? (void *) (((uintptr_t) (char *) alloca ((N) + 2 * sa_alignment_max - 1) \
61*56bb7041Schristos                 + (2 * sa_alignment_max - 1))                                \
62*56bb7041Schristos                & ~(uintptr_t)(2 * sa_alignment_max - 1))                     \
63*56bb7041Schristos    : mmalloca (N))
64*56bb7041Schristos #else
65*56bb7041Schristos # define malloca(N) \
66*56bb7041Schristos   mmalloca (N)
67*56bb7041Schristos #endif
68*56bb7041Schristos extern void * mmalloca (size_t n);
69*56bb7041Schristos 
70*56bb7041Schristos /* Free a block of memory allocated through malloca().  */
71*56bb7041Schristos #if HAVE_ALLOCA
72*56bb7041Schristos extern void freea (void *p);
73*56bb7041Schristos #else
74*56bb7041Schristos # define freea free
75*56bb7041Schristos #endif
76*56bb7041Schristos 
77*56bb7041Schristos /* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
78*56bb7041Schristos    It allocates an array of N objects, each with S bytes of memory,
79*56bb7041Schristos    on the stack.  S must be positive and N must be nonnegative.
80*56bb7041Schristos    The array must be freed using freea() before the function returns.  */
81*56bb7041Schristos #define nmalloca(n, s) (xalloc_oversized (n, s) ? NULL : malloca ((n) * (s)))
82*56bb7041Schristos 
83*56bb7041Schristos 
84*56bb7041Schristos #ifdef __cplusplus
85*56bb7041Schristos }
86*56bb7041Schristos #endif
87*56bb7041Schristos 
88*56bb7041Schristos 
89*56bb7041Schristos /* ------------------- Auxiliary, non-public definitions ------------------- */
90*56bb7041Schristos 
91*56bb7041Schristos /* Determine the alignment of a type at compile time.  */
92*56bb7041Schristos #if defined __GNUC__ || defined __IBM__ALIGNOF__
93*56bb7041Schristos # define sa_alignof __alignof__
94*56bb7041Schristos #elif defined __cplusplus
95*56bb7041Schristos   template <class type> struct sa_alignof_helper { char __slot1; type __slot2; };
96*56bb7041Schristos # define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2)
97*56bb7041Schristos #elif defined __hpux
98*56bb7041Schristos   /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof
99*56bb7041Schristos      values.  */
100*56bb7041Schristos # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
101*56bb7041Schristos #elif defined _AIX
102*56bb7041Schristos   /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof
103*56bb7041Schristos      values.  */
104*56bb7041Schristos # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
105*56bb7041Schristos #else
106*56bb7041Schristos # define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
107*56bb7041Schristos #endif
108*56bb7041Schristos 
109*56bb7041Schristos enum
110*56bb7041Schristos {
111*56bb7041Schristos /* The desired alignment of memory allocations is the maximum alignment
112*56bb7041Schristos    among all elementary types.  */
113*56bb7041Schristos   sa_alignment_long = sa_alignof (long),
114*56bb7041Schristos   sa_alignment_double = sa_alignof (double),
115*56bb7041Schristos   sa_alignment_longlong = sa_alignof (long long),
116*56bb7041Schristos   sa_alignment_longdouble = sa_alignof (long double),
117*56bb7041Schristos   sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1)
118*56bb7041Schristos                       | (sa_alignment_longlong - 1)
119*56bb7041Schristos                       | (sa_alignment_longdouble - 1)
120*56bb7041Schristos                      ) + 1
121*56bb7041Schristos };
122*56bb7041Schristos 
123*56bb7041Schristos #endif /* _MALLOCA_H */
124