171
|
1 /* xmalloc.c -- safe versions of malloc and realloc */ |
|
2 |
|
3 /* This file is part of GNU Info, a program for reading online documentation |
|
4 stored in Info format. |
|
5 |
|
6 This file has appeared in prior works by the Free Software Foundation; |
|
7 thus it carries copyright dates from 1988 through 1993. |
|
8 |
|
9 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software |
|
10 Foundation, Inc. |
|
11 |
|
12 This program is free software; you can redistribute it and/or modify |
|
13 it under the terms of the GNU General Public License as published by |
|
14 the Free Software Foundation; either version 2, or (at your option) |
|
15 any later version. |
|
16 |
|
17 This program is distributed in the hope that it will be useful, |
|
18 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 GNU General Public License for more details. |
|
21 |
|
22 You should have received a copy of the GNU General Public License |
|
23 along with this program; if not, write to the Free Software |
|
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
25 |
|
26 Written by Brian Fox (bfox@ai.mit.edu). */ |
|
27 |
1242
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
171
|
32 #if !defined (ALREADY_HAVE_XMALLOC) |
|
33 #include <stdio.h> |
|
34 |
|
35 static void memory_error_and_abort (); |
|
36 |
|
37 /* **************************************************************** */ |
|
38 /* */ |
|
39 /* Memory Allocation and Deallocation. */ |
|
40 /* */ |
|
41 /* **************************************************************** */ |
|
42 |
|
43 /* Return a pointer to free()able block of memory large enough |
|
44 to hold BYTES number of bytes. If the memory cannot be allocated, |
|
45 print an error message and abort. */ |
|
46 void * |
|
47 xmalloc (bytes) |
|
48 int bytes; |
|
49 { |
|
50 void *temp = (void *)malloc (bytes); |
|
51 |
|
52 if (!temp) |
|
53 memory_error_and_abort ("xmalloc"); |
|
54 return (temp); |
|
55 } |
|
56 |
|
57 void * |
|
58 xrealloc (pointer, bytes) |
|
59 void *pointer; |
|
60 int bytes; |
|
61 { |
|
62 void *temp; |
|
63 |
|
64 if (!pointer) |
|
65 temp = (void *)malloc (bytes); |
|
66 else |
|
67 temp = (void *)realloc (pointer, bytes); |
|
68 |
|
69 if (!temp) |
|
70 memory_error_and_abort ("xrealloc"); |
|
71 |
|
72 return (temp); |
|
73 } |
|
74 |
|
75 static void |
|
76 memory_error_and_abort (fname) |
|
77 char *fname; |
|
78 { |
|
79 fprintf (stderr, "%s: Out of virtual memory!\n", fname); |
|
80 abort (); |
|
81 } |
|
82 #endif /* !ALREADY_HAVE_XMALLOC */ |