xmalloc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, 2006 Free Software Foundation,
  4. Inc.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include <config.h>
  17. #if ! HAVE_INLINE
  18. # define static_inline
  19. #endif
  20. #include "xalloc.h"
  21. #undef static_inline
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifndef SIZE_MAX
  25. # define SIZE_MAX ((size_t) -1)
  26. #endif
  27. /* 1 if calloc is known to be compatible with GNU calloc. This
  28. matters if we are not also using the calloc module, which defines
  29. HAVE_CALLOC and supports the GNU API even on non-GNU platforms. */
  30. #if defined HAVE_CALLOC || defined __GLIBC__
  31. enum { HAVE_GNU_CALLOC = 1 };
  32. #else
  33. enum { HAVE_GNU_CALLOC = 0 };
  34. #endif
  35. /* Allocate N bytes of memory dynamically, with error checking. */
  36. void *
  37. xmalloc (size_t n)
  38. {
  39. void *p = malloc (n);
  40. if (!p && n != 0)
  41. xalloc_die ();
  42. return p;
  43. }
  44. /* Change the size of an allocated block of memory P to N bytes,
  45. with error checking. */
  46. void *
  47. xrealloc (void *p, size_t n)
  48. {
  49. p = realloc (p, n);
  50. if (!p && n != 0)
  51. xalloc_die ();
  52. return p;
  53. }
  54. /* If P is null, allocate a block of at least *PN bytes; otherwise,
  55. reallocate P so that it contains more than *PN bytes. *PN must be
  56. nonzero unless P is null. Set *PN to the new block's size, and
  57. return the pointer to the new block. *PN is never set to zero, and
  58. the returned pointer is never null. */
  59. void *
  60. x2realloc (void *p, size_t *pn)
  61. {
  62. return x2nrealloc (p, pn, 1);
  63. }
  64. /* Allocate S bytes of zeroed memory dynamically, with error checking.
  65. There's no need for xnzalloc (N, S), since it would be equivalent
  66. to xcalloc (N, S). */
  67. void *
  68. xzalloc (size_t s)
  69. {
  70. return memset (xmalloc (s), 0, s);
  71. }
  72. /* Allocate zeroed memory for N elements of S bytes, with error
  73. checking. S must be nonzero. */
  74. void *
  75. xcalloc (size_t n, size_t s)
  76. {
  77. void *p;
  78. /* Test for overflow, since some calloc implementations don't have
  79. proper overflow checks. But omit overflow and size-zero tests if
  80. HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
  81. returns NULL if successful. */
  82. if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
  83. || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
  84. xalloc_die ();
  85. return p;
  86. }
  87. /* Clone an object P of size S, with error checking. There's no need
  88. for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
  89. need for an arithmetic overflow check. */
  90. void *
  91. xmemdup (void const *p, size_t s)
  92. {
  93. return memcpy (xmalloc (s), p, s);
  94. }
  95. /* Clone STRING. */
  96. char *
  97. xstrdup (char const *string)
  98. {
  99. return xmemdup (string, strlen (string) + 1);
  100. }