]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/xalloc.h
Merge branch 'lsclocks/rtc' of https://github.com/t-8ch/util-linux
[thirdparty/util-linux.git] / include / xalloc.h
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-or-later
3 *
4 * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org>
5 * Copyright (C) 2010-2022 Karel Zak <kzak@redhat.com>
6 *
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
9 *
10 * General memory allocation wrappers for malloc, realloc, calloc and strdup
11 */
12
13 #ifndef UTIL_LINUX_XALLOC_H
14 #define UTIL_LINUX_XALLOC_H
15
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "c.h"
20
21 #ifndef XALLOC_EXIT_CODE
22 # define XALLOC_EXIT_CODE EXIT_FAILURE
23 #endif
24
25 static inline
26 __ul_alloc_size(1)
27 __ul_returns_nonnull
28 void *xmalloc(const size_t size)
29 {
30 void *ret = malloc(size);
31
32 if (!ret && size)
33 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
34 return ret;
35 }
36
37 static inline
38 __ul_alloc_size(2)
39 __ul_returns_nonnull
40 void *xrealloc(void *ptr, const size_t size)
41 {
42 void *ret = realloc(ptr, size);
43
44 if (!ret && size)
45 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
46 return ret;
47 }
48
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
61 static inline
62 __ul_calloc_size(1, 2)
63 __ul_returns_nonnull
64 void *xcalloc(const size_t nelems, const size_t size)
65 {
66 void *ret = calloc(nelems, size);
67
68 if (!ret && size && nelems)
69 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
70 return ret;
71 }
72
73 static inline
74 __attribute__((warn_unused_result))
75 __ul_returns_nonnull
76 char *xstrdup(const char *str)
77 {
78 char *ret;
79
80 assert(str);
81 ret = strdup(str);
82 if (!ret)
83 err(XALLOC_EXIT_CODE, "cannot duplicate string");
84 return ret;
85 }
86
87 static inline
88 __attribute__((warn_unused_result))
89 __ul_returns_nonnull
90 char *xstrndup(const char *str, size_t size)
91 {
92 char *ret;
93
94 assert(str);
95 ret = strndup(str, size);
96 if (!ret)
97 err(XALLOC_EXIT_CODE, "cannot duplicate string");
98 return ret;
99 }
100
101
102 static inline
103 __attribute__((__format__(printf, 2, 3)))
104 int xasprintf(char **strp, const char *fmt, ...)
105 {
106 int ret;
107 va_list args;
108
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 }
116
117 static inline
118 __attribute__((__format__(printf, 2, 0)))
119 int xvasprintf(char **strp, const char *fmt, va_list ap)
120 {
121 int ret = vasprintf(&(*strp), fmt, ap);
122
123 if (ret < 0)
124 err(XALLOC_EXIT_CODE, "cannot allocate string");
125 return ret;
126 }
127
128
129 static inline
130 __attribute__((warn_unused_result))
131 char *xgethostname(void)
132 {
133 char *name;
134 size_t sz = get_hostname_max() + 1;
135
136 name = xmalloc(sizeof(char) * sz);
137 if (gethostname(name, sz) != 0) {
138 free(name);
139 return NULL;
140 }
141 name[sz - 1] = '\0';
142 return name;
143 }
144
145 #endif