home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / gnuinfo / Source / c / xmalloc < prev   
Encoding:
Text File  |  1994-10-01  |  2.2 KB  |  80 lines

  1. #include "defines.h"
  2. /* xmalloc.c -- safe versions of malloc and realloc */
  3.  
  4. /* This file is part of GNU Info, a program for reading online documentation
  5.    stored in Info format.
  6.  
  7.    This file has appeared in prior works by the Free Software Foundation;
  8.    thus it carries copyright dates from 1988 through 1993.
  9.  
  10.    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software
  11.    Foundation, Inc.
  12.  
  13.    This program is free software; you can redistribute it and/or modify
  14.    it under the terms of the GNU General Public License as published by
  15.    the Free Software Foundation; either version 2, or (at your option)
  16.    any later version.
  17.  
  18.    This program is distributed in the hope that it will be useful,
  19.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.    GNU General Public License for more details.
  22.  
  23.    You should have received a copy of the GNU General Public License
  24.    along with this program; if not, write to the Free Software
  25.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27.    Written by Brian Fox (bfox@ai.mit.edu). */
  28.  
  29. #if !defined (ALREADY_HAVE_XMALLOC)
  30. #include <stdio.h>
  31.  
  32. static void memory_error_and_abort ();
  33.  
  34. /* **************************************************************** */
  35. /*                                    */
  36. /*           Memory Allocation and Deallocation.            */
  37. /*                                    */
  38. /* **************************************************************** */
  39.  
  40. /* Return a pointer to free()able block of memory large enough
  41.    to hold BYTES number of bytes.  If the memory cannot be allocated,
  42.    print an error message and abort. */
  43. void *
  44. xmalloc (bytes)
  45.      int bytes;
  46. {
  47.   void *temp = (void *)malloc (bytes);
  48.  
  49.   if (!temp)
  50.     memory_error_and_abort ("xmalloc");
  51.   return (temp);
  52. }
  53.  
  54. void *
  55. xrealloc (pointer, bytes)
  56.      void *pointer;
  57.      int bytes;
  58. {
  59.   void *temp;
  60.  
  61.   if (!pointer)
  62.     temp = (void *)malloc (bytes);
  63.   else
  64.     temp = (void *)realloc (pointer, bytes);
  65.  
  66.   if (!temp)
  67.     memory_error_and_abort ("xrealloc");
  68.  
  69.   return (temp);
  70. }
  71.  
  72. static void
  73. memory_error_and_abort (fname)
  74.      char *fname;
  75. {
  76.   fprintf (stderr, "%s: Out of virtual memory!\n", fname);
  77.   abort ();
  78. }
  79. #endif /* !ALREADY_HAVE_XMALLOC */
  80.