Use module 'alignof'.
[gnulib.git] / lib / malloca.h
1 /* Safe automatic memory allocation.
2    Copyright (C) 2003-2007, 2009 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2003.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program 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 General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifndef _MALLOCA_H
20 #define _MALLOCA_H
21
22 #include <alloca.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25
26 #include "alignof.h"
27
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33
34 /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call
35    alloca(N); otherwise it returns NULL.  It either returns N bytes of
36    memory allocated on the stack, that lasts until the function returns,
37    or NULL.
38    Use of safe_alloca should be avoided:
39      - inside arguments of function calls - undefined behaviour,
40      - in inline functions - the allocation may actually last until the
41        calling function returns.
42 */
43 #if HAVE_ALLOCA
44 /* The OS usually guarantees only one guard page at the bottom of the stack,
45    and a page size can be as small as 4096 bytes.  So we cannot safely
46    allocate anything larger than 4096 bytes.  Also care for the possibility
47    of a few compiler-allocated temporary stack slots.
48    This must be a macro, not an inline function.  */
49 # define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL)
50 #else
51 # define safe_alloca(N) ((void) (N), NULL)
52 #endif
53
54 /* malloca(N) is a safe variant of alloca(N).  It allocates N bytes of
55    memory allocated on the stack, that must be freed using freea() before
56    the function returns.  Upon failure, it returns NULL.  */
57 #if HAVE_ALLOCA
58 # define malloca(N) \
59   ((N) < 4032 - sa_increment                                        \
60    ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \
61    : mmalloca (N))
62 #else
63 # define malloca(N) \
64   mmalloca (N)
65 #endif
66 extern void * mmalloca (size_t n);
67
68 /* Free a block of memory allocated through malloca().  */
69 #if HAVE_ALLOCA
70 extern void freea (void *p);
71 #else
72 # define freea free
73 #endif
74
75 /* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
76    It allocates an array of N objects, each with S bytes of memory,
77    on the stack.  S must be positive and N must be nonnegative.
78    The array must be freed using freea() before the function returns.  */
79 #if 1
80 /* Cf. the definition of xalloc_oversized.  */
81 # define nmalloca(n, s) \
82     ((n) > (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) \
83      ? NULL \
84      : malloca ((n) * (s)))
85 #else
86 extern void * nmalloca (size_t n, size_t s);
87 #endif
88
89
90 #ifdef __cplusplus
91 }
92 #endif
93
94
95 /* ------------------- Auxiliary, non-public definitions ------------------- */
96
97 enum
98 {
99 /* The desired alignment of memory allocations is the maximum alignment
100    among all elementary types.  */
101   sa_alignment_long = alignof (long),
102   sa_alignment_double = alignof (double),
103 #if HAVE_LONG_LONG_INT
104   sa_alignment_longlong = alignof (long long),
105 #endif
106   sa_alignment_longdouble = alignof (long double),
107   sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1)
108 #if HAVE_LONG_LONG_INT
109                       | (sa_alignment_longlong - 1)
110 #endif
111                       | (sa_alignment_longdouble - 1)
112                      ) + 1,
113 /* The increment that guarantees room for a magic word must be >= sizeof (int)
114    and a multiple of sa_alignment_max.  */
115   sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max
116 };
117
118 #endif /* _MALLOCA_H */