/[gnats]/gnats/libiberty/xmalloc.c
ViewVC logotype

Diff of /gnats/libiberty/xmalloc.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by pdm, Mon Dec 10 23:03:26 2001 UTC revision 1.4 by chewie, Sat Nov 13 05:14:17 2004 UTC
# Line 17  License along with libiberty; see the fi Line 17  License along with libiberty; see the fi
17  not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,  not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  Boston, MA 02111-1307, USA.  */  Boston, MA 02111-1307, USA.  */
19    
20    /*
21    
22    @deftypefn Replacement void* xmalloc (size_t)
23    
24    Allocate memory without fail.  If @code{malloc} fails, this will print
25    a message to @code{stderr} (using the name set by
26    @code{xmalloc_set_program_name},
27    if any) and then call @code{xexit}.  Note that it is therefore safe for
28    a program to contain @code{#define malloc xmalloc} in its source.
29    
30    @end deftypefn
31    
32    @deftypefn Replacement void* xrealloc (void *@var{ptr}, size_t @var{size})
33    Reallocate memory without fail.  This routine functions like @code{realloc},
34    but will behave the same as @code{xmalloc} if memory cannot be found.
35    
36    @end deftypefn
37    
38    @deftypefn Replacement void* xcalloc (size_t @var{nelem}, size_t @var{elsize})
39    
40    Allocate memory without fail, and set it to zero.  This routine functions
41    like @code{calloc}, but will behave the same as @code{xmalloc} if memory
42    cannot be found.
43    
44    @end deftypefn
45    
46    @deftypefn Replacement void xmalloc_set_program_name (const char *@var{name})
47    
48    You can use this to set the name of the program used by
49    @code{xmalloc_failed} when printing a failure message.
50    
51    @end deftypefn
52    
53    @deftypefn Replacement void xmalloc_failed (size_t)
54    
55    This function is not meant to be called by client code, and is listed
56    here for completeness only.  If any of the allocation routines fail, this
57    function will be called to print an error message and terminate execution.
58    
59    @end deftypefn
60    
61    */
62    
63  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
64  #include "config.h"  #include "config.h"
65  #endif  #endif
# Line 25  Boston, MA 02111-1307, USA.  */ Line 68  Boston, MA 02111-1307, USA.  */
68    
69  #include <stdio.h>  #include <stdio.h>
70    
71  #ifdef __STDC__  #ifdef ANSI_PROTOTYPES
72  #include <stddef.h>  #include <stddef.h>
73  #else  #else
74  #define size_t unsigned long  #define size_t unsigned long
# Line 77  xmalloc_failed (size) Line 120  xmalloc_failed (size)
120    else    else
121      allocated = (char *) sbrk (0) - (char *) &environ;      allocated = (char *) sbrk (0) - (char *) &environ;
122    fprintf (stderr,    fprintf (stderr,
123             "\n%s%sCannot allocate %lu bytes after allocating %lu bytes\n",             "\n%s%sout of memory allocating %lu bytes after a total of %lu bytes\n",
124             name, *name ? ": " : "",             name, *name ? ": " : "",
125             (unsigned long) size, (unsigned long) allocated);             (unsigned long) size, (unsigned long) allocated);
126  #else /* HAVE_SBRK */  #else /* HAVE_SBRK */
127    fprintf (stderr,    fprintf (stderr,
128             "\n%s%sCannot allocate %lu bytes\n",             "\n%s%sout of memory allocating %lu bytes\n",
129             name, *name ? ": " : "",             name, *name ? ": " : "",
130             (unsigned long) size);             (unsigned long) size);
131  #endif /* HAVE_SBRK */  #endif /* HAVE_SBRK */

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26