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