1 | /* |
---|
2 | * Non-failing memory allocation routines. |
---|
3 | * Copyright (c) 1995 Markku Rossi. |
---|
4 | * |
---|
5 | * Author: Markku Rossi <mtr@iki.fi> |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * This file is part of GNU enscript. |
---|
10 | * |
---|
11 | * This program is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation; either version 2, or (at your option) |
---|
14 | * any later version. |
---|
15 | * |
---|
16 | * This program is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU General Public License |
---|
22 | * along with this program; see the file COPYING. If not, write to |
---|
23 | * the Free Software Foundation, 59 Temple Place - Suite 330, |
---|
24 | * Boston, MA 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include <stdio.h> |
---|
28 | |
---|
29 | #ifdef HAVE_CONFIG_H |
---|
30 | #include <config.h> |
---|
31 | #endif |
---|
32 | |
---|
33 | #if STDC_HEADERS |
---|
34 | #include <stdlib.h> |
---|
35 | #else /* no STDC_HEADERS */ |
---|
36 | |
---|
37 | #if HAVE_STDLIB_H |
---|
38 | #include <stdlib.h> |
---|
39 | #endif |
---|
40 | |
---|
41 | #endif /* no STDC_HEADERS */ |
---|
42 | |
---|
43 | #if ENABLE_NLS |
---|
44 | #include <libintl.h> |
---|
45 | #define _(String) gettext (String) |
---|
46 | #else |
---|
47 | #define _(String) String |
---|
48 | #endif |
---|
49 | |
---|
50 | /* |
---|
51 | * Global functions. |
---|
52 | */ |
---|
53 | |
---|
54 | void * |
---|
55 | xmalloc (size) |
---|
56 | size_t size; |
---|
57 | { |
---|
58 | void *ptr; |
---|
59 | |
---|
60 | ptr = malloc (size); |
---|
61 | if (ptr == NULL) |
---|
62 | { |
---|
63 | fprintf (stderr, _("xmalloc(): couldn't allocate %d bytes\n"), size); |
---|
64 | exit (1); |
---|
65 | } |
---|
66 | |
---|
67 | return ptr; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | void * |
---|
72 | xcalloc (num, size) |
---|
73 | size_t num; |
---|
74 | size_t size; |
---|
75 | { |
---|
76 | void *ptr; |
---|
77 | |
---|
78 | ptr = calloc (num, size); |
---|
79 | if (ptr == NULL) |
---|
80 | { |
---|
81 | fprintf (stderr, _("xcalloc(): couldn't allocate %d bytes\n"), size); |
---|
82 | exit (1); |
---|
83 | } |
---|
84 | |
---|
85 | return ptr; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void * |
---|
90 | xrealloc (ptr, size) |
---|
91 | void *ptr; |
---|
92 | size_t size; |
---|
93 | { |
---|
94 | void *nptr; |
---|
95 | |
---|
96 | if (ptr == NULL) |
---|
97 | return xmalloc (size); |
---|
98 | |
---|
99 | nptr = realloc (ptr, size); |
---|
100 | if (nptr == NULL) |
---|
101 | { |
---|
102 | fprintf (stderr, _("xrealloc(): couldn't reallocate %d bytes\n"), size); |
---|
103 | exit (1); |
---|
104 | } |
---|
105 | |
---|
106 | return nptr; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | void |
---|
111 | xfree (ptr) |
---|
112 | void *ptr; |
---|
113 | { |
---|
114 | if (ptr == NULL) |
---|
115 | return; |
---|
116 | |
---|
117 | free (ptr); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | char * |
---|
122 | xstrdup (str) |
---|
123 | char *str; |
---|
124 | { |
---|
125 | char *tmp; |
---|
126 | |
---|
127 | tmp = xmalloc (strlen (str) + 1); |
---|
128 | strcpy (tmp, str); |
---|
129 | |
---|
130 | return tmp; |
---|
131 | } |
---|