malloca.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Safe automatic memory allocation.
  2. Copyright (C) 2003-2007, 2009-2024 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2003.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #ifndef _MALLOCA_H
  15. #define _MALLOCA_H
  16. /* This file uses _GL_ATTRIBUTE_ALLOC_SIZE, _GL_ATTRIBUTE_DEALLOC,
  17. _GL_ATTRIBUTE_MALLOC, HAVE_ALLOCA. */
  18. #if !_GL_CONFIG_H_INCLUDED
  19. #error "Please include config.h first."
  20. #endif
  21. #include <alloca.h>
  22. #include <stddef.h>
  23. #include <stdlib.h>
  24. #include <stdint.h>
  25. #if defined __CHERI_PURE_CAPABILITY__
  26. # error #include <cheri.h>
  27. #endif
  28. #include "xalloc-oversized.h"
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call
  33. alloca(N); otherwise it returns NULL. It either returns N bytes of
  34. memory allocated on the stack, that lasts until the function returns,
  35. or NULL.
  36. Use of safe_alloca should be avoided:
  37. - inside arguments of function calls - undefined behaviour,
  38. - in inline functions - the allocation may actually last until the
  39. calling function returns.
  40. */
  41. #if HAVE_ALLOCA
  42. /* The OS usually guarantees only one guard page at the bottom of the stack,
  43. and a page size can be as small as 4096 bytes. So we cannot safely
  44. allocate anything larger than 4096 bytes. Also care for the possibility
  45. of a few compiler-allocated temporary stack slots.
  46. This must be a macro, not a function. */
  47. # define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL)
  48. #else
  49. # define safe_alloca(N) ((void) (N), NULL)
  50. #endif
  51. /* Free a block of memory allocated through malloca(). */
  52. #if HAVE_ALLOCA
  53. extern void freea (void *p);
  54. #else
  55. # define freea free
  56. #endif
  57. /* malloca(N) is a safe variant of alloca(N). It allocates N bytes of
  58. memory allocated on the stack, that must be freed using freea() before
  59. the function returns. Upon failure, it returns NULL. */
  60. #if HAVE_ALLOCA
  61. # if defined __CHERI_PURE_CAPABILITY__
  62. # define malloca(N) \
  63. ((N) < 4032 - (2 * sa_alignment_max - 1) \
  64. ? cheri_bounds_set ((void *) (((uintptr_t) \
  65. (char *) \
  66. alloca ((N) + 2 * sa_alignment_max - 1) \
  67. + (2 * sa_alignment_max - 1)) \
  68. & ~(uintptr_t)(2 * sa_alignment_max - 1)), \
  69. (N)) \
  70. : mmalloca (N))
  71. # else
  72. # define malloca(N) \
  73. ((N) < 4032 - (2 * sa_alignment_max - 1) \
  74. ? (void *) (((uintptr_t) (char *) alloca ((N) + 2 * sa_alignment_max - 1) \
  75. + (2 * sa_alignment_max - 1)) \
  76. & ~(uintptr_t)(2 * sa_alignment_max - 1)) \
  77. : mmalloca (N))
  78. # endif
  79. #else
  80. # define malloca(N) \
  81. mmalloca (N)
  82. #endif
  83. extern void *mmalloca (size_t n)
  84. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (freea, 1)
  85. _GL_ATTRIBUTE_ALLOC_SIZE ((1));
  86. /* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
  87. It allocates an array of N objects, each with S bytes of memory,
  88. on the stack. N and S should be nonnegative and free of side effects.
  89. The array must be freed using freea() before the function returns. */
  90. #define nmalloca(n, s) \
  91. (xalloc_oversized (n, s) ? NULL : malloca ((n) * (size_t) (s)))
  92. #ifdef __cplusplus
  93. }
  94. #endif
  95. /* ------------------- Auxiliary, non-public definitions ------------------- */
  96. /* Determine the alignment of a type at compile time. */
  97. #if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__
  98. # define sa_alignof __alignof__
  99. #elif defined __cplusplus
  100. template <class type> struct sa_alignof_helper { char __slot1; type __slot2; };
  101. # define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2)
  102. #elif defined __hpux
  103. /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof
  104. values. */
  105. # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
  106. #elif defined _AIX
  107. /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof
  108. values. */
  109. # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
  110. #else
  111. # define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
  112. #endif
  113. enum
  114. {
  115. /* The desired alignment of memory allocations is the maximum alignment
  116. among all elementary types. */
  117. sa_alignment_long = sa_alignof (long),
  118. sa_alignment_double = sa_alignof (double),
  119. sa_alignment_longlong = sa_alignof (long long),
  120. sa_alignment_longdouble = sa_alignof (long double),
  121. sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1)
  122. | (sa_alignment_longlong - 1)
  123. | (sa_alignment_longdouble - 1)
  124. ) + 1
  125. };
  126. #endif /* _MALLOCA_H */