blob: f6dec67a8953e6812f98b27a4feba4670186d9e7 [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001/* xmalloc.c -- safe versions of malloc and realloc */
2
Jari Aaltob80f6442004-07-27 13:29:18 +00003/* Copyright (C) 1991-2003 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
5 This file is part of GNU Readline, a library for reading lines
6 of text with interactive input and history editing.
7
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*/
Jari Aalto726f6381996-08-26 18:22:31 +000021
Jari Aaltoccc6cda1996-12-23 17:02:34 +000022#if defined (HAVE_CONFIG_H)
23#include <config.h>
24#endif
25
Jari Aalto726f6381996-08-26 18:22:31 +000026#include <stdio.h>
27
28#if defined (HAVE_STDLIB_H)
29# include <stdlib.h>
30#else
31# include "ansi_stdlib.h"
32#endif /* HAVE_STDLIB_H */
33
Jari Aaltof73dda02001-11-13 17:56:06 +000034/* Generic pointer type. */
35#ifndef PTR_T
36
37#if defined (__STDC__)
38# define PTR_T void *
39#else
40# define PTR_T char *
41#endif
42
43#endif /* PTR_T */
Jari Aalto726f6381996-08-26 18:22:31 +000044
45/* **************************************************************** */
46/* */
47/* Memory Allocation and Deallocation. */
48/* */
49/* **************************************************************** */
50
Jari Aalto726f6381996-08-26 18:22:31 +000051static void
52memory_error_and_abort (fname)
53 char *fname;
54{
Jari Aaltoccc6cda1996-12-23 17:02:34 +000055 fprintf (stderr, "%s: out of virtual memory\n", fname);
56 exit (2);
Jari Aalto726f6381996-08-26 18:22:31 +000057}
Jari Aaltoccc6cda1996-12-23 17:02:34 +000058
Jari Aaltof73dda02001-11-13 17:56:06 +000059/* Return a pointer to free()able block of memory large enough
60 to hold BYTES number of bytes. If the memory cannot be allocated,
61 print an error message and abort. */
62PTR_T
63xmalloc (bytes)
64 size_t bytes;
65{
66 PTR_T temp;
67
68 temp = malloc (bytes);
69 if (temp == 0)
70 memory_error_and_abort ("xmalloc");
71 return (temp);
72}
73
74PTR_T
75xrealloc (pointer, bytes)
76 PTR_T pointer;
77 size_t bytes;
78{
79 PTR_T temp;
80
81 temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
82
83 if (temp == 0)
84 memory_error_and_abort ("xrealloc");
85 return (temp);
86}
87
Jari Aaltoccc6cda1996-12-23 17:02:34 +000088void
89xfree (string)
Jari Aaltof73dda02001-11-13 17:56:06 +000090 PTR_T string;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000091{
92 if (string)
93 free (string);
94}