1*6b445a62SJohn Marino /* xmalloc.c -- safe versions of malloc and realloc */
2*6b445a62SJohn Marino
3*6b445a62SJohn Marino /* Copyright (C) 1991-2009 Free Software Foundation, Inc.
4*6b445a62SJohn Marino
5*6b445a62SJohn Marino This file is part of the GNU Readline Library (Readline), a library
6*6b445a62SJohn Marino for reading lines of text with interactive input and history editing.
7*6b445a62SJohn Marino
8*6b445a62SJohn Marino Readline is free software: you can redistribute it and/or modify
9*6b445a62SJohn Marino it under the terms of the GNU General Public License as published by
10*6b445a62SJohn Marino the Free Software Foundation, either version 3 of the License, or
11*6b445a62SJohn Marino (at your option) any later version.
12*6b445a62SJohn Marino
13*6b445a62SJohn Marino Readline is distributed in the hope that it will be useful,
14*6b445a62SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
15*6b445a62SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*6b445a62SJohn Marino GNU General Public License for more details.
17*6b445a62SJohn Marino
18*6b445a62SJohn Marino You should have received a copy of the GNU General Public License
19*6b445a62SJohn Marino along with Readline. If not, see <http://www.gnu.org/licenses/>.
20*6b445a62SJohn Marino */
21*6b445a62SJohn Marino
22*6b445a62SJohn Marino #define READLINE_LIBRARY
23*6b445a62SJohn Marino
24*6b445a62SJohn Marino #if defined (HAVE_CONFIG_H)
25*6b445a62SJohn Marino #include <config.h>
26*6b445a62SJohn Marino #endif
27*6b445a62SJohn Marino
28*6b445a62SJohn Marino #include <stdio.h>
29*6b445a62SJohn Marino
30*6b445a62SJohn Marino #if defined (HAVE_STDLIB_H)
31*6b445a62SJohn Marino # include <stdlib.h>
32*6b445a62SJohn Marino #else
33*6b445a62SJohn Marino # include "ansi_stdlib.h"
34*6b445a62SJohn Marino #endif /* HAVE_STDLIB_H */
35*6b445a62SJohn Marino
36*6b445a62SJohn Marino #include "xmalloc.h"
37*6b445a62SJohn Marino
38*6b445a62SJohn Marino /* **************************************************************** */
39*6b445a62SJohn Marino /* */
40*6b445a62SJohn Marino /* Memory Allocation and Deallocation. */
41*6b445a62SJohn Marino /* */
42*6b445a62SJohn Marino /* **************************************************************** */
43*6b445a62SJohn Marino
44*6b445a62SJohn Marino static void
memory_error_and_abort(fname)45*6b445a62SJohn Marino memory_error_and_abort (fname)
46*6b445a62SJohn Marino char *fname;
47*6b445a62SJohn Marino {
48*6b445a62SJohn Marino fprintf (stderr, "%s: out of virtual memory\n", fname);
49*6b445a62SJohn Marino exit (2);
50*6b445a62SJohn Marino }
51*6b445a62SJohn Marino
52*6b445a62SJohn Marino /* Return a pointer to free()able block of memory large enough
53*6b445a62SJohn Marino to hold BYTES number of bytes. If the memory cannot be allocated,
54*6b445a62SJohn Marino print an error message and abort. */
55*6b445a62SJohn Marino PTR_T
xmalloc(bytes)56*6b445a62SJohn Marino xmalloc (bytes)
57*6b445a62SJohn Marino size_t bytes;
58*6b445a62SJohn Marino {
59*6b445a62SJohn Marino PTR_T temp;
60*6b445a62SJohn Marino
61*6b445a62SJohn Marino temp = malloc (bytes);
62*6b445a62SJohn Marino if (temp == 0)
63*6b445a62SJohn Marino memory_error_and_abort ("xmalloc");
64*6b445a62SJohn Marino return (temp);
65*6b445a62SJohn Marino }
66*6b445a62SJohn Marino
67*6b445a62SJohn Marino PTR_T
xrealloc(pointer,bytes)68*6b445a62SJohn Marino xrealloc (pointer, bytes)
69*6b445a62SJohn Marino PTR_T pointer;
70*6b445a62SJohn Marino size_t bytes;
71*6b445a62SJohn Marino {
72*6b445a62SJohn Marino PTR_T temp;
73*6b445a62SJohn Marino
74*6b445a62SJohn Marino temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
75*6b445a62SJohn Marino
76*6b445a62SJohn Marino if (temp == 0)
77*6b445a62SJohn Marino memory_error_and_abort ("xrealloc");
78*6b445a62SJohn Marino return (temp);
79*6b445a62SJohn Marino }
80