xmalloc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* xmalloc.c -- malloc with out of memory checking
  2. Copyright (C) 1990-1996, 2000-2003, 2005-2007 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. /* Specification. */
  15. #include "xalloc.h"
  16. #include <stdlib.h>
  17. #include "error.h"
  18. #include "gettext.h"
  19. #define _(str) gettext (str)
  20. /* Exit value when the requested amount of memory is not available.
  21. The caller may set it to some other value. */
  22. int xmalloc_exit_failure = EXIT_FAILURE;
  23. void
  24. xalloc_die ()
  25. {
  26. error (xmalloc_exit_failure, 0, _("memory exhausted"));
  27. /* The `noreturn' cannot be given to error, since it may return if
  28. its first argument is 0. To help compilers understand the
  29. xalloc_die does terminate, call exit. */
  30. exit (EXIT_FAILURE);
  31. }
  32. static void *
  33. fixup_null_alloc (size_t n)
  34. {
  35. void *p;
  36. p = NULL;
  37. if (n == 0)
  38. p = malloc ((size_t) 1);
  39. if (p == NULL)
  40. xalloc_die ();
  41. return p;
  42. }
  43. /* Allocate N bytes of memory dynamically, with error checking. */
  44. void *
  45. xmalloc (size_t n)
  46. {
  47. void *p;
  48. p = malloc (n);
  49. if (p == NULL)
  50. p = fixup_null_alloc (n);
  51. return p;
  52. }
  53. /* Allocate memory for NMEMB elements of SIZE bytes, with error checking.
  54. SIZE must be > 0. */
  55. void *
  56. xnmalloc (size_t nmemb, size_t size)
  57. {
  58. size_t n;
  59. void *p;
  60. if (xalloc_oversized (nmemb, size))
  61. xalloc_die ();
  62. n = nmemb * size;
  63. p = malloc (n);
  64. if (p == NULL)
  65. p = fixup_null_alloc (n);
  66. return p;
  67. }
  68. /* Allocate SIZE bytes of memory dynamically, with error checking,
  69. and zero it. */
  70. void *
  71. xzalloc (size_t size)
  72. {
  73. void *p;
  74. p = xmalloc (size);
  75. memset (p, 0, size);
  76. return p;
  77. }
  78. /* Allocate memory for N elements of S bytes, with error checking,
  79. and zero it. */
  80. void *
  81. xcalloc (size_t n, size_t s)
  82. {
  83. void *p;
  84. p = calloc (n, s);
  85. if (p == NULL)
  86. p = fixup_null_alloc (n);
  87. return p;
  88. }
  89. /* Change the size of an allocated block of memory P to N bytes,
  90. with error checking.
  91. If P is NULL, run xmalloc. */
  92. void *
  93. xrealloc (void *p, size_t n)
  94. {
  95. if (p == NULL)
  96. return xmalloc (n);
  97. p = realloc (p, n);
  98. if (p == NULL)
  99. p = fixup_null_alloc (n);
  100. return p;
  101. }