]>
Commit | Line | Data |
---|---|---|
d7df7ba2 DB |
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 | * | |
8ba013af | 7 | * General memory allocation wrappers for malloc, realloc, calloc and strdup |
d7df7ba2 DB |
8 | */ |
9 | ||
10 | #ifndef UTIL_LINUX_XALLOC_H | |
11 | #define UTIL_LINUX_XALLOC_H | |
12 | ||
13 | #include <stdlib.h> | |
b45fa8b2 | 14 | #include <string.h> |
d7df7ba2 | 15 | |
40084d0d KZ |
16 | #include "c.h" |
17 | ||
3e27b34e KZ |
18 | #ifndef XALLOC_EXIT_CODE |
19 | # define XALLOC_EXIT_CODE EXIT_FAILURE | |
20 | #endif | |
21 | ||
16c9f32f KZ |
22 | static inline void __err_oom(const char *file, unsigned int line) |
23 | { | |
24 | err(XALLOC_EXIT_CODE, "%s: %u: cannot allocate memory", file, line); | |
25 | } | |
26 | ||
27 | #define err_oom() __err_oom(__FILE__, __LINE__) | |
28 | ||
f1b327f8 | 29 | static inline __ul_alloc_size(1) __ul_returns_nonnull |
d7df7ba2 DB |
30 | void *xmalloc(const size_t size) |
31 | { | |
32 | void *ret = malloc(size); | |
33 | ||
34 | if (!ret && size) | |
3e27b34e | 35 | err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); |
d7df7ba2 DB |
36 | return ret; |
37 | } | |
38 | ||
f1b327f8 | 39 | static inline __ul_alloc_size(2) __ul_returns_nonnull |
d7df7ba2 DB |
40 | void *xrealloc(void *ptr, const size_t size) |
41 | { | |
42 | void *ret = realloc(ptr, size); | |
43 | ||
44 | if (!ret && size) | |
3e27b34e | 45 | err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); |
d7df7ba2 DB |
46 | return ret; |
47 | } | |
48 | ||
f1b327f8 | 49 | static inline __ul_calloc_size(1, 2) __ul_returns_nonnull |
d7df7ba2 DB |
50 | void *xcalloc(const size_t nelems, const size_t size) |
51 | { | |
52 | void *ret = calloc(nelems, size); | |
53 | ||
54 | if (!ret && size && nelems) | |
3e27b34e | 55 | err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); |
d7df7ba2 DB |
56 | return ret; |
57 | } | |
58 | ||
f1b327f8 SK |
59 | static inline char __attribute__((warn_unused_result)) __ul_returns_nonnull |
60 | *xstrdup(const char *str) | |
ecc264bc | 61 | { |
12218ccd | 62 | char *ret; |
ecc264bc | 63 | |
12218ccd DR |
64 | if (!str) |
65 | return NULL; | |
8ddb6b0c | 66 | |
12218ccd | 67 | ret = strdup(str); |
8ddb6b0c | 68 | |
12218ccd DR |
69 | if (!ret) |
70 | err(XALLOC_EXIT_CODE, "cannot duplicate string"); | |
71 | return ret; | |
ecc264bc KZ |
72 | } |
73 | ||
f1b327f8 SK |
74 | static inline char * __attribute__((warn_unused_result)) __ul_returns_nonnull |
75 | xstrndup(const char *str, size_t size) | |
2b88d3d7 KZ |
76 | { |
77 | char *ret; | |
78 | ||
79 | if (!str) | |
80 | return NULL; | |
81 | ||
82 | ret = strndup(str, size); | |
83 | ||
84 | if (!ret) | |
85 | err(XALLOC_EXIT_CODE, "cannot duplicate string"); | |
86 | return ret; | |
87 | } | |
88 | ||
89 | ||
d0aa8a44 SK |
90 | static inline int __attribute__ ((__format__(printf, 2, 3))) |
91 | xasprintf(char **strp, const char *fmt, ...) | |
eeb31db9 SK |
92 | { |
93 | int ret; | |
94 | va_list args; | |
95 | va_start(args, fmt); | |
96 | ret = vasprintf(&(*strp), fmt, args); | |
97 | va_end(args); | |
98 | if (ret < 0) | |
99 | err(XALLOC_EXIT_CODE, "cannot allocate string"); | |
100 | return ret; | |
101 | } | |
1b2aa629 | 102 | |
3eb3c8d7 RG |
103 | static inline int __attribute__ ((__format__(printf, 2, 0))) |
104 | xvasprintf(char **strp, const char *fmt, va_list ap) | |
2e6c3a53 KZ |
105 | { |
106 | int ret = vasprintf(&(*strp), fmt, ap); | |
107 | if (ret < 0) | |
108 | err(XALLOC_EXIT_CODE, "cannot allocate string"); | |
109 | return ret; | |
110 | } | |
111 | ||
1b2aa629 | 112 | |
c9678832 | 113 | static inline char * __attribute__((warn_unused_result)) xgethostname(void) |
1b2aa629 KZ |
114 | { |
115 | char *name; | |
116 | size_t sz = get_hostname_max() + 1; | |
117 | ||
118 | name = xmalloc(sizeof(char) * sz); | |
1b2aa629 | 119 | |
8362545b KZ |
120 | if (gethostname(name, sz) != 0) { |
121 | free(name); | |
122 | return NULL; | |
123 | } | |
1b2aa629 KZ |
124 | name[sz - 1] = '\0'; |
125 | return name; | |
126 | } | |
127 | ||
d7df7ba2 | 128 | #endif |