blob: ae00baf3f0fe846635419b9c3745f63a54aeb1f6 [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* xmalloc.c -- safe versions of malloc and realloc */
2
3/* Copyright (C) 1991 Free Software Foundation, Inc.
4
5 This file is part of GNU Readline, a library for reading lines
6 of text with interactive input and history editing.
7
8 Readline is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
Jari Aaltobb706242000-03-17 21:46:59 +000010 Free Software Foundation; either version 2, or (at your option) any
Jari Aaltoccc6cda1996-12-23 17:02:34 +000011 later version.
12
13 Readline is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Readline; see the file COPYING. If not, write to the Free
Jari Aaltobb706242000-03-17 21:46:59 +000020 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000021
22#if defined (HAVE_CONFIG_H)
23#include <config.h>
24#endif
25
Jari Aaltod166f041997-06-05 14:59:13 +000026#include "bashtypes.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000027#include <stdio.h>
28
29#if defined (HAVE_UNISTD_H)
30# include <unistd.h>
31#endif
32
33#if defined (HAVE_STDLIB_H)
34# include <stdlib.h>
35#else
36# include "ansi_stdlib.h"
37#endif /* HAVE_STDLIB_H */
38
39#include "error.h"
40
41#if !defined (PTR_T)
42# if defined (__STDC__)
43# define PTR_T void *
44# else
45# define PTR_T char *
46# endif /* !__STDC__ */
47#endif /* !PTR_T */
48
Jari Aaltob72432f1999-02-19 17:11:39 +000049#if defined (HAVE_SBRK) && !defined (SBRK_DECLARED)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000050extern char *sbrk();
51#endif
52
53static PTR_T lbreak;
54static int brkfound;
55static size_t allocated;
56
57/* **************************************************************** */
58/* */
59/* Memory Allocation and Deallocation. */
60/* */
61/* **************************************************************** */
62
Jari Aaltob72432f1999-02-19 17:11:39 +000063#if defined (HAVE_SBRK)
64static size_t
65findbrk ()
66{
67 if (brkfound == 0)
68 {
69 lbreak = (PTR_T)sbrk (0);
70 brkfound++;
71 }
72 return (char *)sbrk (0) - (char *)lbreak;
73}
74#endif
75
Jari Aaltoccc6cda1996-12-23 17:02:34 +000076/* Return a pointer to free()able block of memory large enough
77 to hold BYTES number of bytes. If the memory cannot be allocated,
78 print an error message and abort. */
79char *
80xmalloc (bytes)
81 size_t bytes;
82{
83 char *temp;
84
85 temp = (char *)malloc (bytes);
86
87 if (temp == 0)
88 {
Jari Aaltob72432f1999-02-19 17:11:39 +000089#if defined (HAVE_SBRK)
90 allocated = findbrk ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +000091 fatal_error ("xmalloc: cannot allocate %lu bytes (%lu bytes allocated)", (unsigned long)bytes, (unsigned long)allocated);
Jari Aaltob72432f1999-02-19 17:11:39 +000092#else
93 fatal_error ("xmalloc: cannot allocate %lu bytes", (unsigned long)bytes);
94#endif /* !HAVE_SBRK */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000095 }
96
97 return (temp);
98}
99
100char *
101xrealloc (pointer, bytes)
102 PTR_T pointer;
103 size_t bytes;
104{
105 char *temp;
106
107 temp = pointer ? (char *)realloc (pointer, bytes) : (char *)malloc (bytes);
108
109 if (temp == 0)
110 {
Jari Aaltob72432f1999-02-19 17:11:39 +0000111#if defined (HAVE_SBRK)
112 allocated = findbrk ();
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000113 fatal_error ("xrealloc: cannot reallocate %lu bytes (%lu bytes allocated)", (unsigned long)bytes, (unsigned long)allocated);
Jari Aaltob72432f1999-02-19 17:11:39 +0000114#else
115 fatal_error ("xmalloc: cannot allocate %lu bytes", (unsigned long)bytes);
116#endif /* !HAVE_SBRK */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000117 }
118
119 return (temp);
120}
121
122/* Use this as the function to call when adding unwind protects so we
123 don't need to know what free() returns. */
124void
125xfree (string)
Jari Aaltobb706242000-03-17 21:46:59 +0000126 PTR_T string;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000127{
128 if (string)
129 free (string);
130}