xref: /openbsd-src/gnu/usr.bin/texinfo/lib/xmalloc.c (revision a1acfa9b69ad64eb720639240c8438f11107dc85)
1*a1acfa9bSespie /* xmalloc.c -- safe versions of malloc and realloc.
2840175f0Skstailey 
3*a1acfa9bSespie    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 2004 Free Software
4840175f0Skstailey    Foundation, Inc.
5840175f0Skstailey 
6840175f0Skstailey    This program is free software; you can redistribute it and/or modify
7840175f0Skstailey    it under the terms of the GNU General Public License as published by
8840175f0Skstailey    the Free Software Foundation; either version 2, or (at your option)
9840175f0Skstailey    any later version.
10840175f0Skstailey 
11840175f0Skstailey    This program is distributed in the hope that it will be useful,
12840175f0Skstailey    but WITHOUT ANY WARRANTY; without even the implied warranty of
13840175f0Skstailey    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14840175f0Skstailey    GNU General Public License for more details.
15840175f0Skstailey 
16840175f0Skstailey    You should have received a copy of the GNU General Public License
17840175f0Skstailey    along with this program; if not, write to the Free Software
18840175f0Skstailey    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19840175f0Skstailey 
20840175f0Skstailey    Written by Brian Fox (bfox@ai.mit.edu). */
21840175f0Skstailey 
22840175f0Skstailey #if !defined (ALREADY_HAVE_XMALLOC)
23*a1acfa9bSespie #include "system.h"
24840175f0Skstailey 
25*a1acfa9bSespie static void
memory_error_and_abort(const char * fname)26*a1acfa9bSespie memory_error_and_abort (const char *fname)
27*a1acfa9bSespie {
28*a1acfa9bSespie   fprintf (stderr, "%s: Out of virtual memory!\n", fname);
29*a1acfa9bSespie   abort ();
30*a1acfa9bSespie }
31840175f0Skstailey 
32840175f0Skstailey /* Return a pointer to free()able block of memory large enough
33840175f0Skstailey    to hold BYTES number of bytes.  If the memory cannot be allocated,
34840175f0Skstailey    print an error message and abort. */
35840175f0Skstailey void *
xmalloc(size_t bytes)36*a1acfa9bSespie xmalloc (size_t bytes)
37840175f0Skstailey {
38840175f0Skstailey   void *temp = malloc (bytes);
39840175f0Skstailey 
40840175f0Skstailey   if (!temp)
41840175f0Skstailey     memory_error_and_abort ("xmalloc");
42840175f0Skstailey   return (temp);
43840175f0Skstailey }
44840175f0Skstailey 
45840175f0Skstailey void *
xrealloc(void * pointer,size_t bytes)46*a1acfa9bSespie xrealloc (void *pointer, size_t bytes)
47840175f0Skstailey {
48840175f0Skstailey   void *temp;
49840175f0Skstailey 
50840175f0Skstailey   if (!pointer)
51840175f0Skstailey     temp = malloc (bytes);
52840175f0Skstailey   else
53840175f0Skstailey     temp = realloc (pointer, bytes);
54840175f0Skstailey 
55840175f0Skstailey   if (!temp)
56840175f0Skstailey     memory_error_and_abort ("xrealloc");
57840175f0Skstailey 
58840175f0Skstailey   return (temp);
59840175f0Skstailey }
60840175f0Skstailey 
61840175f0Skstailey #endif /* !ALREADY_HAVE_XMALLOC */
62