xmalloc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* memory allocation routines with error checking.
  2. Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
  3. This file is part of the libiberty library.
  4. Libiberty is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. Libiberty is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with libiberty; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. /*
  17. @deftypefn Replacement void* xmalloc (size_t)
  18. Allocate memory without fail. If @code{malloc} fails, this will print
  19. a message to @code{stderr} (using the name set by
  20. @code{xmalloc_set_program_name},
  21. if any) and then call @code{xexit}. Note that it is therefore safe for
  22. a program to contain @code{#define malloc xmalloc} in its source.
  23. @end deftypefn
  24. @deftypefn Replacement void* xrealloc (void *@var{ptr}, size_t @var{size})
  25. Reallocate memory without fail. This routine functions like @code{realloc},
  26. but will behave the same as @code{xmalloc} if memory cannot be found.
  27. @end deftypefn
  28. @deftypefn Replacement void* xcalloc (size_t @var{nelem}, size_t @var{elsize})
  29. Allocate memory without fail, and set it to zero. This routine functions
  30. like @code{calloc}, but will behave the same as @code{xmalloc} if memory
  31. cannot be found.
  32. @end deftypefn
  33. @deftypefn Replacement void xmalloc_set_program_name (const char *@var{name})
  34. You can use this to set the name of the program used by
  35. @code{xmalloc_failed} when printing a failure message.
  36. @end deftypefn
  37. @deftypefn Replacement void xmalloc_failed (size_t)
  38. This function is not meant to be called by client code, and is listed
  39. here for completeness only. If any of the allocation routines fail, this
  40. function will be called to print an error message and terminate execution.
  41. @end deftypefn
  42. */
  43. #ifdef HAVE_CONFIG_H
  44. #include "config.h"
  45. #endif
  46. #include "ansidecl.h"
  47. #include "libiberty.h"
  48. #include <stdio.h>
  49. #include <stddef.h>
  50. #if VMS
  51. #include <stdlib.h>
  52. #include <unixlib.h>
  53. #else
  54. /* For systems with larger pointers than ints, these must be declared. */
  55. # if HAVE_STDLIB_H && HAVE_UNISTD_H && HAVE_DECL_MALLOC \
  56. && HAVE_DECL_REALLOC && HAVE_DECL_CALLOC && HAVE_DECL_SBRK
  57. # include <stdlib.h>
  58. # include <unistd.h>
  59. # else
  60. # ifdef __cplusplus
  61. extern "C" {
  62. # endif /* __cplusplus */
  63. void *malloc (size_t);
  64. void *realloc (void *, size_t);
  65. void *calloc (size_t, size_t);
  66. void *sbrk (ptrdiff_t);
  67. # ifdef __cplusplus
  68. }
  69. # endif /* __cplusplus */
  70. # endif /* HAVE_STDLIB_H ... */
  71. #endif /* VMS */
  72. /* The program name if set. */
  73. static const char *name = "";
  74. #ifdef HAVE_SBRK
  75. /* The initial sbrk, set when the program name is set. Not used for win32
  76. ports other than cygwin32. */
  77. static char *first_break = NULL;
  78. #endif /* HAVE_SBRK */
  79. void
  80. xmalloc_set_program_name (const char *s)
  81. {
  82. name = s;
  83. #ifdef HAVE_SBRK
  84. /* Win32 ports other than cygwin32 don't have brk() */
  85. if (first_break == NULL)
  86. first_break = (char *) sbrk (0);
  87. #endif /* HAVE_SBRK */
  88. }
  89. void
  90. xmalloc_failed (size_t size)
  91. {
  92. #ifdef HAVE_SBRK
  93. extern char **environ;
  94. size_t allocated;
  95. if (first_break != NULL)
  96. allocated = (char *) sbrk (0) - first_break;
  97. else
  98. allocated = (char *) sbrk (0) - (char *) &environ;
  99. fprintf (stderr,
  100. "\n%s%sout of memory allocating %lu bytes after a total of %lu bytes\n",
  101. name, *name ? ": " : "",
  102. (unsigned long) size, (unsigned long) allocated);
  103. #else /* HAVE_SBRK */
  104. fprintf (stderr,
  105. "\n%s%sout of memory allocating %lu bytes\n",
  106. name, *name ? ": " : "",
  107. (unsigned long) size);
  108. #endif /* HAVE_SBRK */
  109. xexit (1);
  110. }
  111. PTR
  112. xmalloc (size_t size)
  113. {
  114. PTR newmem;
  115. if (size == 0)
  116. size = 1;
  117. newmem = malloc (size);
  118. if (!newmem)
  119. xmalloc_failed (size);
  120. return (newmem);
  121. }
  122. PTR
  123. xcalloc (size_t nelem, size_t elsize)
  124. {
  125. PTR newmem;
  126. if (nelem == 0 || elsize == 0)
  127. nelem = elsize = 1;
  128. newmem = calloc (nelem, elsize);
  129. if (!newmem)
  130. xmalloc_failed (nelem * elsize);
  131. return (newmem);
  132. }
  133. PTR
  134. xrealloc (PTR oldmem, size_t size)
  135. {
  136. PTR newmem;
  137. if (size == 0)
  138. size = 1;
  139. if (!oldmem)
  140. newmem = malloc (size);
  141. else
  142. newmem = realloc (oldmem, size);
  143. if (!newmem)
  144. xmalloc_failed (size);
  145. return (newmem);
  146. }