xmalloc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* xmalloc.c -- malloc with out of memory checking
  2. Copyright (C) 1990-1999, 2000, 2002 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. #include "gettext.h"
  27. #define _(msgid) gettext (msgid)
  28. #define N_(msgid) msgid
  29. #include "error.h"
  30. #include "xalloc.h"
  31. #ifndef EXIT_FAILURE
  32. # define EXIT_FAILURE 1
  33. #endif
  34. /* The following tests require AC_PREREQ(2.54). */
  35. #ifndef HAVE_MALLOC
  36. "you must run the autoconf test for a GNU libc compatible malloc"
  37. #endif
  38. #ifndef HAVE_REALLOC
  39. "you must run the autoconf test for a GNU libc compatible realloc"
  40. #endif
  41. /* Exit value when the requested amount of memory is not available.
  42. The caller may set it to some other value. */
  43. int xalloc_exit_failure = EXIT_FAILURE;
  44. /* If non NULL, call this function when memory is exhausted. */
  45. void (*xalloc_fail_func) PARAMS ((void)) = 0;
  46. /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
  47. before exiting when memory is exhausted. Goes through gettext. */
  48. char const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
  49. void
  50. xalloc_die (void)
  51. {
  52. if (xalloc_fail_func)
  53. (*xalloc_fail_func) ();
  54. error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
  55. /* The `noreturn' cannot be given to error, since it may return if
  56. its first argument is 0. To help compilers understand the
  57. xalloc_die does terminate, call exit. */
  58. exit (EXIT_FAILURE);
  59. }
  60. /* Allocate N bytes of memory dynamically, with error checking. */
  61. void *
  62. xmalloc (size_t n)
  63. {
  64. void *p;
  65. p = malloc (n);
  66. if (p == 0)
  67. xalloc_die ();
  68. return p;
  69. }
  70. /* Change the size of an allocated block of memory P to N bytes,
  71. with error checking. */
  72. void *
  73. xrealloc (void *p, size_t n)
  74. {
  75. p = realloc (p, n);
  76. if (p == 0)
  77. xalloc_die ();
  78. return p;
  79. }
  80. /* Allocate memory for N elements of S bytes, with error checking. */
  81. void *
  82. xcalloc (size_t n, size_t s)
  83. {
  84. void *p;
  85. p = calloc (n, s);
  86. if (p == 0)
  87. xalloc_die ();
  88. return p;
  89. }