Blame glib/galloca.h

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#ifndef __G_ALLOCA_H__
Packit ae235b
#define __G_ALLOCA_H__
Packit ae235b
Packit ae235b
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
Packit ae235b
#error "Only <glib.h> can be included directly."
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include <glib/gtypes.h>
Packit ae235b
Packit ae235b
#if defined(__BIONIC__) && defined (GLIB_HAVE_ALLOCA_H)
Packit ae235b
# include <alloca.h>
Packit ae235b
#elif defined(__GNUC__)
Packit ae235b
/* GCC does the right thing */
Packit ae235b
# undef alloca
Packit ae235b
# define alloca(size)   __builtin_alloca (size)
Packit ae235b
#elif defined (GLIB_HAVE_ALLOCA_H)
Packit ae235b
/* a native and working alloca.h is there */ 
Packit ae235b
# include <alloca.h>
Packit ae235b
#else /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
Packit ae235b
# if defined(_MSC_VER) || defined(__DMC__)
Packit ae235b
#  include <malloc.h>
Packit ae235b
#  define alloca _alloca
Packit ae235b
# else /* !_MSC_VER && !__DMC__ */
Packit ae235b
#  ifdef _AIX
Packit ae235b
#   pragma alloca
Packit ae235b
#  else /* !_AIX */
Packit ae235b
#   ifndef alloca /* predefined by HP cc +Olibcalls */
Packit ae235b
G_BEGIN_DECLS
Packit ae235b
char *alloca ();
Packit ae235b
G_END_DECLS
Packit ae235b
#   endif /* !alloca */
Packit ae235b
#  endif /* !_AIX */
Packit ae235b
# endif /* !_MSC_VER && !__DMC__ */
Packit ae235b
#endif /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_alloca:
Packit ae235b
 * @size: number of bytes to allocate.
Packit ae235b
 * 
Packit ae235b
 * Allocates @size bytes on the stack; these bytes will be freed when the current
Packit ae235b
 * stack frame is cleaned up. This macro essentially just wraps the alloca()
Packit ae235b
 * function present on most UNIX variants.
Packit ae235b
 * Thus it provides the same advantages and pitfalls as alloca():
Packit ae235b
 *
Packit ae235b
 * - alloca() is very fast, as on most systems it's implemented by just adjusting
Packit ae235b
 *   the stack pointer register.
Packit ae235b
 *
Packit ae235b
 * - It doesn't cause any memory fragmentation, within its scope, separate alloca()
Packit ae235b
 *   blocks just build up and are released together at function end.
Packit ae235b
 *
Packit ae235b
 * - Allocation sizes have to fit into the current stack frame. For instance in a
Packit ae235b
 *   threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes,
Packit ae235b
 *   so be sparse with alloca() uses.
Packit ae235b
 *
Packit ae235b
 * - Allocation failure due to insufficient stack space is not indicated with a %NULL
Packit ae235b
 *   return like e.g. with malloc(). Instead, most systems probably handle it the same
Packit ae235b
 *   way as out of stack space situations from infinite function recursion, i.e.
Packit ae235b
 *   with a segmentation fault.
Packit ae235b
 *
Packit ae235b
 * - Special care has to be taken when mixing alloca() with GNU C variable sized arrays.
Packit ae235b
 *   Stack space allocated with alloca() in the same scope as a variable sized array
Packit ae235b
 *   will be freed together with the variable sized array upon exit of that scope, and
Packit ae235b
 *   not upon exit of the enclosing function scope.
Packit ae235b
 * 
Packit ae235b
 * Returns: space for @size bytes, allocated on the stack
Packit ae235b
 */
Packit ae235b
#define g_alloca(size)		 alloca (size)
Packit ae235b
/**
Packit ae235b
 * g_newa:
Packit ae235b
 * @struct_type: Type of memory chunks to be allocated
Packit ae235b
 * @n_structs: Number of chunks to be allocated
Packit ae235b
 * 
Packit ae235b
 * Wraps g_alloca() in a more typesafe manner.
Packit ae235b
 * 
Packit ae235b
 * Returns: Pointer to stack space for @n_structs chunks of type @struct_type
Packit ae235b
 */
Packit ae235b
#define g_newa(struct_type, n_structs)	((struct_type*) g_alloca (sizeof (struct_type) * (gsize) (n_structs)))
Packit ae235b
Packit ae235b
#endif /* __G_ALLOCA_H__ */