home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / libiberty / xmalloc.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  122 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., 59 Temple Place - Suite 330,
  18. Boston, MA 02111-1307, 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. #define ptrdiff_t long
  30. #endif
  31.  
  32. #if VMS
  33. #include <stdlib.h>
  34. #include <unixlib.h>
  35. #else
  36. /* For systems with larger pointers than ints, these must be declared.  */
  37. PTR malloc PARAMS ((size_t));
  38. PTR realloc PARAMS ((PTR, size_t));
  39. PTR sbrk PARAMS ((ptrdiff_t));
  40. #endif
  41.  
  42. /* The program name if set.  */
  43. static const char *name = "";
  44.  
  45. /* The initial sbrk, set when the program name is set.  */
  46. static char *first_break = NULL;
  47.  
  48. void
  49. xmalloc_set_program_name (s)
  50.      const char *s;
  51. {
  52. #ifdef HAVE_SBRK
  53.   name = s;
  54.   if (first_break == NULL)
  55.     first_break = (char *) sbrk (0);
  56. #else
  57.   return;
  58. #endif
  59. }
  60.  
  61. PTR
  62. xmalloc (size)
  63.     size_t size;
  64. {
  65.   PTR newmem;
  66.  
  67.   if (size == 0)
  68.     size = 1;
  69.   newmem = malloc (size);
  70.   if (!newmem)
  71.     {
  72. #ifdef HAVE_SBRK
  73.       extern char **environ;
  74.       size_t allocated;
  75.  
  76.       if (first_break != NULL)
  77.     allocated = (char *) sbrk (0) - first_break;
  78.       else
  79.     allocated = (char *) sbrk (0) - (char *) &environ;
  80.       fprintf (stderr,
  81.            "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
  82.            name, *name ? ": " : "",
  83.            (unsigned long) size, (unsigned long) allocated);
  84. #endif
  85.       xexit (1);
  86.     }
  87.   return (newmem);
  88. }
  89.  
  90. PTR
  91. xrealloc (oldmem, size)
  92.     PTR oldmem;
  93.     size_t size;
  94. {
  95.   PTR newmem;
  96.  
  97.   if (size == 0)
  98.     size = 1;
  99.   if (!oldmem)
  100.     newmem = malloc (size);
  101.   else
  102.     newmem = realloc (oldmem, size);
  103.   if (!newmem)
  104.     {
  105. #ifdef HAVE_SBRK
  106.       extern char **environ;
  107.       size_t allocated;
  108.  
  109.       if (first_break != NULL)
  110.     allocated = (char *) sbrk (0) - first_break;
  111.       else
  112.     allocated = (char *) sbrk (0) - (char *) &environ;
  113.       fprintf (stderr,
  114.            "\n%s%sCan not reallocate %lu bytes after allocating %lu bytes\n",
  115.            name, *name ? ": " : "",
  116.            (unsigned long) size, (unsigned long) allocated);
  117. #endif
  118.       xexit (1);
  119.     }
  120.   return (newmem);
  121. }
  122.