| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: xmalloc.c,v 1.1.1.1 2003/01/26 00:43:15 wiz Exp $ */ | | 1 | /* $NetBSD: xmalloc.c,v 1.2 2012/03/12 17:41:59 christos Exp $ */ |
2 | | | 2 | |
3 | /* xmalloc.c -- malloc with out of memory checking | | 3 | /* xmalloc.c -- malloc with out of memory checking |
4 | Copyright (C) 1990-1999, 2000, 2002 Free Software Foundation, Inc. | | 4 | Copyright (C) 1990-1999, 2000, 2002 Free Software Foundation, Inc. |
5 | | | 5 | |
6 | This program is free software; you can redistribute it and/or modify | | 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 | | 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) | | 8 | the Free Software Foundation; either version 2, or (at your option) |
9 | any later version. | | 9 | any later version. |
10 | | | 10 | |
11 | This program is distributed in the hope that it will be useful, | | 11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. | | 14 | GNU General Public License for more details. |
| @@ -74,42 +74,42 @@ xalloc_die (void) | | | @@ -74,42 +74,42 @@ xalloc_die (void) |
74 | its first argument is 0. To help compilers understand the | | 74 | its first argument is 0. To help compilers understand the |
75 | xalloc_die does terminate, call exit. */ | | 75 | xalloc_die does terminate, call exit. */ |
76 | exit (EXIT_FAILURE); | | 76 | exit (EXIT_FAILURE); |
77 | } | | 77 | } |
78 | | | 78 | |
79 | /* Allocate N bytes of memory dynamically, with error checking. */ | | 79 | /* Allocate N bytes of memory dynamically, with error checking. */ |
80 | | | 80 | |
81 | void * | | 81 | void * |
82 | xmalloc (size_t n) | | 82 | xmalloc (size_t n) |
83 | { | | 83 | { |
84 | void *p; | | 84 | void *p; |
85 | | | 85 | |
86 | p = malloc (n); | | 86 | p = malloc (n); |
87 | if (p == 0) | | 87 | if (p == 0 && n) |
88 | xalloc_die (); | | 88 | xalloc_die (); |
89 | return p; | | 89 | return p; |
90 | } | | 90 | } |
91 | | | 91 | |
92 | /* Change the size of an allocated block of memory P to N bytes, | | 92 | /* Change the size of an allocated block of memory P to N bytes, |
93 | with error checking. */ | | 93 | with error checking. */ |
94 | | | 94 | |
95 | void * | | 95 | void * |
96 | xrealloc (void *p, size_t n) | | 96 | xrealloc (void *p, size_t n) |
97 | { | | 97 | { |
98 | p = realloc (p, n); | | 98 | p = realloc (p, n); |
99 | if (p == 0) | | 99 | if (p == 0 && n) |
100 | xalloc_die (); | | 100 | xalloc_die (); |
101 | return p; | | 101 | return p; |
102 | } | | 102 | } |
103 | | | 103 | |
104 | /* Allocate memory for N elements of S bytes, with error checking. */ | | 104 | /* Allocate memory for N elements of S bytes, with error checking. */ |
105 | | | 105 | |
106 | void * | | 106 | void * |
107 | xcalloc (size_t n, size_t s) | | 107 | xcalloc (size_t n, size_t s) |
108 | { | | 108 | { |
109 | void *p; | | 109 | void *p; |
110 | | | 110 | |
111 | p = calloc (n, s); | | 111 | p = calloc (n, s); |
112 | if (p == 0) | | 112 | if (p == 0 && n && s) |
113 | xalloc_die (); | | 113 | xalloc_die (); |
114 | return p; | | 114 | return p; |
115 | } | | 115 | } |