xmalloc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* memory allocation routines with error checking.
  2. Copyright (C) 1989-2022 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 "environ.h"
  49. #include <stdio.h>
  50. #include <stddef.h>
  51. #if VMS
  52. #include <stdlib.h>
  53. #include <unixlib.h>
  54. #else
  55. /* For systems with larger pointers than ints, these must be declared. */
  56. # if HAVE_STDLIB_H && HAVE_UNISTD_H && HAVE_DECL_MALLOC \
  57. && HAVE_DECL_REALLOC && HAVE_DECL_CALLOC && HAVE_DECL_SBRK
  58. # include <stdlib.h>
  59. # include <unistd.h>
  60. # else
  61. # ifdef __cplusplus
  62. extern "C" {
  63. # endif /* __cplusplus */
  64. void *malloc (size_t);
  65. void *realloc (void *, size_t);
  66. void *calloc (size_t, size_t);
  67. #ifdef HAVE_SBRK
  68. void *sbrk (ptrdiff_t);
  69. #endif
  70. # ifdef __cplusplus
  71. }
  72. # endif /* __cplusplus */
  73. # endif /* HAVE_STDLIB_H ... */
  74. #endif /* VMS */
  75. /* The program name if set. */
  76. static const char *name = "";
  77. #ifdef HAVE_SBRK
  78. /* The initial sbrk, set when the program name is set. Not used for win32
  79. ports other than cygwin32. */
  80. static char *first_break = NULL;
  81. #endif /* HAVE_SBRK */
  82. void
  83. xmalloc_set_program_name (const char *s)
  84. {
  85. name = s;
  86. #ifdef HAVE_SBRK
  87. /* Win32 ports other than cygwin32 don't have brk() */
  88. if (first_break == NULL)
  89. first_break = (char *) sbrk (0);
  90. #endif /* HAVE_SBRK */
  91. }
  92. void
  93. xmalloc_failed (size_t size)
  94. {
  95. #ifdef HAVE_SBRK
  96. size_t allocated;
  97. if (first_break != NULL)
  98. allocated = (char *) sbrk (0) - first_break;
  99. else
  100. allocated = (char *) sbrk (0) - (char *) &environ;
  101. fprintf (stderr,
  102. "\n%s%sout of memory allocating %lu bytes after a total of %lu bytes\n",
  103. name, *name ? ": " : "",
  104. (unsigned long) size, (unsigned long) allocated);
  105. #else /* HAVE_SBRK */
  106. fprintf (stderr,
  107. "\n%s%sout of memory allocating %lu bytes\n",
  108. name, *name ? ": " : "",
  109. (unsigned long) size);
  110. #endif /* HAVE_SBRK */
  111. xexit (1);
  112. }
  113. PTR
  114. xmalloc (size_t size)
  115. {
  116. PTR newmem;
  117. if (size == 0)
  118. size = 1;
  119. newmem = malloc (size);
  120. if (!newmem)
  121. xmalloc_failed (size);
  122. return (newmem);
  123. }
  124. PTR
  125. xcalloc (size_t nelem, size_t elsize)
  126. {
  127. PTR newmem;
  128. if (nelem == 0 || elsize == 0)
  129. nelem = elsize = 1;
  130. newmem = calloc (nelem, elsize);
  131. if (!newmem)
  132. xmalloc_failed (nelem * elsize);
  133. return (newmem);
  134. }
  135. PTR
  136. xrealloc (PTR oldmem, size_t size)
  137. {
  138. PTR newmem;
  139. if (size == 0)
  140. size = 1;
  141. if (!oldmem)
  142. newmem = malloc (size);
  143. else
  144. newmem = realloc (oldmem, size);
  145. if (!newmem)
  146. xmalloc_failed (size);
  147. return (newmem);
  148. }