1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #ifndef __G_ALLOCA_H__
28 #define __G_ALLOCA_H__
30 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
34 #include <glib/gtypes.h>
37 #if defined(__BIONIC__) && defined (GLIB_HAVE_ALLOCA_H)
39 #elif defined(__GNUC__)
40 /* GCC does the right thing */
42 # define alloca(size) __builtin_alloca (size)
43 #elif defined (GLIB_HAVE_ALLOCA_H)
44 /* a native and working alloca.h is there */
46 #else /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
47 # if defined(_MSC_VER) || defined(__DMC__)
49 # define alloca _alloca
50 # else /* !_MSC_VER && !__DMC__ */
54 # ifndef alloca /* predefined by HP cc +Olibcalls */
60 # endif /* !_MSC_VER && !__DMC__ */
61 #endif /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
65 * @size: number of bytes to allocate.
67 * Allocates @size bytes on the stack; these bytes will be freed when the current
68 * stack frame is cleaned up. This macro essentially just wraps the alloca()
69 * function present on most UNIX variants.
70 * Thus it provides the same advantages and pitfalls as alloca():
72 * - alloca() is very fast, as on most systems it's implemented by just adjusting
73 * the stack pointer register.
75 * - It doesn't cause any memory fragmentation, within its scope, separate alloca()
76 * blocks just build up and are released together at function end.
78 * - Allocation sizes have to fit into the current stack frame. For instance in a
79 * threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes,
80 * so be sparse with alloca() uses.
82 * - Allocation failure due to insufficient stack space is not indicated with a %NULL
83 * return like e.g. with malloc(). Instead, most systems probably handle it the same
84 * way as out of stack space situations from infinite function recursion, i.e.
85 * with a segmentation fault.
87 * - Allowing @size to be specified by an untrusted party would allow for them
88 * to trigger a segmentation fault by specifying a large size, leading to a
89 * denial of service vulnerability. @size must always be entirely under the
90 * control of the program.
92 * - Special care has to be taken when mixing alloca() with GNU C variable sized arrays.
93 * Stack space allocated with alloca() in the same scope as a variable sized array
94 * will be freed together with the variable sized array upon exit of that scope, and
95 * not upon exit of the enclosing function scope.
97 * Returns: space for @size bytes, allocated on the stack
99 #define g_alloca(size) alloca (size)
103 * @size: number of bytes to allocate.
105 * Wraps g_alloca() and initializes allocated memory to zeroes.
106 * If @size is `0` it returns %NULL.
108 * Note that the @size argument will be evaluated multiple times.
110 * Returns: (nullable) (transfer full): space for @size bytes, allocated on the stack
114 #define g_alloca0(size) ((size) == 0 ? NULL : memset (g_alloca (size), 0, (size)))
118 * @struct_type: Type of memory chunks to be allocated
119 * @n_structs: Number of chunks to be allocated
121 * Wraps g_alloca() in a more typesafe manner.
123 * As mentioned in the documentation for g_alloca(), @n_structs must always be
124 * entirely under the control of the program, or you may introduce a denial of
125 * service vulnerability. In addition, the multiplication of @struct_type by
126 * @n_structs is not checked, so an overflow may lead to a remote code execution
129 * Returns: Pointer to stack space for @n_structs chunks of type @struct_type
131 #define g_newa(struct_type, n_structs) ((struct_type*) g_alloca (sizeof (struct_type) * (gsize) (n_structs)))
135 * @struct_type: the type of the elements to allocate.
136 * @n_structs: the number of elements to allocate.
138 * Wraps g_alloca0() in a more typesafe manner.
140 * Returns: (nullable) (transfer full): Pointer to stack space for @n_structs
141 * chunks of type @struct_type
145 #define g_newa0(struct_type, n_structs) ((struct_type*) g_alloca0 (sizeof (struct_type) * (gsize) (n_structs)))
147 #endif /* __G_ALLOCA_H__ */