home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libiberty / xmalloc.c < prev   
C/C++ Source or Header  |  1994-02-16  |  2KB  |  86 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. /* 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. void
  39. xmalloc_set_program_name (s)
  40.      const char *s;
  41. {
  42.   name = s;
  43. }
  44.  
  45. PTR
  46. xmalloc (size)
  47.     size_t size;
  48. {
  49.   PTR newmem;
  50.  
  51.   if (size == 0)
  52.     size = 1;
  53.   newmem = malloc (size);
  54.   if (!newmem)
  55.     {
  56.       fprintf (stderr, "\n%s%sCan not allocate %lu bytes\n",
  57.            name, *name ? ": " : "",
  58.            (unsigned long) size);
  59.       xexit (1);
  60.     }
  61.   return (newmem);
  62. }
  63.  
  64. PTR
  65. xrealloc (oldmem, size)
  66.     PTR oldmem;
  67.     size_t size;
  68. {
  69.   PTR newmem;
  70.  
  71.   if (size == 0)
  72.     size = 1;
  73.   if (!oldmem)
  74.     newmem = malloc (size);
  75.   else
  76.     newmem = realloc (oldmem, size);
  77.   if (!newmem)
  78.     {
  79.       fprintf (stderr, "\n%s%sCan not reallocate %lu bytes\n",
  80.            name, *name ? ": " : "",
  81.            (unsigned long) size);
  82.       xexit (1);
  83.     }
  84.   return (newmem);
  85. }
  86.