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#if STDC_HEADERS || THINK_C
19#include <stdlib.h>
20#else
21char *malloc ();
22char *realloc ();
23void free ();
24#endif
25
26#ifdef THINK_C
27#define error(x, y, z) perror(z) /* Throw out meaningless arguments */
28#else
29void error ();
30#endif
31
32/* Allocate N bytes of memory dynamically, with error checking.  */
33
34char *
35xmalloc (n)
36     unsigned n;
37{
38  char *p;
39
40  p = malloc (n);
41  if (p == 0)
42    /* Must exit with 2 for `cmp'.  */
43    error (2, 0, "virtual memory exhausted");
44  return p;
45}
46
47/* Change the size of an allocated block of memory P to N bytes,
48   with error checking.
49   If P is NULL, run xmalloc.
50   If N is 0, run free and return NULL.  */
51
52char *
53xrealloc (p, n)
54     char *p;
55     unsigned n;
56{
57  if (p == 0)
58    return xmalloc (n);
59  if (n == 0)
60    {
61      free (p);
62      return 0;
63    }
64  p = realloc (p, n);
65  if (p == 0)
66    /* Must exit with 2 for `cmp'.  */
67    error (2, 0, "virtual memory exhausted");
68  return p;
69}
70