You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

111 lines
4.3 KiB

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