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