Mercurial > gnulib
annotate lib/xmalloca.h @ 40203:30d91797009f
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Fri, 01 Mar 2019 08:42:42 -0800 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
8944 | 1 /* Safe automatic memory allocation with out of memory checking. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19539
diff
changeset
|
2 Copyright (C) 2003, 2005, 2007, 2009-2019 Free Software Foundation, Inc. |
8944 | 3 Written by Bruno Haible <bruno@clisp.org>, 2003. |
4 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8944
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
8944 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8944
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8944
diff
changeset
|
8 (at your option) any later version. |
8944 | 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 | |
19190 | 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
8944 | 17 |
18 #ifndef _XMALLOCA_H | |
19 #define _XMALLOCA_H | |
20 | |
21 #include "malloca.h" | |
9559 | 22 #include "xalloc.h" |
8944 | 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 | |
9318 | 31 of memory allocated on the stack, that must be freed using freea() before |
8944 | 32 the function returns. Upon failure, it exits with an error message. */ |
33 #if HAVE_ALLOCA | |
34 # define xmalloca(N) \ | |
19539
149ea3012b47
xmalloca: pacify gcc -Wbad-function-cast
Bruno Haible <bruno@clisp.org>
parents:
19520
diff
changeset
|
35 ((N) < 4032 - (2 * sa_alignment_max - 1) \ |
149ea3012b47
xmalloca: pacify gcc -Wbad-function-cast
Bruno Haible <bruno@clisp.org>
parents:
19520
diff
changeset
|
36 ? (void *) (((uintptr_t) (char *) alloca ((N) + 2 * sa_alignment_max - 1) \ |
149ea3012b47
xmalloca: pacify gcc -Wbad-function-cast
Bruno Haible <bruno@clisp.org>
parents:
19520
diff
changeset
|
37 + (2 * sa_alignment_max - 1)) \ |
149ea3012b47
xmalloca: pacify gcc -Wbad-function-cast
Bruno Haible <bruno@clisp.org>
parents:
19520
diff
changeset
|
38 & ~(uintptr_t)(2 * sa_alignment_max - 1)) \ |
8944 | 39 : xmmalloca (N)) |
40 extern void * xmmalloca (size_t n); | |
41 #else | |
42 # define xmalloca(N) \ | |
43 xmalloc (N) | |
44 #endif | |
45 | |
9559 | 46 /* xnmalloca(N,S) is an overflow-safe variant of xmalloca (N * S). |
47 It allocates an array of N objects, each with S bytes of memory, | |
48 on the stack. S must be positive and N must be nonnegative. | |
49 The array must be freed using freea() before the function returns. | |
50 Upon failure, it exits with an error message. */ | |
51 #if HAVE_ALLOCA | |
52 /* Rely on xmalloca (SIZE_MAX) calling xalloc_die (). */ | |
53 # define xnmalloca(n, s) \ | |
54 xmalloca (xalloc_oversized ((n), (s)) ? (size_t) (-1) : (n) * (s)) | |
55 #else | |
56 # define xnmalloca(n, s) \ | |
57 xnmalloc ((n), (s)) | |
58 #endif | |
8944 | 59 |
60 | |
61 #ifdef __cplusplus | |
62 } | |
63 #endif | |
64 | |
65 | |
66 #endif /* _XMALLOCA_H */ |