xalloc.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* xalloc.h -- malloc with out-of-memory checking
  2. Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
  3. 1999, 2000, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
  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 of the License, or
  7. (at your option) 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, see <http://www.gnu.org/licenses/>. */
  14. #ifndef XALLOC_H_
  15. # define XALLOC_H_
  16. # include <stddef.h>
  17. # ifdef __cplusplus
  18. extern "C" {
  19. # endif
  20. # ifndef __attribute__
  21. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
  22. # define __attribute__(x)
  23. # endif
  24. # endif
  25. # ifndef ATTRIBUTE_NORETURN
  26. # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
  27. # endif
  28. # ifndef ATTRIBUTE_MALLOC
  29. # if __GNUC__ >= 3
  30. # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
  31. # else
  32. # define ATTRIBUTE_MALLOC
  33. # endif
  34. # endif
  35. /* This function is always triggered when memory is exhausted.
  36. It must be defined by the application, either explicitly
  37. or by using gnulib's xalloc-die module. This is the
  38. function to call when one wants the program to die because of a
  39. memory allocation failure. */
  40. extern void xalloc_die (void) ATTRIBUTE_NORETURN;
  41. void *xmalloc (size_t s) ATTRIBUTE_MALLOC;
  42. void *xzalloc (size_t s) ATTRIBUTE_MALLOC;
  43. void *xcalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
  44. void *xrealloc (void *p, size_t s);
  45. void *x2realloc (void *p, size_t *pn);
  46. void *xmemdup (void const *p, size_t s) ATTRIBUTE_MALLOC;
  47. char *xstrdup (char const *str) ATTRIBUTE_MALLOC;
  48. /* Return 1 if an array of N objects, each of size S, cannot exist due
  49. to size arithmetic overflow. S must be positive and N must be
  50. nonnegative. This is a macro, not an inline function, so that it
  51. works correctly even when SIZE_MAX < N.
  52. By gnulib convention, SIZE_MAX represents overflow in size
  53. calculations, so the conservative dividend to use here is
  54. SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
  55. However, malloc (SIZE_MAX) fails on all known hosts where
  56. sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
  57. exactly-SIZE_MAX allocations on such hosts; this avoids a test and
  58. branch when S is known to be 1. */
  59. # define xalloc_oversized(n, s) \
  60. ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
  61. /* In the following macros, T must be an elementary or structure/union or
  62. typedef'ed type, or a pointer to such a type. To apply one of the
  63. following macros to a function pointer or array type, you need to typedef
  64. it first and use the typedef name. */
  65. /* Allocate an object of type T dynamically, with error checking. */
  66. /* extern t *XMALLOC (typename t); */
  67. # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
  68. /* Allocate memory for N elements of type T, with error checking. */
  69. /* extern t *XNMALLOC (size_t n, typename t); */
  70. # define XNMALLOC(n, t) \
  71. ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
  72. /* Allocate an object of type T dynamically, with error checking,
  73. and zero it. */
  74. /* extern t *XZALLOC (typename t); */
  75. # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
  76. /* Allocate memory for N elements of type T, with error checking,
  77. and zero it. */
  78. /* extern t *XCALLOC (size_t n, typename t); */
  79. # define XCALLOC(n, t) \
  80. ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
  81. # if HAVE_INLINE
  82. # define static_inline static inline
  83. # else
  84. void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
  85. void *xnrealloc (void *p, size_t n, size_t s);
  86. void *x2nrealloc (void *p, size_t *pn, size_t s);
  87. char *xcharalloc (size_t n) ATTRIBUTE_MALLOC;
  88. # endif
  89. # ifdef static_inline
  90. /* Allocate an array of N objects, each with S bytes of memory,
  91. dynamically, with error checking. S must be nonzero. */
  92. static_inline void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
  93. static_inline void *
  94. xnmalloc (size_t n, size_t s)
  95. {
  96. if (xalloc_oversized (n, s))
  97. xalloc_die ();
  98. return xmalloc (n * s);
  99. }
  100. /* Change the size of an allocated block of memory P to an array of N
  101. objects each of S bytes, with error checking. S must be nonzero. */
  102. static_inline void *
  103. xnrealloc (void *p, size_t n, size_t s)
  104. {
  105. if (xalloc_oversized (n, s))
  106. xalloc_die ();
  107. return xrealloc (p, n * s);
  108. }
  109. /* If P is null, allocate a block of at least *PN such objects;
  110. otherwise, reallocate P so that it contains more than *PN objects
  111. each of S bytes. *PN must be nonzero unless P is null, and S must
  112. be nonzero. Set *PN to the new number of objects, and return the
  113. pointer to the new block. *PN is never set to zero, and the
  114. returned pointer is never null.
  115. Repeated reallocations are guaranteed to make progress, either by
  116. allocating an initial block with a nonzero size, or by allocating a
  117. larger block.
  118. In the following implementation, nonzero sizes are increased by a
  119. factor of approximately 1.5 so that repeated reallocations have
  120. O(N) overall cost rather than O(N**2) cost, but the
  121. specification for this function does not guarantee that rate.
  122. Here is an example of use:
  123. int *p = NULL;
  124. size_t used = 0;
  125. size_t allocated = 0;
  126. void
  127. append_int (int value)
  128. {
  129. if (used == allocated)
  130. p = x2nrealloc (p, &allocated, sizeof *p);
  131. p[used++] = value;
  132. }
  133. This causes x2nrealloc to allocate a block of some nonzero size the
  134. first time it is called.
  135. To have finer-grained control over the initial size, set *PN to a
  136. nonzero value before calling this function with P == NULL. For
  137. example:
  138. int *p = NULL;
  139. size_t used = 0;
  140. size_t allocated = 0;
  141. size_t allocated1 = 1000;
  142. void
  143. append_int (int value)
  144. {
  145. if (used == allocated)
  146. {
  147. p = x2nrealloc (p, &allocated1, sizeof *p);
  148. allocated = allocated1;
  149. }
  150. p[used++] = value;
  151. }
  152. */
  153. static_inline void *
  154. x2nrealloc (void *p, size_t *pn, size_t s)
  155. {
  156. size_t n = *pn;
  157. if (! p)
  158. {
  159. if (! n)
  160. {
  161. /* The approximate size to use for initial small allocation
  162. requests, when the invoking code specifies an old size of
  163. zero. 64 bytes is the largest "small" request for the
  164. GNU C library malloc. */
  165. enum { DEFAULT_MXFAST = 64 };
  166. n = DEFAULT_MXFAST / s;
  167. n += !n;
  168. }
  169. }
  170. else
  171. {
  172. /* Set N = ceil (1.5 * N) so that progress is made if N == 1.
  173. Check for overflow, so that N * S stays in size_t range.
  174. The check is slightly conservative, but an exact check isn't
  175. worth the trouble. */
  176. if ((size_t) -1 / 3 * 2 / s <= n)
  177. xalloc_die ();
  178. n += (n + 1) / 2;
  179. }
  180. *pn = n;
  181. return xrealloc (p, n * s);
  182. }
  183. /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
  184. except it returns char *. */
  185. static_inline char *xcharalloc (size_t n) ATTRIBUTE_MALLOC;
  186. static_inline char *
  187. xcharalloc (size_t n)
  188. {
  189. return XNMALLOC (n, char);
  190. }
  191. # endif
  192. # ifdef __cplusplus
  193. }
  194. /* C++ does not allow conversions from void * to other pointer types
  195. without a cast. Use templates to work around the problem when
  196. possible. */
  197. template <typename T> inline T *
  198. xrealloc (T *p, size_t s)
  199. {
  200. return (T *) xrealloc ((void *) p, s);
  201. }
  202. template <typename T> inline T *
  203. xnrealloc (T *p, size_t n, size_t s)
  204. {
  205. return (T *) xnrealloc ((void *) p, n, s);
  206. }
  207. template <typename T> inline T *
  208. x2realloc (T *p, size_t *pn)
  209. {
  210. return (T *) x2realloc ((void *) p, pn);
  211. }
  212. template <typename T> inline T *
  213. x2nrealloc (T *p, size_t *pn, size_t s)
  214. {
  215. return (T *) x2nrealloc ((void *) p, pn, s);
  216. }
  217. template <typename T> inline T *
  218. xmemdup (T const *p, size_t s)
  219. {
  220. return (T *) xmemdup ((void const *) p, s);
  221. }
  222. # endif
  223. #endif /* !XALLOC_H_ */