xmalloc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* xmalloc.c -- malloc with out of memory checking
  2. Copyright (C) 1990-2000, 2002-2006, 2008-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. #include <config.h>
  14. #define XALLOC_INLINE _GL_EXTERN_INLINE
  15. #include "xalloc.h"
  16. #include <stdlib.h>
  17. #include <string.h>
  18. /* 1 if calloc is known to be compatible with GNU calloc. This
  19. matters if we are not also using the calloc module, which defines
  20. HAVE_CALLOC_GNU and supports the GNU API even on non-GNU platforms. */
  21. #if defined HAVE_CALLOC_GNU || (defined __GLIBC__ && !defined __UCLIBC__)
  22. enum { HAVE_GNU_CALLOC = 1 };
  23. #else
  24. enum { HAVE_GNU_CALLOC = 0 };
  25. #endif
  26. /* Allocate N bytes of memory dynamically, with error checking. */
  27. void *
  28. xmalloc (size_t n)
  29. {
  30. void *p = malloc (n);
  31. if (!p && n != 0)
  32. xalloc_die ();
  33. return p;
  34. }
  35. /* Change the size of an allocated block of memory P to N bytes,
  36. with error checking. */
  37. void *
  38. xrealloc (void *p, size_t n)
  39. {
  40. if (!n && p)
  41. {
  42. /* The GNU and C99 realloc behaviors disagree here. Act like
  43. GNU, even if the underlying realloc is C99. */
  44. free (p);
  45. return NULL;
  46. }
  47. p = realloc (p, n);
  48. if (!p && n)
  49. xalloc_die ();
  50. return p;
  51. }
  52. /* If P is null, allocate a block of at least *PN bytes; otherwise,
  53. reallocate P so that it contains more than *PN bytes. *PN must be
  54. nonzero unless P is null. Set *PN to the new block's size, and
  55. return the pointer to the new block. *PN is never set to zero, and
  56. the returned pointer is never null. */
  57. void *
  58. x2realloc (void *p, size_t *pn)
  59. {
  60. return x2nrealloc (p, pn, 1);
  61. }
  62. /* Allocate S bytes of zeroed memory dynamically, with error checking.
  63. There's no need for xnzalloc (N, S), since it would be equivalent
  64. to xcalloc (N, S). */
  65. void *
  66. xzalloc (size_t s)
  67. {
  68. return memset (xmalloc (s), 0, s);
  69. }
  70. /* Allocate zeroed memory for N elements of S bytes, with error
  71. checking. S must be nonzero. */
  72. void *
  73. xcalloc (size_t n, size_t s)
  74. {
  75. void *p;
  76. /* Test for overflow, since some calloc implementations don't have
  77. proper overflow checks. But omit overflow and size-zero tests if
  78. HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
  79. returns NULL if successful. */
  80. if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
  81. || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
  82. xalloc_die ();
  83. return p;
  84. }
  85. /* Clone an object P of size S, with error checking. There's no need
  86. for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
  87. need for an arithmetic overflow check. */
  88. void *
  89. xmemdup (void const *p, size_t s)
  90. {
  91. return memcpy (xmalloc (s), p, s);
  92. }
  93. /* Clone STRING. */
  94. char *
  95. xstrdup (char const *string)
  96. {
  97. return xmemdup (string, strlen (string) + 1);
  98. }