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