xmalloc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* xmalloc.c -- malloc with out of memory checking
  2. Copyright (C) 1990-1999, 2000 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 2, or (at your option)
  6. 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, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #if HAVE_CONFIG_H
  15. # include <config.h>
  16. #endif
  17. #include <sys/types.h>
  18. #if STDC_HEADERS
  19. # include <stdlib.h>
  20. #else
  21. void *calloc ();
  22. void *malloc ();
  23. void *realloc ();
  24. void free ();
  25. #endif
  26. #if ENABLE_NLS
  27. # include <libintl.h>
  28. # define _(Text) gettext (Text)
  29. #else
  30. # define textdomain(Domain)
  31. # define _(Text) Text
  32. #endif
  33. #define N_(Text) Text
  34. #include "error.h"
  35. #include "xalloc.h"
  36. #ifndef EXIT_FAILURE
  37. # define EXIT_FAILURE 1
  38. #endif
  39. #ifndef HAVE_DONE_WORKING_MALLOC_CHECK
  40. "you must run the autoconf test for a properly working malloc -- see malloc.m4"
  41. #endif
  42. #ifndef HAVE_DONE_WORKING_REALLOC_CHECK
  43. "you must run the autoconf test for a properly working realloc --see realloc.m4"
  44. #endif
  45. /* Exit value when the requested amount of memory is not available.
  46. The caller may set it to some other value. */
  47. int xalloc_exit_failure = EXIT_FAILURE;
  48. /* If non NULL, call this function when memory is exhausted. */
  49. void (*xalloc_fail_func) PARAMS ((void)) = 0;
  50. /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
  51. before exiting when memory is exhausted. Goes through gettext. */
  52. char const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
  53. void
  54. xalloc_die (void)
  55. {
  56. if (xalloc_fail_func)
  57. (*xalloc_fail_func) ();
  58. error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
  59. /* The `noreturn' cannot be given to error, since it may return if
  60. its first argument is 0. To help compilers understand the
  61. xalloc_die does terminate, call exit. */
  62. exit (EXIT_FAILURE);
  63. }
  64. /* Allocate N bytes of memory dynamically, with error checking. */
  65. void *
  66. xmalloc (size_t n)
  67. {
  68. void *p;
  69. p = malloc (n);
  70. if (p == 0)
  71. xalloc_die ();
  72. return p;
  73. }
  74. /* Change the size of an allocated block of memory P to N bytes,
  75. with error checking. */
  76. void *
  77. xrealloc (void *p, size_t n)
  78. {
  79. p = realloc (p, n);
  80. if (p == 0)
  81. xalloc_die ();
  82. return p;
  83. }
  84. /* Allocate memory for N elements of S bytes, with error checking. */
  85. void *
  86. xcalloc (size_t n, size_t s)
  87. {
  88. void *p;
  89. p = calloc (n, s);
  90. if (p == 0)
  91. xalloc_die ();
  92. return p;
  93. }