home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / tcpip / netkit-a.06 / netkit-a / NetKit-A-0.06 / nfs-server-2.0 / xmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-13  |  1.5 KB  |  68 lines

  1. /* xmalloc.c -- malloc with out of memory checking
  2.    Copyright (C) 1990, 1991 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. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21.  
  22. #ifdef STDC_HEADERS
  23. #include <stdlib.h>
  24. #else
  25. char *malloc ();
  26. char *realloc ();
  27. void free ();
  28. #endif
  29.  
  30. void mallocfailed ();
  31.  
  32. /* Allocate N bytes of memory dynamically, with error checking.  */
  33.  
  34. char *
  35. xmalloc (n)
  36.      unsigned n;
  37. {
  38.   char *p;
  39.  
  40.   p = malloc (n);
  41.   if (p == 0)
  42.     mallocfailed();
  43.   return p;
  44. }
  45.  
  46. /* Change the size of an allocated block of memory P to N bytes,
  47.    with error checking.
  48.    If P is NULL, run xmalloc.
  49.    If N is 0, run free and return NULL.  */
  50.  
  51. char *
  52. xrealloc (p, n)
  53.      char *p;
  54.      unsigned n;
  55. {
  56.   if (p == 0)
  57.     return xmalloc (n);
  58.   if (n == 0)
  59.     {
  60.       free (p);
  61.       return 0;
  62.     }
  63.   p = realloc (p, n);
  64.   if (p == 0)
  65.     mallocfailed();
  66.   return p;
  67. }
  68.