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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8944
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
8944
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #ifndef _XMALLOCA_H
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #define _XMALLOCA_H
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include "malloca.h"
9559
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
22 #include "xalloc.h"
8944
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #ifdef __cplusplus
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 extern "C" {
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #endif
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 /* xmalloca(N) is a checking safe variant of alloca(N). It allocates N bytes
9318
18c67ec54b59 Fix typo in comment.
Ben Pfaff <blp@cs.stanford.edu>
parents: 9309
diff changeset
31 of memory allocated on the stack, that must be freed using freea() before
8944
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 the function returns. Upon failure, it exits with an error message. */
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #if HAVE_ALLOCA
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 : xmmalloca (N))
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 extern void * xmmalloca (size_t n);
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #else
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 # define xmalloca(N) \
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 xmalloc (N)
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #endif
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
9559
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
46 /* xnmalloca(N,S) is an overflow-safe variant of xmalloca (N * S).
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
47 It allocates an array of N objects, each with S bytes of memory,
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
48 on the stack. S must be positive and N must be nonnegative.
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
49 The array must be freed using freea() before the function returns.
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
50 Upon failure, it exits with an error message. */
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
51 #if HAVE_ALLOCA
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
52 /* Rely on xmalloca (SIZE_MAX) calling xalloc_die (). */
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
53 # define xnmalloca(n, s) \
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
54 xmalloca (xalloc_oversized ((n), (s)) ? (size_t) (-1) : (n) * (s))
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
55 #else
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
56 # define xnmalloca(n, s) \
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
57 xnmalloc ((n), (s))
7913bdfb0cf8 New macro xnmalloca.
Bruno Haible <bruno@clisp.org>
parents: 9318
diff changeset
58 #endif
8944
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 #ifdef __cplusplus
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 }
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 #endif
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65
b04902755c7f Move to here from xallocsa.h.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 #endif /* _XMALLOCA_H */