xref: /openbsd-src/gnu/usr.bin/texinfo/lib/xmalloc.c (revision a1acfa9b69ad64eb720639240c8438f11107dc85)
1 /* xmalloc.c -- safe versions of malloc and realloc.
2 
3    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 2004 Free Software
4    Foundation, Inc.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20    Written by Brian Fox (bfox@ai.mit.edu). */
21 
22 #if !defined (ALREADY_HAVE_XMALLOC)
23 #include "system.h"
24 
25 static void
memory_error_and_abort(const char * fname)26 memory_error_and_abort (const char *fname)
27 {
28   fprintf (stderr, "%s: Out of virtual memory!\n", fname);
29   abort ();
30 }
31 
32 /* Return a pointer to free()able block of memory large enough
33    to hold BYTES number of bytes.  If the memory cannot be allocated,
34    print an error message and abort. */
35 void *
xmalloc(size_t bytes)36 xmalloc (size_t bytes)
37 {
38   void *temp = malloc (bytes);
39 
40   if (!temp)
41     memory_error_and_abort ("xmalloc");
42   return (temp);
43 }
44 
45 void *
xrealloc(void * pointer,size_t bytes)46 xrealloc (void *pointer, size_t bytes)
47 {
48   void *temp;
49 
50   if (!pointer)
51     temp = malloc (bytes);
52   else
53     temp = realloc (pointer, bytes);
54 
55   if (!temp)
56     memory_error_and_abort ("xrealloc");
57 
58   return (temp);
59 }
60 
61 #endif /* !ALREADY_HAVE_XMALLOC */
62