home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / amiga / binutils-2.5.2-src.lha / binutils-2.5.2 / libiberty / xmalloc.c < prev   
Encoding:
C/C++ Source or Header  |  1994-11-10  |  2.7 KB  |  119 lines

  1. /* memory allocation routines with error checking.
  2.    Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
  3.    
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. Libiberty is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with libiberty; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.  */
  19.  
  20. #include "ansidecl.h"
  21. #include "libiberty.h"
  22.  
  23. #include <stdio.h>
  24.  
  25. #ifdef __STDC__
  26. #include <stddef.h>
  27. #else
  28. #define size_t unsigned long
  29. #endif
  30.  
  31. #ifndef __amigados__
  32. #define HAVE_SBRK
  33. #endif
  34.  
  35. /* For systems with larger pointers than ints, these must be declared.  */
  36. PTR malloc PARAMS ((size_t));
  37. PTR realloc PARAMS ((PTR, size_t));
  38.  
  39. /* The program name if set.  */
  40. static const char *name = "";
  41.  
  42. /* The initial sbrk, set when the program name is set.  */
  43. static char *first_break = NULL;
  44.  
  45. void
  46. xmalloc_set_program_name (s)
  47.      const char *s;
  48. {
  49. #ifdef HAVE_SBRK
  50.   name = s;
  51.   if (first_break == NULL)
  52.     first_break = (char *) sbrk (0);
  53. #else
  54.   return;
  55. #endif
  56. }
  57.  
  58. PTR
  59. xmalloc (size)
  60.     size_t size;
  61. {
  62.   PTR newmem;
  63.  
  64.   if (size == 0)
  65.     size = 1;
  66.   newmem = malloc (size);
  67.   if (!newmem)
  68.     {
  69. #ifdef HAVE_SBRK
  70.       extern char **environ;
  71.       size_t allocated;
  72.  
  73.       if (first_break != NULL)
  74.     allocated = (char *) sbrk (0) - first_break;
  75.       else
  76.     allocated = (char *) sbrk (0) - (char *) &environ;
  77.       fprintf (stderr,
  78.            "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
  79.            name, *name ? ": " : "",
  80.            (unsigned long) size, (unsigned long) allocated);
  81. #endif
  82.       xexit (1);
  83.     }
  84.   return (newmem);
  85. }
  86.  
  87. PTR
  88. xrealloc (oldmem, size)
  89.     PTR oldmem;
  90.     size_t size;
  91. {
  92.   PTR newmem;
  93.  
  94.   if (size == 0)
  95.     size = 1;
  96.   if (!oldmem)
  97.     newmem = malloc (size);
  98.   else
  99.     newmem = realloc (oldmem, size);
  100.   if (!newmem)
  101.     {
  102. #ifdef HAVE_SBRK
  103.       extern char **environ;
  104.       size_t allocated;
  105.  
  106.       if (first_break != NULL)
  107.     allocated = (char *) sbrk (0) - first_break;
  108.       else
  109.     allocated = (char *) sbrk (0) - (char *) &environ;
  110.       fprintf (stderr,
  111.            "\n%s%sCan not reallocate %lu bytes after allocating %lu bytes\n",
  112.            name, *name ? ": " : "",
  113.            (unsigned long) size, (unsigned long) allocated);
  114. #endif
  115.       xexit (1);
  116.     }
  117.   return (newmem);
  118. }
  119.