/[public]/getopt/trunk/xalloc.h
ViewVC logotype

Contents of /getopt/trunk/xalloc.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 323 - (show annotations)
Tue Jul 17 17:09:37 2012 UTC (12 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 926 byte(s)
(Frodo) Remaining important differences with util-linux resolved

1 /*
2 * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 *
7 * General memory allocation wrappers for malloc, realloc, calloc and strdup
8 */
9
10 #ifndef UTIL_LINUX_XALLOC_H
11 #define UTIL_LINUX_XALLOC_H
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #ifndef XALLOC_EXIT_CODE
17 # define XALLOC_EXIT_CODE 3
18 #endif
19
20 static void *xmalloc(const size_t size)
21 {
22 void *ret = malloc(size);
23
24 if (!ret && size) {
25 fprintf(stderr, "%s: error: cannot allocate %zu bytes", program_invocation_short_name, size);
26 exit(XALLOC_EXIT_CODE);
27 }
28 return ret;
29 }
30
31 static void *xrealloc(void *ptr, const size_t size)
32 {
33 void *ret = realloc(ptr, size);
34
35 if (!ret && size)
36 fprintf(stderr, "%s: Error: cannot allocate %zu bytes", program_invocation_short_name, size);
37 return ret;
38 }
39
40 #endif

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26