]>
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 | __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 | ||
aba9e76e SK |
73 | static inline |
74 | __attribute__((warn_unused_result)) | |
75 | __ul_returns_nonnull | |
76 | char *xstrdup(const char *str) | |
ecc264bc | 77 | { |
273ebba1 | 78 | char *ret; |
ecc264bc | 79 | |
273ebba1 SK |
80 | assert(str); |
81 | ret = strdup(str); | |
82 | if (!ret) | |
83 | err(XALLOC_EXIT_CODE, "cannot duplicate string"); | |
84 | return ret; | |
ecc264bc KZ |
85 | } |
86 | ||
aba9e76e SK |
87 | static inline |
88 | __attribute__((warn_unused_result)) | |
89 | __ul_returns_nonnull | |
90 | char *xstrndup(const char *str, size_t size) | |
2b88d3d7 | 91 | { |
273ebba1 | 92 | char *ret; |
2b88d3d7 | 93 | |
273ebba1 SK |
94 | assert(str); |
95 | ret = strndup(str, size); | |
96 | if (!ret) | |
97 | err(XALLOC_EXIT_CODE, "cannot duplicate string"); | |
98 | return ret; | |
2b88d3d7 KZ |
99 | } |
100 | ||
101 | ||
aba9e76e SK |
102 | static inline |
103 | __attribute__((__format__(printf, 2, 3))) | |
104 | int xasprintf(char **strp, const char *fmt, ...) | |
eeb31db9 SK |
105 | { |
106 | int ret; | |
107 | va_list args; | |
273ebba1 | 108 | |
eeb31db9 SK |
109 | va_start(args, fmt); |
110 | ret = vasprintf(&(*strp), fmt, args); | |
111 | va_end(args); | |
112 | if (ret < 0) | |
113 | err(XALLOC_EXIT_CODE, "cannot allocate string"); | |
114 | return ret; | |
115 | } | |
1b2aa629 | 116 | |
aba9e76e SK |
117 | static inline |
118 | __attribute__((__format__(printf, 2, 0))) | |
119 | int xvasprintf(char **strp, const char *fmt, va_list ap) | |
2e6c3a53 KZ |
120 | { |
121 | int ret = vasprintf(&(*strp), fmt, ap); | |
273ebba1 | 122 | |
2e6c3a53 KZ |
123 | if (ret < 0) |
124 | err(XALLOC_EXIT_CODE, "cannot allocate string"); | |
125 | return ret; | |
126 | } | |
127 | ||
1b2aa629 | 128 | |
aba9e76e SK |
129 | static inline |
130 | __attribute__((warn_unused_result)) | |
131 | char *xgethostname(void) | |
1b2aa629 KZ |
132 | { |
133 | char *name; | |
134 | size_t sz = get_hostname_max() + 1; | |
135 | ||
136 | name = xmalloc(sizeof(char) * sz); | |
8362545b KZ |
137 | if (gethostname(name, sz) != 0) { |
138 | free(name); | |
139 | return NULL; | |
140 | } | |
1b2aa629 KZ |
141 | name[sz - 1] = '\0'; |
142 | return name; | |
143 | } | |
144 | ||
d7df7ba2 | 145 | #endif |