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