xalloc.h 8.1 KB

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