Back to index

tetex-bin  3.0
xmalloc.c
Go to the documentation of this file.
00001 /* xmalloc.c -- safe versions of malloc and realloc.
00002 
00003    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 2004 Free Software
00004    Foundation, Inc.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2, or (at your option)
00009    any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019 
00020    Written by Brian Fox (bfox@ai.mit.edu). */
00021 
00022 #if !defined (ALREADY_HAVE_XMALLOC)
00023 #include "system.h"
00024 
00025 static void
00026 memory_error_and_abort (const char *fname)
00027 {
00028   fprintf (stderr, "%s: Out of virtual memory!\n", fname);
00029   abort ();
00030 }
00031 
00032 /* Return a pointer to free()able block of memory large enough
00033    to hold BYTES number of bytes.  If the memory cannot be allocated,
00034    print an error message and abort. */
00035 void *
00036 xmalloc (size_t bytes)
00037 {
00038   void *temp = malloc (bytes);
00039 
00040   if (!temp)
00041     memory_error_and_abort ("xmalloc");
00042   return (temp);
00043 }
00044 
00045 void *
00046 xrealloc (void *pointer, size_t bytes)
00047 {
00048   void *temp;
00049 
00050   if (!pointer)
00051     temp = malloc (bytes);
00052   else
00053     temp = realloc (pointer, bytes);
00054 
00055   if (!temp)
00056     memory_error_and_abort ("xrealloc");
00057 
00058   return (temp);
00059 }
00060 
00061 #endif /* !ALREADY_HAVE_XMALLOC */