121308Sache/* xmalloc.c -- safe versions of malloc and realloc */ 221308Sache 321308Sache/* Copyright (C) 1991 Free Software Foundation, Inc. 421308Sache 521308Sache This file is part of GNU Readline, a library for reading lines 621308Sache of text with interactive input and history editing. 721308Sache 821308Sache Readline is free software; you can redistribute it and/or modify it 921308Sache under the terms of the GNU General Public License as published by the 1058310Sache Free Software Foundation; either version 2, or (at your option) any 1121308Sache later version. 1221308Sache 1321308Sache Readline is distributed in the hope that it will be useful, but 1421308Sache WITHOUT ANY WARRANTY; without even the implied warranty of 1521308Sache MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1621308Sache General Public License for more details. 1721308Sache 1821308Sache You should have received a copy of the GNU General Public License 1921308Sache along with Readline; see the file COPYING. If not, write to the Free 2058310Sache Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 2158310Sache#define READLINE_LIBRARY 2221308Sache 2321308Sache#if defined (HAVE_CONFIG_H) 2421308Sache#include <config.h> 2521308Sache#endif 2621308Sache 2721308Sache#include <stdio.h> 2821308Sache 2921308Sache#if defined (HAVE_STDLIB_H) 3021308Sache# include <stdlib.h> 3121308Sache#else 3221308Sache# include "ansi_stdlib.h" 3321308Sache#endif /* HAVE_STDLIB_H */ 3421308Sache 3558310Sache#include "xmalloc.h" 3621308Sache 3721308Sache/* **************************************************************** */ 3821308Sache/* */ 3921308Sache/* Memory Allocation and Deallocation. */ 4021308Sache/* */ 4121308Sache/* **************************************************************** */ 4221308Sache 4358310Sachestatic void 4458310Sachememory_error_and_abort (fname) 4558310Sache char *fname; 4658310Sache{ 4758310Sache fprintf (stderr, "%s: out of virtual memory\n", fname); 4858310Sache exit (2); 4958310Sache} 5058310Sache 5121308Sache/* Return a pointer to free()able block of memory large enough 5221308Sache to hold BYTES number of bytes. If the memory cannot be allocated, 5321308Sache print an error message and abort. */ 54119610SachePTR_T 5521308Sachexmalloc (bytes) 56119610Sache size_t bytes; 5721308Sache{ 58119610Sache PTR_T temp; 5921308Sache 60119610Sache temp = malloc (bytes); 6121308Sache if (temp == 0) 6221308Sache memory_error_and_abort ("xmalloc"); 6321308Sache return (temp); 6421308Sache} 6521308Sache 66119610SachePTR_T 6721308Sachexrealloc (pointer, bytes) 6858310Sache PTR_T pointer; 69119610Sache size_t bytes; 7021308Sache{ 71119610Sache PTR_T temp; 7221308Sache 73119610Sache temp = pointer ? realloc (pointer, bytes) : malloc (bytes); 7421308Sache 7521308Sache if (temp == 0) 7621308Sache memory_error_and_abort ("xrealloc"); 7721308Sache return (temp); 7821308Sache} 7921308Sache 8021308Sache/* Use this as the function to call when adding unwind protects so we 8121308Sache don't need to know what free() returns. */ 8221308Sachevoid 8321308Sachexfree (string) 8458310Sache PTR_T string; 8521308Sache{ 8621308Sache if (string) 8721308Sache free (string); 8821308Sache} 89