galloca.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  19. * file for a list of people on the GLib Team. See the ChangeLog
  20. * files for a list of changes. These files are distributed with
  21. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  22. */
  23. #ifndef __G_ALLOCA_H__
  24. #define __G_ALLOCA_H__
  25. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #include <glib/gtypes.h>
  29. #if defined(__BIONIC__) && defined (GLIB_HAVE_ALLOCA_H)
  30. # include <alloca.h>
  31. #elif defined(__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. *
  63. * - alloca() is very fast, as on most systems it's implemented by just adjusting
  64. * the stack pointer register.
  65. *
  66. * - It doesn't cause any memory fragmentation, within its scope, separate alloca()
  67. * blocks just build up and are released together at function end.
  68. *
  69. * - Allocation sizes have to fit into the current stack frame. For instance in a
  70. * threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes,
  71. * so be sparse with alloca() uses.
  72. *
  73. * - Allocation failure due to insufficient stack space is not indicated with a %NULL
  74. * return like e.g. with malloc(). Instead, most systems probably handle it the same
  75. * way as out of stack space situations from infinite function recursion, i.e.
  76. * with a segmentation fault.
  77. *
  78. * - Special care has to be taken when mixing alloca() with GNU C variable sized arrays.
  79. * Stack space allocated with alloca() in the same scope as a variable sized array
  80. * will be freed together with the variable sized array upon exit of that scope, and
  81. * not upon exit of the enclosing function scope.
  82. *
  83. * Returns: space for @size bytes, allocated on the stack
  84. */
  85. #define g_alloca(size) alloca (size)
  86. /**
  87. * g_newa:
  88. * @struct_type: Type of memory chunks to be allocated
  89. * @n_structs: Number of chunks to be allocated
  90. *
  91. * Wraps g_alloca() in a more typesafe manner.
  92. *
  93. * Returns: Pointer to stack space for @n_structs chunks of type @struct_type
  94. */
  95. #define g_newa(struct_type, n_structs) ((struct_type*) g_alloca (sizeof (struct_type) * (gsize) (n_structs)))
  96. #endif /* __G_ALLOCA_H__ */