xalloc.h 7.5 KB

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