blob: c77d7634d6a062cfe12bee494b35fbccc1653c85 [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001/* xmalloc.c -- safe versions of malloc and realloc */
2
Jari Aalto31859422009-01-12 13:36:28 +00003/* Copyright (C) 1991-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
Jari Aalto31859422009-01-12 13:36:28 +00005 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
Jari Aalto726f6381996-08-26 18:22:31 +00007
Jari Aalto31859422009-01-12 13:36:28 +00008 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000012
Jari Aalto31859422009-01-12 13:36:28 +000013 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000017
18 You should have received a copy of the GNU General Public License
Jari Aalto31859422009-01-12 13:36:28 +000019 along with Readline. If not, see <http://www.gnu.org/licenses/>.
20*/
21
Jari Aaltobb706242000-03-17 21:46:59 +000022#define READLINE_LIBRARY
Jari Aalto726f6381996-08-26 18:22:31 +000023
Jari Aaltoccc6cda1996-12-23 17:02:34 +000024#if defined (HAVE_CONFIG_H)
25#include <config.h>
26#endif
27
Jari Aalto726f6381996-08-26 18:22:31 +000028#include <stdio.h>
29
30#if defined (HAVE_STDLIB_H)
31# include <stdlib.h>
32#else
33# include "ansi_stdlib.h"
34#endif /* HAVE_STDLIB_H */
35
Jari Aaltobb706242000-03-17 21:46:59 +000036#include "xmalloc.h"
Jari Aalto726f6381996-08-26 18:22:31 +000037
38/* **************************************************************** */
39/* */
40/* Memory Allocation and Deallocation. */
41/* */
42/* **************************************************************** */
43
Jari Aaltobb706242000-03-17 21:46:59 +000044static void
45memory_error_and_abort (fname)
46 char *fname;
47{
48 fprintf (stderr, "%s: out of virtual memory\n", fname);
49 exit (2);
50}
51
Jari Aalto726f6381996-08-26 18:22:31 +000052/* Return a pointer to free()able block of memory large enough
53 to hold BYTES number of bytes. If the memory cannot be allocated,
54 print an error message and abort. */
Jari Aaltof73dda02001-11-13 17:56:06 +000055PTR_T
Jari Aalto726f6381996-08-26 18:22:31 +000056xmalloc (bytes)
Jari Aaltof73dda02001-11-13 17:56:06 +000057 size_t bytes;
Jari Aalto726f6381996-08-26 18:22:31 +000058{
Jari Aaltof73dda02001-11-13 17:56:06 +000059 PTR_T temp;
Jari Aalto726f6381996-08-26 18:22:31 +000060
Jari Aaltof73dda02001-11-13 17:56:06 +000061 temp = malloc (bytes);
Jari Aaltoccc6cda1996-12-23 17:02:34 +000062 if (temp == 0)
Jari Aalto726f6381996-08-26 18:22:31 +000063 memory_error_and_abort ("xmalloc");
64 return (temp);
65}
66
Jari Aaltof73dda02001-11-13 17:56:06 +000067PTR_T
Jari Aalto726f6381996-08-26 18:22:31 +000068xrealloc (pointer, bytes)
Jari Aaltobb706242000-03-17 21:46:59 +000069 PTR_T pointer;
Jari Aaltof73dda02001-11-13 17:56:06 +000070 size_t bytes;
Jari Aalto726f6381996-08-26 18:22:31 +000071{
Jari Aaltof73dda02001-11-13 17:56:06 +000072 PTR_T temp;
Jari Aalto726f6381996-08-26 18:22:31 +000073
Jari Aaltof73dda02001-11-13 17:56:06 +000074 temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
Jari Aalto726f6381996-08-26 18:22:31 +000075
Jari Aaltoccc6cda1996-12-23 17:02:34 +000076 if (temp == 0)
Jari Aalto726f6381996-08-26 18:22:31 +000077 memory_error_and_abort ("xrealloc");
78 return (temp);
79}