xmalloc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* xmalloc.c -- malloc with out of memory checking
  2. Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2003,
  3. 1999, 2000, 2002, 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include "xalloc.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "gettext.h"
  22. #define _(msgid) gettext (msgid)
  23. #define N_(msgid) msgid
  24. #include "error.h"
  25. #include "exitfail.h"
  26. #ifndef SIZE_MAX
  27. # define SIZE_MAX ((size_t) -1)
  28. #endif
  29. #ifndef HAVE_MALLOC
  30. "you must run the autoconf test for a GNU libc compatible malloc"
  31. #endif
  32. #ifndef HAVE_REALLOC
  33. "you must run the autoconf test for a GNU libc compatible realloc"
  34. #endif
  35. /* If non NULL, call this function when memory is exhausted. */
  36. void (*xalloc_fail_func) (void) = 0;
  37. /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
  38. before exiting when memory is exhausted. Goes through gettext. */
  39. char const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
  40. void
  41. xalloc_die (void)
  42. {
  43. if (xalloc_fail_func)
  44. (*xalloc_fail_func) ();
  45. error (exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
  46. /* The `noreturn' cannot be given to error, since it may return if
  47. its first argument is 0. To help compilers understand the
  48. xalloc_die does terminate, call abort. */
  49. abort ();
  50. }
  51. /* Allocate an array of N objects, each with S bytes of memory,
  52. dynamically, with error checking. S must be nonzero. */
  53. static inline void *
  54. xnmalloc_inline (size_t n, size_t s)
  55. {
  56. void *p;
  57. if (xalloc_oversized (n, s) || ! (p = malloc (n * s)))
  58. xalloc_die ();
  59. return p;
  60. }
  61. void *
  62. xnmalloc (size_t n, size_t s)
  63. {
  64. return xnmalloc_inline (n, s);
  65. }
  66. /* Allocate N bytes of memory dynamically, with error checking. */
  67. void *
  68. xmalloc (size_t n)
  69. {
  70. return xnmalloc_inline (n, 1);
  71. }
  72. /* Change the size of an allocated block of memory P to an array of N
  73. objects each of S bytes, with error checking. S must be nonzero. */
  74. static inline void *
  75. xnrealloc_inline (void *p, size_t n, size_t s)
  76. {
  77. if (xalloc_oversized (n, s) || ! (p = realloc (p, n * s)))
  78. xalloc_die ();
  79. return p;
  80. }
  81. void *
  82. xnrealloc (void *p, size_t n, size_t s)
  83. {
  84. return xnrealloc_inline (p, n, s);
  85. }
  86. /* Change the size of an allocated block of memory P to N bytes,
  87. with error checking. */
  88. void *
  89. xrealloc (void *p, size_t n)
  90. {
  91. return xnrealloc_inline (p, n, 1);
  92. }
  93. /* If P is null, allocate a block of at least *PN such objects;
  94. otherwise, reallocate P so that it contains more than *PN objects
  95. each of S bytes. *PN must be nonzero unless P is null, and S must
  96. be nonzero. Set *PN to the new number of objects, and return the
  97. pointer to the new block. *PN is never set to zero, and the
  98. returned pointer is never null.
  99. Repeated reallocations are guaranteed to make progress, either by
  100. allocating an initial block with a nonzero size, or by allocating a
  101. larger block.
  102. In the following implementation, nonzero sizes are doubled so that
  103. repeated reallocations have O(N log N) overall cost rather than
  104. O(N**2) cost, but the specification for this function does not
  105. guarantee that sizes are doubled.
  106. Here is an example of use:
  107. int *p = NULL;
  108. size_t used = 0;
  109. size_t allocated = 0;
  110. void
  111. append_int (int value)
  112. {
  113. if (used == allocated)
  114. p = x2nrealloc (p, &allocated, sizeof *p);
  115. p[used++] = value;
  116. }
  117. This causes x2nrealloc to allocate a block of some nonzero size the
  118. first time it is called.
  119. To have finer-grained control over the initial size, set *PN to a
  120. nonzero value before calling this function with P == NULL. For
  121. example:
  122. int *p = NULL;
  123. size_t used = 0;
  124. size_t allocated = 0;
  125. size_t allocated1 = 1000;
  126. void
  127. append_int (int value)
  128. {
  129. if (used == allocated)
  130. {
  131. p = x2nrealloc (p, &allocated1, sizeof *p);
  132. allocated = allocated1;
  133. }
  134. p[used++] = value;
  135. }
  136. */
  137. static inline void *
  138. x2nrealloc_inline (void *p, size_t *pn, size_t s)
  139. {
  140. size_t n = *pn;
  141. if (! p)
  142. {
  143. if (! n)
  144. {
  145. /* The approximate size to use for initial small allocation
  146. requests, when the invoking code specifies an old size of
  147. zero. 64 bytes is the largest "small" request for the
  148. GNU C library malloc. */
  149. enum { DEFAULT_MXFAST = 64 };
  150. n = DEFAULT_MXFAST / s;
  151. n += !n;
  152. }
  153. }
  154. else
  155. {
  156. if (SIZE_MAX / 2 / s < n)
  157. xalloc_die ();
  158. n *= 2;
  159. }
  160. *pn = n;
  161. return xrealloc (p, n * s);
  162. }
  163. void *
  164. x2nrealloc (void *p, size_t *pn, size_t s)
  165. {
  166. return x2nrealloc_inline (p, pn, s);
  167. }
  168. /* If P is null, allocate a block of at least *PN bytes; otherwise,
  169. reallocate P so that it contains more than *PN bytes. *PN must be
  170. nonzero unless P is null. Set *PN to the new block's size, and
  171. return the pointer to the new block. *PN is never set to zero, and
  172. the returned pointer is never null. */
  173. void *
  174. x2realloc (void *p, size_t *pn)
  175. {
  176. return x2nrealloc_inline (p, pn, 1);
  177. }
  178. /* Allocate S bytes of zeroed memory dynamically, with error checking.
  179. There's no need for xnzalloc (N, S), since it would be equivalent
  180. to xcalloc (N, S). */
  181. void *
  182. xzalloc (size_t s)
  183. {
  184. return memset (xmalloc (s), 0, s);
  185. }
  186. /* Allocate zeroed memory for N elements of S bytes, with error
  187. checking. S must be nonzero. */
  188. void *
  189. xcalloc (size_t n, size_t s)
  190. {
  191. void *p;
  192. /* Test for overflow, since some calloc implementations don't have
  193. proper overflow checks. */
  194. if (xalloc_oversized (n, s) || ! (p = calloc (n, s)))
  195. xalloc_die ();
  196. return p;
  197. }
  198. /* Clone an object P of size S, with error checking. There's no need
  199. for xnclone (P, N, S), since xclone (P, N * S) works without any
  200. need for an arithmetic overflow check. */
  201. void *
  202. xclone (void const *p, size_t s)
  203. {
  204. return memcpy (xmalloc (s), p, s);
  205. }