home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / diff.lzh / diff / xmalloc.c < prev   
Encoding:
C/C++ Source or Header  |  1986-04-24  |  2.1 KB  |  90 lines

  1. static char RCSid[]="$Id: xmalloc.c_v 1.1 96/04/23 02:57:20 hiro Exp $";
  2. /* xmalloc.c -- malloc with out of memory checking
  3.    Copyright (C) 1990, 1991, 1993 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program 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
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #ifdef HAVE_CONFIG_H
  20. #if defined (CONFIG_BROKETS)
  21. /* We use <config.h> instead of "config.h" so that a compilation
  22.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  23.    (which it would do because it found this file in $srcdir).  */
  24. #include <config.h>
  25. #else
  26. #include "config.h"
  27. #endif
  28. #endif
  29.  
  30. #if __STDC__
  31. #define VOID void
  32. #else
  33. #define VOID char
  34. #endif
  35.  
  36. #include <sys/types.h>
  37.  
  38. #if STDC_HEADERS
  39. #include <stdlib.h>
  40. #else
  41. VOID *malloc ();
  42. VOID *realloc ();
  43. void free ();
  44. #endif
  45.  
  46. #if __STDC__ && defined (HAVE_VPRINTF)
  47. void error (int, int, char const *, ...);
  48. #else
  49. void error ();
  50. #endif
  51.  
  52. /* Allocate N bytes of memory dynamically, with error checking.  */
  53.  
  54. VOID *
  55. xmalloc (n)
  56.      size_t n;
  57. {
  58.   VOID *p;
  59.  
  60.   p = malloc (n);
  61.   if (p == 0)
  62.     /* Must exit with 2 for `cmp'.  */
  63.     error (2, 0, "memory exhausted");
  64.   return p;
  65. }
  66.  
  67. /* Change the size of an allocated block of memory P to N bytes,
  68.    with error checking.
  69.    If P is NULL, run xmalloc.
  70.    If N is 0, run free and return NULL.  */
  71.  
  72. VOID *
  73. xrealloc (p, n)
  74.      VOID *p;
  75.      size_t n;
  76. {
  77.   if (p == 0)
  78.     return xmalloc (n);
  79.   if (n == 0)
  80.     {
  81.       free (p);
  82.       return 0;
  83.     }
  84.   p = realloc (p, n);
  85.   if (p == 0)
  86.     /* Must exit with 2 for `cmp'.  */
  87.     error (2, 0, "memory exhausted");
  88.   return p;
  89. }
  90.