maint: update copyright
[gnulib.git] / lib / xmalloca.h
1 /* Safe automatic memory allocation with out of memory checking.
2    Copyright (C) 2003, 2005, 2007, 2009-2014 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 3 of the License, or
8    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
17
18 #ifndef _XMALLOCA_H
19 #define _XMALLOCA_H
20
21 #include "malloca.h"
22 #include "xalloc.h"
23
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29
30 /* xmalloca(N) is a checking safe variant of alloca(N).  It allocates N bytes
31    of memory allocated on the stack, that must be freed using freea() before
32    the function returns.  Upon failure, it exits with an error message.  */
33 #if HAVE_ALLOCA
34 # define xmalloca(N) \
35   ((N) < 4032 - sa_increment                                        \
36    ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \
37    : xmmalloca (N))
38 extern void * xmmalloca (size_t n);
39 #else
40 # define xmalloca(N) \
41   xmalloc (N)
42 #endif
43
44 /* xnmalloca(N,S) is an overflow-safe variant of xmalloca (N * S).
45    It allocates an array of N objects, each with S bytes of memory,
46    on the stack.  S must be positive and N must be nonnegative.
47    The array must be freed using freea() before the function returns.
48    Upon failure, it exits with an error message.  */
49 #if HAVE_ALLOCA
50 /* Rely on xmalloca (SIZE_MAX) calling xalloc_die ().  */
51 # define xnmalloca(n, s) \
52     xmalloca (xalloc_oversized ((n), (s)) ? (size_t) (-1) : (n) * (s))
53 #else
54 # define xnmalloca(n, s) \
55     xnmalloc ((n), (s))
56 #endif
57
58
59 #ifdef __cplusplus
60 }
61 #endif
62
63
64 #endif /* _XMALLOCA_H */