Debian Codegraph @

gnulib/malloca.h

raw symbols SCIP
1 /* Safe automatic memory allocation.
2 Copyright (C) 2003-2007, 2009-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <[email protected]>, 2003.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #ifndef _MALLOCA_H
19 #define _MALLOCA_Hcxx . . $ `gnulib/malloca.h:19:9`!
20
21 /* This file uses _GL_ATTRIBUTE_ALLOC_SIZE, _GL_ATTRIBUTE_DEALLOC,
22 _GL_ATTRIBUTE_MALLOC, HAVE_ALLOCA. */
23 #if !_GL_CONFIG_H_INCLUDEDcxx . . $ `liblouis/config.h:10:9`!
24 #error "Please include config.h first."
25 #endif
26
27 #include <alloca.h>cxx . . $ `<file>/gnulib/alloca.h`/
28 #include <stddef.h>cxx . . $ `<file>/gnulib/stddef.h`/
29 #include <stdlib.h>cxx . . $ `<file>/gnulib/stdlib.h`/
30 #include <stdint.h>
31 #if defined __CHERI_PURE_CAPABILITY__
32 # include <cheri.h>
33 #endif
34
35 #include "xalloc-oversized.h"cxx . . $ `<file>/gnulib/xalloc-oversized.h`/
36
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42
43 /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call
44 alloca(N); otherwise it returns NULL. It either returns N bytes of
45 memory allocated on the stack, that lasts until the function returns,
46 or NULL.
47 Use of safe_alloca should be avoided:
48 - inside arguments of function calls - undefined behaviour,
49 - in inline functions - the allocation may actually last until the
50 calling function returns.
51 */
52 #if HAVE_ALLOCAcxx . . $ `liblouis/config.h:191:9`!
53 /* The OS usually guarantees only one guard page at the bottom of the stack,
54 and a page size can be as small as 4096 bytes. So we cannot safely
55 allocate anything larger than 4096 bytes. Also care for the possibility
56 of a few compiler-allocated temporary stack slots.
57 This must be a macro, not a function. */
58 # define safe_allocacxx . . $ `gnulib/malloca.h:58:10`!(N) ((N) < 4032 ? alloca (N) : NULL)
59 #else
60 # define safe_alloca(N) ((void) (N), NULL)
61 #endif
62
63 /* Free a block of memory allocated through malloca(). */
64 #if HAVE_ALLOCAcxx . . $ `liblouis/config.h:191:9`!
65 extern void freeacxx . . $ freea(5d22bdacc48458e8). (void *p);
66 #else
67 # define freea free
68 #endif
69
70 /* malloca(N) is a safe variant of alloca(N). It allocates N bytes of
71 memory allocated on the stack, that must be freed using freea() before
72 the function returns. Upon failure, it returns NULL. */
73 #if HAVE_ALLOCAcxx . . $ `liblouis/config.h:191:9`!
74 # if defined __CHERI_PURE_CAPABILITY__
75 # define malloca(N) \
76 ((N) < 4032 - (2 * sa_alignment_max - 1) \
77 ? cheri_bounds_set ((void *) (((uintptr_t) \
78 (char *) \
79 alloca ((N) + 2 * sa_alignment_max - 1) \
80 + (2 * sa_alignment_max - 1)) \
81 & ~(uintptr_t)(2 * sa_alignment_max - 1)), \
82 (N)) \
83 : mmalloca (N))
84 # else
85 # define mallocacxx . . $ `gnulib/malloca.h:85:11`!(N) \
86 ((N) < 4032 - (2 * sa_alignment_max - 1) \
87 ? (void *) (((uintptr_t) (char *) alloca ((N) + 2 * sa_alignment_max - 1) \
88 + (2 * sa_alignment_max - 1)) \
89 & ~(uintptr_t)(2 * sa_alignment_max - 1)) \
90 : mmalloca (N))
91 # endif
92 #else
93 # define malloca(N) \
94 mmalloca (N)
95 #endif
96 extern void *mmallocacxx . . $ mmalloca(bfcfa4d6b7f7ef64). (size_tcxx . . $ size_t# n)
97 _GL_ATTRIBUTE_MALLOCcxx . . $ `liblouis/config.h:1218:11`! _GL_ATTRIBUTE_DEALLOCcxx . . $ `liblouis/config.h:1092:11`! (freea, 1)
98 _GL_ATTRIBUTE_ALLOC_SIZEcxx . . $ `liblouis/config.h:1016:11`! ((1));
99
100 /* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
101 It allocates an array of N objects, each with S bytes of memory,
102 on the stack. N and S should be nonnegative and free of side effects.
103 The array must be freed using freea() before the function returns. */
104 #define nmallocacxx . . $ `gnulib/malloca.h:104:9`!(n, s) \
105 (xalloc_oversized (n, s) ? NULL : malloca ((n) * (size_t) (s)))
106
107
108 #ifdef __cplusplus
109 }
110 #endif
111
112
113 /* ------------------- Auxiliary, non-public definitions ------------------- */
114
115 /* Determine the alignment of a type at compile time. */
116 #if defined __GNUC__cxx . . $ `<built-in>:8:9`! || defined __clang__cxx . . $ `<built-in>:3:9`! || defined __IBM__ALIGNOF__
117 # define sa_alignofcxx . . $ `gnulib/malloca.h:117:10`! __alignof__
118 #elif defined __cplusplus
119 template <class type> struct sa_alignof_helper { char __slot1; type __slot2; };
120 # define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2)
121 #elif defined __hpux
122 /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof
123 values. */
124 # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
125 #elif defined _AIX
126 /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof
127 values. */
128 # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
129 #else
130 # define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
131 #endif
132
133 enumcxx . . $ $anonymous_type_11eafdca73acfd7d_0#
134 {
135 /* The desired alignment of memory allocations is the maximum alignment
136 among all elementary types. */
137 sa_alignment_longcxx . . $ sa_alignment_long. = sa_alignofcxx . . $ `gnulib/malloca.h:117:10`! (long),
138 sa_alignment_doublecxx . . $ sa_alignment_double. = sa_alignofcxx . . $ `gnulib/malloca.h:117:10`! (double),
139 sa_alignment_longlongcxx . . $ sa_alignment_longlong. = sa_alignofcxx . . $ `gnulib/malloca.h:117:10`! (long long),
140 sa_alignment_longdoublecxx . . $ sa_alignment_longdouble. = sa_alignofcxx . . $ `gnulib/malloca.h:117:10`! (long double),
141 sa_alignment_maxcxx . . $ sa_alignment_max. = ((sa_alignment_longcxx . . $ sa_alignment_long. - 1) | (sa_alignment_doublecxx . . $ sa_alignment_double. - 1)
142 | (sa_alignment_longlongcxx . . $ sa_alignment_longlong. - 1)
143 | (sa_alignment_longdoublecxx . . $ sa_alignment_longdouble. - 1)
144 ) + 1
145 };
146
147 #endif /* _MALLOCA_H */