galloca.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  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. #ifndef __G_ALLOCA_H__
  26. #define __G_ALLOCA_H__
  27. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  28. #error "Only <glib.h> can be included directly."
  29. #endif
  30. #include <glib/gtypes.h>
  31. #include <string.h>
  32. #if defined(__BIONIC__) && defined (GLIB_HAVE_ALLOCA_H)
  33. # include <alloca.h>
  34. #elif defined(__GNUC__)
  35. /* GCC does the right thing */
  36. # undef alloca
  37. # define alloca(size) __builtin_alloca (size)
  38. #elif defined (GLIB_HAVE_ALLOCA_H)
  39. /* a native and working alloca.h is there */
  40. # include <alloca.h>
  41. #else /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
  42. # if defined(_MSC_VER) || defined(__DMC__)
  43. # include <malloc.h>
  44. # define alloca _alloca
  45. # else /* !_MSC_VER && !__DMC__ */
  46. # ifdef _AIX
  47. # pragma alloca
  48. # else /* !_AIX */
  49. # ifndef alloca /* predefined by HP cc +Olibcalls */
  50. G_BEGIN_DECLS
  51. char *alloca ();
  52. G_END_DECLS
  53. # endif /* !alloca */
  54. # endif /* !_AIX */
  55. # endif /* !_MSC_VER && !__DMC__ */
  56. #endif /* !__GNUC__ && !GLIB_HAVE_ALLOCA_H */
  57. /**
  58. * g_alloca:
  59. * @size: number of bytes to allocate.
  60. *
  61. * Allocates @size bytes on the stack; these bytes will be freed when the current
  62. * stack frame is cleaned up. This macro essentially just wraps the alloca()
  63. * function present on most UNIX variants.
  64. * Thus it provides the same advantages and pitfalls as alloca():
  65. *
  66. * - alloca() is very fast, as on most systems it's implemented by just adjusting
  67. * the stack pointer register.
  68. *
  69. * - It doesn't cause any memory fragmentation, within its scope, separate alloca()
  70. * blocks just build up and are released together at function end.
  71. *
  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. *
  76. * - Allocation failure due to insufficient stack space is not indicated with a %NULL
  77. * return like e.g. with malloc(). Instead, most systems probably handle it the same
  78. * way as out of stack space situations from infinite function recursion, i.e.
  79. * with a segmentation fault.
  80. *
  81. * - Allowing @size to be specified by an untrusted party would allow for them
  82. * to trigger a segmentation fault by specifying a large size, leading to a
  83. * denial of service vulnerability. @size must always be entirely under the
  84. * control of the program.
  85. *
  86. * - Special care has to be taken when mixing alloca() with GNU C variable sized arrays.
  87. * Stack space allocated with alloca() in the same scope as a variable sized array
  88. * will be freed together with the variable sized array upon exit of that scope, and
  89. * not upon exit of the enclosing function scope.
  90. *
  91. * Returns: space for @size bytes, allocated on the stack
  92. */
  93. #define g_alloca(size) alloca (size)
  94. /**
  95. * g_alloca0:
  96. * @size: number of bytes to allocate.
  97. *
  98. * Wraps g_alloca() and initializes allocated memory to zeroes.
  99. * If @size is `0` it returns %NULL.
  100. *
  101. * Note that the @size argument will be evaluated multiple times.
  102. *
  103. * Returns: (nullable) (transfer full): space for @size bytes, allocated on the stack
  104. *
  105. * Since: 2.72
  106. */
  107. #define g_alloca0(size) ((size) == 0 ? NULL : memset (g_alloca (size), 0, (size)))
  108. /**
  109. * g_newa:
  110. * @struct_type: Type of memory chunks to be allocated
  111. * @n_structs: Number of chunks to be allocated
  112. *
  113. * Wraps g_alloca() in a more typesafe manner.
  114. *
  115. * As mentioned in the documentation for g_alloca(), @n_structs must always be
  116. * entirely under the control of the program, or you may introduce a denial of
  117. * service vulnerability. In addition, the multiplication of @struct_type by
  118. * @n_structs is not checked, so an overflow may lead to a remote code execution
  119. * vulnerability.
  120. *
  121. * Returns: Pointer to stack space for @n_structs chunks of type @struct_type
  122. */
  123. #define g_newa(struct_type, n_structs) ((struct_type*) g_alloca (sizeof (struct_type) * (gsize) (n_structs)))
  124. /**
  125. * g_newa0:
  126. * @struct_type: the type of the elements to allocate.
  127. * @n_structs: the number of elements to allocate.
  128. *
  129. * Wraps g_alloca0() in a more typesafe manner.
  130. *
  131. * Returns: (nullable) (transfer full): Pointer to stack space for @n_structs
  132. * chunks of type @struct_type
  133. *
  134. * Since: 2.72
  135. */
  136. #define g_newa0(struct_type, n_structs) ((struct_type*) g_alloca0 (sizeof (struct_type) * (gsize) (n_structs)))
  137. #endif /* __G_ALLOCA_H__ */