home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / useful / dist / gnu / diffutils / diffutils-2.4-amiga / xmalloc.c < prev   
Encoding:
C/C++ Source or Header  |  1993-07-18  |  1.8 KB  |  81 lines

  1. /* xmalloc.c -- malloc with out of memory checking
  2.    Copyright (C) 1990, 1991, 1993 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #if HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21.  
  22. #if __STDC__
  23. #define VOID void
  24. #else
  25. #define VOID char
  26. #endif
  27.  
  28. #if STDC_HEADERS
  29. #include <stdlib.h>
  30. #else
  31. #include <sys/types.h>
  32. VOID *malloc ();
  33. VOID *realloc ();
  34. void free ();
  35. #endif
  36.  
  37. #if __STDC__ && defined (HAVE_VPRINTF)
  38. void error (int, int, char const *, ...);
  39. #else
  40. void error ();
  41. #endif
  42.  
  43. /* Allocate N bytes of memory dynamically, with error checking.  */
  44.  
  45. VOID *
  46. xmalloc (n)
  47.      size_t n;
  48. {
  49.   VOID *p;
  50.  
  51.   p = malloc (n);
  52.   if (p == 0)
  53.     /* Must exit with 2 for `cmp'.  */
  54.     error (2, 0, "virtual memory exhausted");
  55.   return p;
  56. }
  57.  
  58. /* Change the size of an allocated block of memory P to N bytes,
  59.    with error checking.
  60.    If P is NULL, run xmalloc.
  61.    If N is 0, run free and return NULL.  */
  62.  
  63. VOID *
  64. xrealloc (p, n)
  65.      VOID *p;
  66.      size_t n;
  67. {
  68.   if (p == 0)
  69.     return xmalloc (n);
  70.   if (n == 0)
  71.     {
  72.       free (p);
  73.       return 0;
  74.     }
  75.   p = realloc (p, n);
  76.   if (p == 0)
  77.     /* Must exit with 2 for `cmp'.  */
  78.     error (2, 0, "virtual memory exhausted");
  79.   return p;
  80. }
  81.