diff options
Diffstat (limited to 'contrib/binutils/libiberty/xmalloc.c')
-rw-r--r-- | contrib/binutils/libiberty/xmalloc.c | 64 |
1 files changed, 50 insertions, 14 deletions
diff --git a/contrib/binutils/libiberty/xmalloc.c b/contrib/binutils/libiberty/xmalloc.c index b88105a..621c6d2 100644 --- a/contrib/binutils/libiberty/xmalloc.c +++ b/contrib/binutils/libiberty/xmalloc.c @@ -36,28 +36,29 @@ Boston, MA 02111-1307, USA. */ /* For systems with larger pointers than ints, these must be declared. */ PTR malloc PARAMS ((size_t)); PTR realloc PARAMS ((PTR, size_t)); +PTR calloc PARAMS ((size_t, size_t)); PTR sbrk PARAMS ((ptrdiff_t)); #endif /* The program name if set. */ static const char *name = ""; -#if ! defined (_WIN32) || defined (__CYGWIN32__) +#ifdef HAVE_SBRK /* The initial sbrk, set when the program name is set. Not used for win32 ports other than cygwin32. */ static char *first_break = NULL; -#endif +#endif /* HAVE_SBRK */ void xmalloc_set_program_name (s) const char *s; { name = s; -#if ! defined (_WIN32) || defined (__CYGWIN32__) +#ifdef HAVE_SBRK /* Win32 ports other than cygwin32 don't have brk() */ if (first_break == NULL) first_break = (char *) sbrk (0); -#endif /* ! _WIN32 || __CYGWIN32 __ */ +#endif /* HAVE_SBRK */ } PTR @@ -71,7 +72,7 @@ xmalloc (size) newmem = malloc (size); if (!newmem) { -#if ! defined (_WIN32) || defined (__CYGWIN32__) +#ifdef HAVE_SBRK extern char **environ; size_t allocated; @@ -80,15 +81,50 @@ xmalloc (size) else allocated = (char *) sbrk (0) - (char *) &environ; fprintf (stderr, - "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n", + "\n%s%sCannot allocate %lu bytes after allocating %lu bytes\n", name, *name ? ": " : "", (unsigned long) size, (unsigned long) allocated); -#else +#else /* HAVE_SBRK */ fprintf (stderr, - "\n%s%sCan not allocate %lu bytes\n", + "\n%s%sCannot allocate %lu bytes\n", name, *name ? ": " : "", (unsigned long) size); -#endif /* ! _WIN32 || __CYGWIN32 __ */ +#endif /* HAVE_SBRK */ + xexit (1); + } + return (newmem); +} + +PTR +xcalloc (nelem, elsize) + size_t nelem, elsize; +{ + PTR newmem; + + if (nelem == 0 || elsize == 0) + nelem = elsize = 1; + + newmem = calloc (nelem, elsize); + if (!newmem) + { +#ifdef HAVE_SBRK + extern char **environ; + size_t allocated; + + if (first_break != NULL) + allocated = (char *) sbrk (0) - first_break; + else + allocated = (char *) sbrk (0) - (char *) &environ; + fprintf (stderr, + "\n%s%sCannot allocate %lu bytes after allocating %lu bytes\n", + name, *name ? ": " : "", + (unsigned long) (nelem * elsize), (unsigned long) allocated); +#else /* HAVE_SBRK */ + fprintf (stderr, + "\n%s%sCannot allocate %lu bytes\n", + name, *name ? ": " : "", + (unsigned long) (nelem * elsize)); +#endif /* HAVE_SBRK */ xexit (1); } return (newmem); @@ -109,7 +145,7 @@ xrealloc (oldmem, size) newmem = realloc (oldmem, size); if (!newmem) { -#ifndef __MINGW32__ +#ifdef HAVE_SBRK extern char **environ; size_t allocated; @@ -118,15 +154,15 @@ xrealloc (oldmem, size) else allocated = (char *) sbrk (0) - (char *) &environ; fprintf (stderr, - "\n%s%sCan not reallocate %lu bytes after allocating %lu bytes\n", + "\n%s%sCannot reallocate %lu bytes after allocating %lu bytes\n", name, *name ? ": " : "", (unsigned long) size, (unsigned long) allocated); -#else +#else /* HAVE_SBRK */ fprintf (stderr, - "\n%s%sCan not reallocate %lu bytes\n", + "\n%s%sCannot reallocate %lu bytes\n", name, *name ? ": " : "", (unsigned long) size); -#endif /* __MINGW32__ */ +#endif /* HAVE_SBRK */ xexit (1); } return (newmem); |