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