]>
| Commit | Line | Data |
|---|---|---|
| d7df7ba2 | 1 | /* |
| 549a5178 KZ |
2 | * No copyright is claimed. This code is in the public domain; do with |
| 3 | * it what you wish. | |
| faeb1b64 | 4 | * |
| 549a5178 KZ |
5 | * Authors 2010 Davidlohr Bueso <dave@gnu.org> |
| 6 | * 2010-2025 Karel Zak <kzak@redhat.com> | |
| d7df7ba2 | 7 | * |
| 8ba013af | 8 | * General memory allocation wrappers for malloc, realloc, calloc and strdup |
| d7df7ba2 DB |
9 | */ |
| 10 | ||
| 11 | #ifndef UTIL_LINUX_XALLOC_H | |
| 12 | #define UTIL_LINUX_XALLOC_H | |
| 13 | ||
| d4d9744c | 14 | #include <stdio.h> |
| d7df7ba2 | 15 | #include <stdlib.h> |
| b45fa8b2 | 16 | #include <string.h> |
| d7df7ba2 | 17 | |
| 40084d0d | 18 | #include "c.h" |
| bdd28aba | 19 | #include "strutils.h" |
| 40084d0d | 20 | |
| 3e27b34e KZ |
21 | #ifndef XALLOC_EXIT_CODE |
| 22 | # define XALLOC_EXIT_CODE EXIT_FAILURE | |
| 23 | #endif | |
| 24 | ||
| aba9e76e SK |
25 | static inline |
| 26 | __ul_alloc_size(1) | |
| 27 | __ul_returns_nonnull | |
| d7df7ba2 DB |
28 | void *xmalloc(const size_t size) |
| 29 | { | |
| 273ebba1 | 30 | void *ret = malloc(size); |
| d7df7ba2 | 31 | |
| 273ebba1 SK |
32 | if (!ret && size) |
| 33 | err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); | |
| 34 | return ret; | |
| d7df7ba2 DB |
35 | } |
| 36 | ||
| aba9e76e SK |
37 | static inline |
| 38 | __ul_alloc_size(2) | |
| 39 | __ul_returns_nonnull | |
| d7df7ba2 DB |
40 | void *xrealloc(void *ptr, const size_t size) |
| 41 | { | |
| 273ebba1 | 42 | void *ret = realloc(ptr, size); |
| d7df7ba2 | 43 | |
| 273ebba1 SK |
44 | if (!ret && size) |
| 45 | err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); | |
| 46 | return ret; | |
| d7df7ba2 DB |
47 | } |
| 48 | ||
| 3062a58f MY |
49 | static inline |
| 50 | __ul_calloc_size(2, 3) | |
| 51 | __ul_returns_nonnull | |
| 52 | void *xreallocarray(void *ptr, const size_t nelems, const size_t size) | |
| 53 | { | |
| 54 | void *ret = reallocarray(ptr, nelems, size); | |
| 55 | ||
| 56 | if (!ret && size && nelems) | |
| 57 | err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); | |
| 58 | return ret; | |
| 59 | } | |
| 60 | ||
| aba9e76e SK |
61 | static inline |
| 62 | __ul_calloc_size(1, 2) | |
| 63 | __ul_returns_nonnull | |
| d7df7ba2 DB |
64 | void *xcalloc(const size_t nelems, const size_t size) |
| 65 | { | |
| 273ebba1 | 66 | void *ret = calloc(nelems, size); |
| d7df7ba2 | 67 | |
| 273ebba1 SK |
68 | if (!ret && size && nelems) |
| 69 | err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); | |
| 70 | return ret; | |
| d7df7ba2 DB |
71 | } |
| 72 | ||
| 2afb7e6e MY |
73 | static inline |
| 74 | __attribute__((warn_unused_result)) | |
| 75 | __ul_alloc_size(2) | |
| 76 | __ul_returns_nonnull | |
| 77 | void *xmemdup(const void *ptr, size_t size) | |
| 78 | { | |
| 79 | void *ret = xmalloc(size); | |
| 80 | ||
| 81 | memcpy(ret, ptr, size); | |
| 82 | return ret; | |
| 83 | } | |
| 84 | ||
| aba9e76e SK |
85 | static inline |
| 86 | __attribute__((warn_unused_result)) | |
| 87 | __ul_returns_nonnull | |
| 88 | char *xstrdup(const char *str) | |
| ecc264bc | 89 | { |
| 273ebba1 | 90 | char *ret; |
| ecc264bc | 91 | |
| 273ebba1 SK |
92 | assert(str); |
| 93 | ret = strdup(str); | |
| 94 | if (!ret) | |
| 95 | err(XALLOC_EXIT_CODE, "cannot duplicate string"); | |
| 96 | return ret; | |
| ecc264bc KZ |
97 | } |
| 98 | ||
| aba9e76e SK |
99 | static inline |
| 100 | __attribute__((warn_unused_result)) | |
| 101 | __ul_returns_nonnull | |
| 102 | char *xstrndup(const char *str, size_t size) | |
| 2b88d3d7 | 103 | { |
| 273ebba1 | 104 | char *ret; |
| 2b88d3d7 | 105 | |
| 273ebba1 SK |
106 | assert(str); |
| 107 | ret = strndup(str, size); | |
| 108 | if (!ret) | |
| 109 | err(XALLOC_EXIT_CODE, "cannot duplicate string"); | |
| 110 | return ret; | |
| 2b88d3d7 KZ |
111 | } |
| 112 | ||
| 113 | ||
| aba9e76e SK |
114 | static inline |
| 115 | __attribute__((__format__(printf, 2, 3))) | |
| 116 | int xasprintf(char **strp, const char *fmt, ...) | |
| eeb31db9 SK |
117 | { |
| 118 | int ret; | |
| 119 | va_list args; | |
| 273ebba1 | 120 | |
| eeb31db9 SK |
121 | va_start(args, fmt); |
| 122 | ret = vasprintf(&(*strp), fmt, args); | |
| 123 | va_end(args); | |
| 124 | if (ret < 0) | |
| 125 | err(XALLOC_EXIT_CODE, "cannot allocate string"); | |
| 126 | return ret; | |
| 127 | } | |
| 1b2aa629 | 128 | |
| aba9e76e SK |
129 | static inline |
| 130 | __attribute__((__format__(printf, 2, 0))) | |
| 131 | int xvasprintf(char **strp, const char *fmt, va_list ap) | |
| 2e6c3a53 KZ |
132 | { |
| 133 | int ret = vasprintf(&(*strp), fmt, ap); | |
| 273ebba1 | 134 | |
| 2e6c3a53 KZ |
135 | if (ret < 0) |
| 136 | err(XALLOC_EXIT_CODE, "cannot allocate string"); | |
| 137 | return ret; | |
| 138 | } | |
| 139 | ||
| bdd28aba MY |
140 | static inline void xstrappend(char **a, const char *b) |
| 141 | { | |
| d42e5e4b | 142 | if (ul_strappend(a, b) < 0) |
| bdd28aba MY |
143 | err(XALLOC_EXIT_CODE, "cannot allocate string"); |
| 144 | } | |
| 145 | ||
| 146 | static inline void xstrputc(char **a, char c) | |
| 147 | { | |
| 148 | char b[] = {c, '\0'}; | |
| 149 | xstrappend(a, b); | |
| 150 | } | |
| 151 | ||
| 152 | static inline | |
| 153 | __attribute__((__format__(printf, 2, 0))) | |
| 154 | int xstrvfappend(char **a, const char *format, va_list ap) | |
| 155 | { | |
| d42e5e4b | 156 | int ret = ul_strvfappend(a, format, ap); |
| bdd28aba MY |
157 | |
| 158 | if (ret < 0) | |
| 159 | err(XALLOC_EXIT_CODE, "cannot allocate string"); | |
| 160 | return ret; | |
| 161 | ||
| 162 | } | |
| 163 | ||
| 164 | static inline | |
| 165 | __attribute__ ((__format__ (__printf__, 2, 3))) | |
| 166 | int xstrfappend(char **a, const char *format, ...) | |
| 167 | { | |
| 168 | va_list ap; | |
| 169 | int ret; | |
| 170 | ||
| 171 | va_start(ap, format); | |
| 172 | ret = xstrvfappend(a, format, ap); | |
| 173 | va_end(ap); | |
| 174 | ||
| 175 | return ret; | |
| 176 | } | |
| 1b2aa629 | 177 | |
| aba9e76e SK |
178 | static inline |
| 179 | __attribute__((warn_unused_result)) | |
| 180 | char *xgethostname(void) | |
| 1b2aa629 KZ |
181 | { |
| 182 | char *name; | |
| 183 | size_t sz = get_hostname_max() + 1; | |
| 184 | ||
| 185 | name = xmalloc(sizeof(char) * sz); | |
| 8362545b KZ |
186 | if (gethostname(name, sz) != 0) { |
| 187 | free(name); | |
| 188 | return NULL; | |
| 189 | } | |
| 1b2aa629 KZ |
190 | name[sz - 1] = '\0'; |
| 191 | return name; | |
| 192 | } | |
| 193 | ||
| 28453bcc KZ |
194 | static inline |
| 195 | __attribute__((warn_unused_result)) | |
| 196 | char *xgethosturi(const char *proto) | |
| 197 | { | |
| 198 | char *n = xgethostname(); | |
| 199 | char *uri = NULL; | |
| 200 | ||
| 201 | if (!proto) | |
| 202 | proto = "file://"; | |
| 203 | if (!n) | |
| 204 | return xstrdup(proto); | |
| 205 | ||
| 206 | xasprintf(&uri, "%s%s", proto, n); | |
| 207 | free(n); | |
| 208 | return uri; | |
| 209 | } | |
| 210 | ||
| d7df7ba2 | 211 | #endif |