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