Blame glib/glib/galloca.h

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