gsettings test: fix srcdir != builddir
[glib.git] / glib / galloca.h
blob8ece1b297909d602112f3de736686bf291ff8036
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 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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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."
32 #endif
34 #include <glib/gtypes.h>
36 #if defined(__BIONIC__) && defined (GLIB_HAVE_ALLOCA_H)
37 # include <alloca.h>
38 #elif defined(__GNUC__)
39 /* GCC does the right thing */
40 # undef alloca
41 # define alloca(size) __builtin_alloca (size)
42 #elif defined (GLIB_HAVE_ALLOCA_H)
43 /* a native and working alloca.h is there */
44 # include <alloca.h>
45 #else /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
46 # if defined(_MSC_VER) || defined(__DMC__)
47 # include <malloc.h>
48 # define alloca _alloca
49 # else /* !_MSC_VER && !__DMC__ */
50 # ifdef _AIX
51 # pragma alloca
52 # else /* !_AIX */
53 # ifndef alloca /* predefined by HP cc +Olibcalls */
54 G_BEGIN_DECLS
55 char *alloca ();
56 G_END_DECLS
57 # endif /* !alloca */
58 # endif /* !_AIX */
59 # endif /* !_MSC_VER && !__DMC__ */
60 #endif /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
62 /**
63 * g_alloca:
64 * @size: number of bytes to allocate.
66 * Allocates @size bytes on the stack; these bytes will be freed when the current
67 * stack frame is cleaned up. This macro essentially just wraps the alloca()
68 * function present on most UNIX variants.
69 * Thus it provides the same advantages and pitfalls as alloca():
70 * <variablelist>
71 * <varlistentry><term></term><listitem><para>
72 * + alloca() is very fast, as on most systems it's implemented by just adjusting
73 * the stack pointer register.
74 * </para></listitem></varlistentry>
75 * <varlistentry><term></term><listitem><para>
76 * + It doesn't cause any memory fragmentation, within its scope, separate alloca()
77 * blocks just build up and are released together at function end.
78 * </para></listitem></varlistentry>
79 * <varlistentry><term></term><listitem><para>
80 * - Allocation sizes have to fit into the current stack frame. For instance in a
81 * threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes,
82 * so be sparse with alloca() uses.
83 * </para></listitem></varlistentry>
84 * <varlistentry><term></term><listitem><para>
85 * - Allocation failure due to insufficient stack space is not indicated with a %NULL
86 * return like e.g. with malloc(). Instead, most systems probably handle it the same
87 * way as out of stack space situations from infinite function recursion, i.e.
88 * with a segmentation fault.
89 * </para></listitem></varlistentry>
90 * <varlistentry><term></term><listitem><para>
91 * - Special care has to be taken when mixing alloca() with GNU C variable sized arrays.
92 * Stack space allocated with alloca() in the same scope as a variable sized array
93 * will be freed together with the variable sized array upon exit of that scope, and
94 * not upon exit of the enclosing function scope.
95 * </para></listitem></varlistentry>
96 * </variablelist>
98 * Returns: space for @size bytes, allocated on the stack
100 #define g_alloca(size) alloca (size)
102 * g_newa:
103 * @struct_type: Type of memory chunks to be allocated
104 * @n_structs: Number of chunks to be allocated
106 * Wraps g_alloca() in a more typesafe manner.
108 * Returns: Pointer to stack space for @n_structs chunks of type @struct_type
110 #define g_newa(struct_type, n_structs) ((struct_type*) g_alloca (sizeof (struct_type) * (gsize) (n_structs)))
112 #endif /* __G_ALLOCA_H__ */