1100203Sgad/* xmalloc.c -- safe versions of malloc and realloc */ 2100203Sgad 3220586Sgad/* Copyright (C) 1991 Free Software Foundation, Inc. 4100203Sgad 5100203Sgad This file is part of GNU Readline, a library for reading lines 6100203Sgad of text with interactive input and history editing. 7100203Sgad 8100203Sgad Readline is free software; you can redistribute it and/or modify it 9100203Sgad under the terms of the GNU General Public License as published by the 10100203Sgad Free Software Foundation; either version 2, or (at your option) any 11100203Sgad later version. 12100203Sgad 13100203Sgad Readline is distributed in the hope that it will be useful, but 14100203Sgad WITHOUT ANY WARRANTY; without even the implied warranty of 15100203Sgad MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16100203Sgad General Public License for more details. 17100203Sgad 18100203Sgad You should have received a copy of the GNU General Public License 19100203Sgad along with Readline; see the file COPYING. If not, write to the Free 20100203Sgad Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 21100203Sgad#define READLINE_LIBRARY 22100203Sgad 23100203Sgad#if defined (HAVE_CONFIG_H) 24100203Sgad#include <config.h> 25100203Sgad#endif 26100203Sgad 27100203Sgad#include <stdio.h> 28100203Sgad 29100203Sgad#if defined (HAVE_STDLIB_H) 30100203Sgad# include <stdlib.h> 31100203Sgad#else 32100203Sgad# include "ansi_stdlib.h" 33100203Sgad#endif /* HAVE_STDLIB_H */ 34100203Sgad 35117541Sgad#include "xmalloc.h" 36117541Sgad 37100203Sgad/* **************************************************************** */ 38100203Sgad/* */ 39100203Sgad/* Memory Allocation and Deallocation. */ 40100203Sgad/* */ 41100203Sgad/* **************************************************************** */ 42100203Sgad 43100203Sgadstatic void 44100203Sgadmemory_error_and_abort (fname) 45100203Sgad char *fname; 46100203Sgad{ 47100203Sgad fprintf (stderr, "%s: out of virtual memory\n", fname); 48100203Sgad exit (2); 49100203Sgad} 50100203Sgad 51100203Sgad/* Return a pointer to free()able block of memory large enough 52100203Sgad to hold BYTES number of bytes. If the memory cannot be allocated, 53100203Sgad print an error message and abort. */ 54100203SgadPTR_T 55100203Sgadxmalloc (bytes) 56100203Sgad size_t bytes; 57100203Sgad{ 58100203Sgad PTR_T temp; 59100203Sgad 60100203Sgad temp = malloc (bytes); 61100203Sgad if (temp == 0) 62100203Sgad memory_error_and_abort ("xmalloc"); 63100203Sgad return (temp); 64100203Sgad} 65100203Sgad 66100203SgadPTR_T 67100203Sgadxrealloc (pointer, bytes) 68100203Sgad PTR_T pointer; 69100203Sgad size_t bytes; 70100203Sgad{ 71100203Sgad PTR_T temp; 72100203Sgad 73100203Sgad temp = pointer ? realloc (pointer, bytes) : malloc (bytes); 74100203Sgad 75100203Sgad if (temp == 0) 76100203Sgad memory_error_and_abort ("xrealloc"); 77100203Sgad return (temp); 78100203Sgad} 79100203Sgad 80100203Sgad/* Use this as the function to call when adding unwind protects so we 81100203Sgad don't need to know what free() returns. */ 82100203Sgadvoid 83100203Sgadxfree (string) 84100203Sgad PTR_T string; 85100203Sgad{ 86100203Sgad if (string) 87100203Sgad free (string); 88100203Sgad} 89100203Sgad