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

source code of include/glib-2.0/glib/galloca.h