malloca.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Safe automatic memory allocation.
  2. Copyright (C) 2003-2007 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2003.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. This program 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #ifndef _MALLOCA_H
  16. #define _MALLOCA_H
  17. #include <alloca.h>
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* safe_alloca(N) is equivalent to alloca(N) when it is safe to call
  24. alloca(N); otherwise it returns NULL. It either returns N bytes of
  25. memory allocated on the stack, that lasts until the function returns,
  26. or NULL.
  27. Use of safe_alloca should be avoided:
  28. - inside arguments of function calls - undefined behaviour,
  29. - in inline functions - the allocation may actually last until the
  30. calling function returns.
  31. */
  32. #if HAVE_ALLOCA
  33. /* The OS usually guarantees only one guard page at the bottom of the stack,
  34. and a page size can be as small as 4096 bytes. So we cannot safely
  35. allocate anything larger than 4096 bytes. Also care for the possibility
  36. of a few compiler-allocated temporary stack slots.
  37. This must be a macro, not an inline function. */
  38. # define safe_alloca(N) ((N) < 4032 ? alloca (N) : NULL)
  39. #else
  40. # define safe_alloca(N) ((void) (N), NULL)
  41. #endif
  42. /* malloca(N) is a safe variant of alloca(N). It allocates N bytes of
  43. memory allocated on the stack, that must be freed using freea() before
  44. the function returns. Upon failure, it returns NULL. */
  45. #if HAVE_ALLOCA
  46. # define malloca(N) \
  47. ((N) < 4032 - sa_increment \
  48. ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \
  49. : mmalloca (N))
  50. #else
  51. # define malloca(N) \
  52. mmalloca (N)
  53. #endif
  54. extern void * mmalloca (size_t n);
  55. /* Free a block of memory allocated through malloca(). */
  56. #if HAVE_ALLOCA
  57. extern void freea (void *p);
  58. #else
  59. # define freea free
  60. #endif
  61. /* nmalloca(N,S) is an overflow-safe variant of malloca (N * S).
  62. It allocates an array of N objects, each with S bytes of memory,
  63. on the stack. S must be positive and N must be nonnegative.
  64. The array must be freed using freea() before the function returns. */
  65. #if 1
  66. /* Cf. the definition of xalloc_oversized. */
  67. # define nmalloca(n, s) \
  68. ((n) > (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) \
  69. ? NULL \
  70. : malloca ((n) * (s)))
  71. #else
  72. extern void * nmalloca (size_t n, size_t s);
  73. #endif
  74. #ifdef __cplusplus
  75. }
  76. #endif
  77. /* ------------------- Auxiliary, non-public definitions ------------------- */
  78. /* Determine the alignment of a type at compile time. */
  79. #if defined __GNUC__
  80. # define sa_alignof __alignof__
  81. #elif defined __cplusplus
  82. template <class type> struct sa_alignof_helper { char __slot1; type __slot2; };
  83. # define sa_alignof(type) offsetof (sa_alignof_helper<type>, __slot2)
  84. #elif defined __hpux
  85. /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof
  86. values. */
  87. # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
  88. #elif defined _AIX
  89. /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof
  90. values. */
  91. # define sa_alignof(type) (sizeof (type) <= 4 ? 4 : 8)
  92. #else
  93. # define sa_alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
  94. #endif
  95. enum
  96. {
  97. /* The desired alignment of memory allocations is the maximum alignment
  98. among all elementary types. */
  99. sa_alignment_long = sa_alignof (long),
  100. sa_alignment_double = sa_alignof (double),
  101. #if HAVE_LONG_LONG_INT
  102. sa_alignment_longlong = sa_alignof (long long),
  103. #endif
  104. sa_alignment_longdouble = sa_alignof (long double),
  105. sa_alignment_max = ((sa_alignment_long - 1) | (sa_alignment_double - 1)
  106. #if HAVE_LONG_LONG_INT
  107. | (sa_alignment_longlong - 1)
  108. #endif
  109. | (sa_alignment_longdouble - 1)
  110. ) + 1,
  111. /* The increment that guarantees room for a magic word must be >= sizeof (int)
  112. and a multiple of sa_alignment_max. */
  113. sa_increment = ((sizeof (int) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max
  114. };
  115. #endif /* _MALLOCA_H */