1146515Sru/* xmalloc.c -- safe versions of malloc and realloc.
242660Smarkm
3146515Sru   Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 2004 Free Software
442660Smarkm   Foundation, Inc.
542660Smarkm
642660Smarkm   This program is free software; you can redistribute it and/or modify
742660Smarkm   it under the terms of the GNU General Public License as published by
842660Smarkm   the Free Software Foundation; either version 2, or (at your option)
942660Smarkm   any later version.
1042660Smarkm
1142660Smarkm   This program is distributed in the hope that it will be useful,
1242660Smarkm   but WITHOUT ANY WARRANTY; without even the implied warranty of
1342660Smarkm   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1442660Smarkm   GNU General Public License for more details.
1542660Smarkm
1642660Smarkm   You should have received a copy of the GNU General Public License
1742660Smarkm   along with this program; if not, write to the Free Software
1842660Smarkm   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1942660Smarkm
2042660Smarkm   Written by Brian Fox (bfox@ai.mit.edu). */
2142660Smarkm
2242660Smarkm#if !defined (ALREADY_HAVE_XMALLOC)
23146515Sru#include "system.h"
2442660Smarkm
25146515Srustatic void
26146515Srumemory_error_and_abort (const char *fname)
27146515Sru{
28146515Sru  fprintf (stderr, "%s: Out of virtual memory!\n", fname);
29146515Sru  abort ();
30146515Sru}
3142660Smarkm
3242660Smarkm/* Return a pointer to free()able block of memory large enough
3342660Smarkm   to hold BYTES number of bytes.  If the memory cannot be allocated,
3442660Smarkm   print an error message and abort. */
3542660Smarkmvoid *
36146515Sruxmalloc (size_t bytes)
3742660Smarkm{
3842660Smarkm  void *temp = malloc (bytes);
3942660Smarkm
4042660Smarkm  if (!temp)
4142660Smarkm    memory_error_and_abort ("xmalloc");
4242660Smarkm  return (temp);
4342660Smarkm}
4442660Smarkm
4542660Smarkmvoid *
46146515Sruxrealloc (void *pointer, size_t bytes)
4742660Smarkm{
4842660Smarkm  void *temp;
4942660Smarkm
5042660Smarkm  if (!pointer)
5142660Smarkm    temp = malloc (bytes);
5242660Smarkm  else
5342660Smarkm    temp = realloc (pointer, bytes);
5442660Smarkm
5542660Smarkm  if (!temp)
5642660Smarkm    memory_error_and_abort ("xrealloc");
5742660Smarkm
5842660Smarkm  return (temp);
5942660Smarkm}
6042660Smarkm
6142660Smarkm#endif /* !ALREADY_HAVE_XMALLOC */
62