xalloc.h 7.5 KB

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