]>
Commit | Line | Data |
---|---|---|
d7df7ba2 | 1 | /* |
faeb1b64 KZ |
2 | * SPDX-License-Identifier: LGPL-2.1-or-later |
3 | * | |
d7df7ba2 | 4 | * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org> |
faeb1b64 | 5 | * Copyright (C) 2010-2022 Karel Zak <kzak@redhat.com> |
d7df7ba2 DB |
6 | * |
7 | * This file may be redistributed under the terms of the | |
8 | * GNU Lesser General Public License. | |
9 | * | |
8ba013af | 10 | * General memory allocation wrappers for malloc, realloc, calloc and strdup |
d7df7ba2 DB |
11 | */ |
12 | ||
13 | #ifndef UTIL_LINUX_XALLOC_H | |
14 | #define UTIL_LINUX_XALLOC_H | |
15 | ||
16 | #include <stdlib.h> | |
b45fa8b2 | 17 | #include <string.h> |
d7df7ba2 | 18 | |
40084d0d KZ |
19 | #include "c.h" |
20 | ||
3e27b34e KZ |
21 | #ifndef XALLOC_EXIT_CODE |
22 | # define XALLOC_EXIT_CODE EXIT_FAILURE | |
23 | #endif | |
24 | ||
aba9e76e SK |
25 | static inline |
26 | __attribute__((__noreturn__)) | |
27 | void __err_oom(const char *file, unsigned int line) | |
16c9f32f KZ |
28 | { |
29 | err(XALLOC_EXIT_CODE, "%s: %u: cannot allocate memory", file, line); | |
30 | } | |
31 | ||
32 | #define err_oom() __err_oom(__FILE__, __LINE__) | |
33 | ||
aba9e76e SK |
34 | static inline |
35 | __ul_alloc_size(1) | |
36 | __ul_returns_nonnull | |
d7df7ba2 DB |
37 | void *xmalloc(const size_t size) |
38 | { | |
273ebba1 | 39 | void *ret = malloc(size); |
d7df7ba2 | 40 | |
273ebba1 SK |
41 | if (!ret && size) |
42 | err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); | |
43 | return ret; | |
d7df7ba2 DB |
44 | } |
45 | ||
aba9e76e SK |
46 | static inline |
47 | __ul_alloc_size(2) | |
48 | __ul_returns_nonnull | |
d7df7ba2 DB |
49 | void *xrealloc(void *ptr, const size_t size) |
50 | { | |
273ebba1 | 51 | void *ret = realloc(ptr, size); |
d7df7ba2 | 52 | |
273ebba1 SK |
53 | if (!ret && size) |
54 | err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); | |
55 | return ret; | |
d7df7ba2 DB |
56 | } |
57 | ||
aba9e76e SK |
58 | static inline |
59 | __ul_calloc_size(1, 2) | |
60 | __ul_returns_nonnull | |
d7df7ba2 DB |
61 | void *xcalloc(const size_t nelems, const size_t size) |
62 | { | |
273ebba1 | 63 | void *ret = calloc(nelems, size); |
d7df7ba2 | 64 | |
273ebba1 SK |
65 | if (!ret && size && nelems) |
66 | err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size); | |
67 | return ret; | |
d7df7ba2 DB |
68 | } |
69 | ||
aba9e76e SK |
70 | static inline |
71 | __attribute__((warn_unused_result)) | |
72 | __ul_returns_nonnull | |
73 | char *xstrdup(const char *str) | |
ecc264bc | 74 | { |
273ebba1 | 75 | char *ret; |
ecc264bc | 76 | |
273ebba1 SK |
77 | assert(str); |
78 | ret = strdup(str); | |
79 | if (!ret) | |
80 | err(XALLOC_EXIT_CODE, "cannot duplicate string"); | |
81 | return ret; | |
ecc264bc KZ |
82 | } |
83 | ||
aba9e76e SK |
84 | static inline |
85 | __attribute__((warn_unused_result)) | |
86 | __ul_returns_nonnull | |
87 | char *xstrndup(const char *str, size_t size) | |
2b88d3d7 | 88 | { |
273ebba1 | 89 | char *ret; |
2b88d3d7 | 90 | |
273ebba1 SK |
91 | assert(str); |
92 | ret = strndup(str, size); | |
93 | if (!ret) | |
94 | err(XALLOC_EXIT_CODE, "cannot duplicate string"); | |
95 | return ret; | |
2b88d3d7 KZ |
96 | } |
97 | ||
98 | ||
aba9e76e SK |
99 | static inline |
100 | __attribute__((__format__(printf, 2, 3))) | |
101 | int xasprintf(char **strp, const char *fmt, ...) | |
eeb31db9 SK |
102 | { |
103 | int ret; | |
104 | va_list args; | |
273ebba1 | 105 | |
eeb31db9 SK |
106 | va_start(args, fmt); |
107 | ret = vasprintf(&(*strp), fmt, args); | |
108 | va_end(args); | |
109 | if (ret < 0) | |
110 | err(XALLOC_EXIT_CODE, "cannot allocate string"); | |
111 | return ret; | |
112 | } | |
1b2aa629 | 113 | |
aba9e76e SK |
114 | static inline |
115 | __attribute__((__format__(printf, 2, 0))) | |
116 | int xvasprintf(char **strp, const char *fmt, va_list ap) | |
2e6c3a53 KZ |
117 | { |
118 | int ret = vasprintf(&(*strp), fmt, ap); | |
273ebba1 | 119 | |
2e6c3a53 KZ |
120 | if (ret < 0) |
121 | err(XALLOC_EXIT_CODE, "cannot allocate string"); | |
122 | return ret; | |
123 | } | |
124 | ||
1b2aa629 | 125 | |
aba9e76e SK |
126 | static inline |
127 | __attribute__((warn_unused_result)) | |
128 | char *xgethostname(void) | |
1b2aa629 KZ |
129 | { |
130 | char *name; | |
131 | size_t sz = get_hostname_max() + 1; | |
132 | ||
133 | name = xmalloc(sizeof(char) * sz); | |
8362545b KZ |
134 | if (gethostname(name, sz) != 0) { |
135 | free(name); | |
136 | return NULL; | |
137 | } | |
1b2aa629 KZ |
138 | name[sz - 1] = '\0'; |
139 | return name; | |
140 | } | |
141 | ||
d7df7ba2 | 142 | #endif |