xalloc.h 7.4 KB

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