xref: /openbsd-src/gnu/lib/libreadline/xmalloc.c (revision af70c2df780099dcdde93deaba1af49686a47bbf)
11acd27e7Smillert /* xmalloc.c -- safe versions of malloc and realloc */
21acd27e7Smillert 
31acd27e7Smillert /* Copyright (C) 1991 Free Software Foundation, Inc.
41acd27e7Smillert 
51acd27e7Smillert    This file is part of GNU Readline, a library for reading lines
61acd27e7Smillert    of text with interactive input and history editing.
71acd27e7Smillert 
81acd27e7Smillert    Readline is free software; you can redistribute it and/or modify it
91acd27e7Smillert    under the terms of the GNU General Public License as published by the
101acd27e7Smillert    Free Software Foundation; either version 2, or (at your option) any
111acd27e7Smillert    later version.
121acd27e7Smillert 
131acd27e7Smillert    Readline is distributed in the hope that it will be useful, but
141acd27e7Smillert    WITHOUT ANY WARRANTY; without even the implied warranty of
151acd27e7Smillert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
161acd27e7Smillert    General Public License for more details.
171acd27e7Smillert 
181acd27e7Smillert    You should have received a copy of the GNU General Public License
191acd27e7Smillert    along with Readline; see the file COPYING.  If not, write to the Free
201acd27e7Smillert    Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
211acd27e7Smillert #define READLINE_LIBRARY
221acd27e7Smillert 
231acd27e7Smillert #if defined (HAVE_CONFIG_H)
241acd27e7Smillert #include <config.h>
251acd27e7Smillert #endif
261acd27e7Smillert 
271acd27e7Smillert #include <stdio.h>
281acd27e7Smillert 
291acd27e7Smillert #if defined (HAVE_STDLIB_H)
301acd27e7Smillert #  include <stdlib.h>
311acd27e7Smillert #else
321acd27e7Smillert #  include "ansi_stdlib.h"
331acd27e7Smillert #endif /* HAVE_STDLIB_H */
341acd27e7Smillert 
351acd27e7Smillert #include "xmalloc.h"
361acd27e7Smillert 
371acd27e7Smillert /* **************************************************************** */
381acd27e7Smillert /*								    */
391acd27e7Smillert /*		   Memory Allocation and Deallocation.		    */
401acd27e7Smillert /*								    */
411acd27e7Smillert /* **************************************************************** */
421acd27e7Smillert 
431acd27e7Smillert /* Return a pointer to free()able block of memory large enough
441acd27e7Smillert    to hold BYTES number of bytes.  If the memory cannot be allocated,
451acd27e7Smillert    print an error message and abort. */
46*af70c2dfSkettenis PTR_T
xmalloc(bytes)471acd27e7Smillert xmalloc (bytes)
48*af70c2dfSkettenis      size_t bytes;
491acd27e7Smillert {
50*af70c2dfSkettenis   PTR_T temp;
511acd27e7Smillert 
52*af70c2dfSkettenis   temp = malloc (bytes);
531acd27e7Smillert   if (temp == 0)
541acd27e7Smillert     memory_error_and_abort ("xmalloc");
551acd27e7Smillert   return (temp);
561acd27e7Smillert }
571acd27e7Smillert 
58*af70c2dfSkettenis PTR_T
xrealloc(pointer,bytes)591acd27e7Smillert xrealloc (pointer, bytes)
601acd27e7Smillert      PTR_T pointer;
61*af70c2dfSkettenis      size_t bytes;
621acd27e7Smillert {
63*af70c2dfSkettenis   PTR_T temp;
641acd27e7Smillert 
65*af70c2dfSkettenis   temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
661acd27e7Smillert 
671acd27e7Smillert   if (temp == 0)
681acd27e7Smillert     memory_error_and_abort ("xrealloc");
691acd27e7Smillert   return (temp);
701acd27e7Smillert }
711acd27e7Smillert 
721acd27e7Smillert /* Use this as the function to call when adding unwind protects so we
731acd27e7Smillert    don't need to know what free() returns. */
741acd27e7Smillert void
xfree(string)751acd27e7Smillert xfree (string)
761acd27e7Smillert      PTR_T string;
771acd27e7Smillert {
781acd27e7Smillert   if (string)
791acd27e7Smillert     free (string);
801acd27e7Smillert }
81