xmalloc.c 6.1 KB

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