xmalloc.c 3.7 KB

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