xalloc.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* xalloc.h -- malloc with out-of-memory checking
  2. Copyright (C) 1990-2000, 2003-2004, 2006-2020 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 <https://www.gnu.org/licenses/>. */
  13. #ifndef XALLOC_H_
  14. #define XALLOC_H_
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #include "xalloc-oversized.h"
  18. #ifndef _GL_INLINE_HEADER_BEGIN
  19. #error "Please include config.h first."
  20. #endif
  21. _GL_INLINE_HEADER_BEGIN
  22. #ifndef XALLOC_INLINE
  23. # define XALLOC_INLINE _GL_INLINE
  24. #endif
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #if GNULIB_XALLOC_DIE
  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*/ _Noreturn void xalloc_die (void);
  35. #endif /* GNULIB_XALLOC_DIE */
  36. #if GNULIB_XALLOC
  37. void *xmalloc (size_t s)
  38. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
  39. void *xzalloc (size_t s)
  40. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
  41. void *xcalloc (size_t n, size_t s)
  42. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
  43. void *xrealloc (void *p, size_t s)
  44. _GL_ATTRIBUTE_ALLOC_SIZE ((2));
  45. void *x2realloc (void *p, size_t *pn);
  46. void *xmemdup (void const *p, size_t s)
  47. _GL_ATTRIBUTE_ALLOC_SIZE ((2));
  48. char *xstrdup (char const *str)
  49. _GL_ATTRIBUTE_MALLOC;
  50. /* In the following macros, T must be an elementary or structure/union or
  51. typedef'ed type, or a pointer to such a type. To apply one of the
  52. following macros to a function pointer or array type, you need to typedef
  53. it first and use the typedef name. */
  54. /* Allocate an object of type T dynamically, with error checking. */
  55. /* extern t *XMALLOC (typename t); */
  56. # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
  57. /* Allocate memory for N elements of type T, with error checking. */
  58. /* extern t *XNMALLOC (size_t n, typename t); */
  59. # define XNMALLOC(n, t) \
  60. ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
  61. /* Allocate an object of type T dynamically, with error checking,
  62. and zero it. */
  63. /* extern t *XZALLOC (typename t); */
  64. # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
  65. /* Allocate memory for N elements of type T, with error checking,
  66. and zero it. */
  67. /* extern t *XCALLOC (size_t n, typename t); */
  68. # define XCALLOC(n, t) \
  69. ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
  70. /* Allocate an array of N objects, each with S bytes of memory,
  71. dynamically, with error checking. S must be nonzero. */
  72. XALLOC_INLINE void *xnmalloc (size_t n, size_t s)
  73. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
  74. XALLOC_INLINE void *
  75. xnmalloc (size_t n, size_t s)
  76. {
  77. if (xalloc_oversized (n, s))
  78. xalloc_die ();
  79. return xmalloc (n * s);
  80. }
  81. /* Change the size of an allocated block of memory P to an array of N
  82. objects each of S bytes, with error checking. S must be nonzero. */
  83. XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
  84. _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
  85. XALLOC_INLINE void *
  86. xnrealloc (void *p, size_t n, size_t s)
  87. {
  88. if (xalloc_oversized (n, s))
  89. xalloc_die ();
  90. return xrealloc (p, n * s);
  91. }
  92. /* If P is null, allocate a block of at least *PN such objects;
  93. otherwise, reallocate P so that it contains more than *PN objects
  94. each of S bytes. S must be nonzero. Set *PN to the new number of
  95. objects, and return the pointer to the new block. *PN is never set
  96. to zero, and the returned pointer is never null.
  97. Repeated reallocations are guaranteed to make progress, either by
  98. allocating an initial block with a nonzero size, or by allocating a
  99. larger block.
  100. In the following implementation, nonzero sizes are increased by a
  101. factor of approximately 1.5 so that repeated reallocations have
  102. O(N) overall cost rather than O(N**2) cost, but the
  103. specification for this function does not guarantee that rate.
  104. Here is an example of use:
  105. int *p = NULL;
  106. size_t used = 0;
  107. size_t allocated = 0;
  108. void
  109. append_int (int value)
  110. {
  111. if (used == allocated)
  112. p = x2nrealloc (p, &allocated, sizeof *p);
  113. p[used++] = value;
  114. }
  115. This causes x2nrealloc to allocate a block of some nonzero size the
  116. first time it is called.
  117. To have finer-grained control over the initial size, set *PN to a
  118. nonzero value before calling this function with P == NULL. For
  119. example:
  120. int *p = NULL;
  121. size_t used = 0;
  122. size_t allocated = 0;
  123. size_t allocated1 = 1000;
  124. void
  125. append_int (int value)
  126. {
  127. if (used == allocated)
  128. {
  129. p = x2nrealloc (p, &allocated1, sizeof *p);
  130. allocated = allocated1;
  131. }
  132. p[used++] = value;
  133. }
  134. */
  135. XALLOC_INLINE void *
  136. x2nrealloc (void *p, size_t *pn, size_t s)
  137. {
  138. size_t n = *pn;
  139. if (! p)
  140. {
  141. if (! n)
  142. {
  143. /* The approximate size to use for initial small allocation
  144. requests, when the invoking code specifies an old size of
  145. zero. This is the largest "small" request for the GNU C
  146. library malloc. */
  147. enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
  148. n = DEFAULT_MXFAST / s;
  149. n += !n;
  150. }
  151. if (xalloc_oversized (n, s))
  152. xalloc_die ();
  153. }
  154. else
  155. {
  156. /* Set N = floor (1.5 * N) + 1 so that progress is made even if N == 0.
  157. Check for overflow, so that N * S stays in both ptrdiff_t and
  158. size_t range. The check may be slightly conservative, but an
  159. exact check isn't worth the trouble. */
  160. if ((PTRDIFF_MAX < SIZE_MAX ? PTRDIFF_MAX : SIZE_MAX) / 3 * 2 / s
  161. <= n)
  162. xalloc_die ();
  163. n += n / 2 + 1;
  164. }
  165. *pn = n;
  166. return xrealloc (p, n * s);
  167. }
  168. /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
  169. except it returns char *. */
  170. XALLOC_INLINE char *xcharalloc (size_t n)
  171. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
  172. XALLOC_INLINE char *
  173. xcharalloc (size_t n)
  174. {
  175. return XNMALLOC (n, char);
  176. }
  177. #endif /* GNULIB_XALLOC */
  178. #ifdef __cplusplus
  179. }
  180. #endif
  181. #if GNULIB_XALLOC && defined __cplusplus
  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 /* GNULIB_XALLOC && C++ */
  211. _GL_INLINE_HEADER_END
  212. #endif /* !XALLOC_H_ */