Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / glib / galloca.h
blob47151ee8b6187e9b060945e4c17aeaf1e7f0e3a5
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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/>.
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/.
25 #ifndef __G_ALLOCA_H__
26 #define __G_ALLOCA_H__
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
30 #endif
32 #include <glib/gtypes.h>
34 #if defined(__BIONIC__) && defined (GLIB_HAVE_ALLOCA_H)
35 # include <alloca.h>
36 #elif defined(__GNUC__)
37 /* GCC does the right thing */
38 # undef alloca
39 # define alloca(size) __builtin_alloca (size)
40 #elif defined (GLIB_HAVE_ALLOCA_H)
41 /* a native and working alloca.h is there */
42 # include <alloca.h>
43 #else /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
44 # if defined(_MSC_VER) || defined(__DMC__)
45 # include <malloc.h>
46 # define alloca _alloca
47 # else /* !_MSC_VER && !__DMC__ */
48 # ifdef _AIX
49 # pragma alloca
50 # else /* !_AIX */
51 # ifndef alloca /* predefined by HP cc +Olibcalls */
52 G_BEGIN_DECLS
53 char *alloca ();
54 G_END_DECLS
55 # endif /* !alloca */
56 # endif /* !_AIX */
57 # endif /* !_MSC_VER && !__DMC__ */
58 #endif /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
60 /**
61 * g_alloca:
62 * @size: number of bytes to allocate.
64 * Allocates @size bytes on the stack; these bytes will be freed when the current
65 * stack frame is cleaned up. This macro essentially just wraps the alloca()
66 * function present on most UNIX variants.
67 * Thus it provides the same advantages and pitfalls as alloca():
69 * - alloca() is very fast, as on most systems it's implemented by just adjusting
70 * the stack pointer register.
72 * - It doesn't cause any memory fragmentation, within its scope, separate alloca()
73 * blocks just build up and are released together at function end.
75 * - Allocation sizes have to fit into the current stack frame. For instance in a
76 * threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes,
77 * so be sparse with alloca() uses.
79 * - Allocation failure due to insufficient stack space is not indicated with a %NULL
80 * return like e.g. with malloc(). Instead, most systems probably handle it the same
81 * way as out of stack space situations from infinite function recursion, i.e.
82 * with a segmentation fault.
84 * - Special care has to be taken when mixing alloca() with GNU C variable sized arrays.
85 * Stack space allocated with alloca() in the same scope as a variable sized array
86 * will be freed together with the variable sized array upon exit of that scope, and
87 * not upon exit of the enclosing function scope.
89 * Returns: space for @size bytes, allocated on the stack
91 #define g_alloca(size) alloca (size)
92 /**
93 * g_newa:
94 * @struct_type: Type of memory chunks to be allocated
95 * @n_structs: Number of chunks to be allocated
97 * Wraps g_alloca() in a more typesafe manner.
99 * Returns: Pointer to stack space for @n_structs chunks of type @struct_type
101 #define g_newa(struct_type, n_structs) ((struct_type*) g_alloca (sizeof (struct_type) * (gsize) (n_structs)))
103 #endif /* __G_ALLOCA_H__ */