]> git.ipfire.org Git - thirdparty/util-linux.git/blame - include/xalloc.h
Merge branch 'lsfd--close-all' of https://github.com/masatake/util-linux
[thirdparty/util-linux.git] / include / xalloc.h
CommitLineData
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
d4d9744c 16#include <stdio.h>
d7df7ba2 17#include <stdlib.h>
b45fa8b2 18#include <string.h>
d7df7ba2 19
40084d0d 20#include "c.h"
bdd28aba 21#include "strutils.h"
40084d0d 22
3e27b34e
KZ
23#ifndef XALLOC_EXIT_CODE
24# define XALLOC_EXIT_CODE EXIT_FAILURE
25#endif
26
aba9e76e
SK
27static inline
28__ul_alloc_size(1)
29__ul_returns_nonnull
d7df7ba2
DB
30void *xmalloc(const size_t size)
31{
273ebba1 32 void *ret = malloc(size);
d7df7ba2 33
273ebba1
SK
34 if (!ret && size)
35 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
36 return ret;
d7df7ba2
DB
37}
38
aba9e76e
SK
39static inline
40__ul_alloc_size(2)
41__ul_returns_nonnull
d7df7ba2
DB
42void *xrealloc(void *ptr, const size_t size)
43{
273ebba1 44 void *ret = realloc(ptr, size);
d7df7ba2 45
273ebba1
SK
46 if (!ret && size)
47 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
48 return ret;
d7df7ba2
DB
49}
50
3062a58f
MY
51static inline
52__ul_calloc_size(2, 3)
53__ul_returns_nonnull
54void *xreallocarray(void *ptr, const size_t nelems, const size_t size)
55{
56 void *ret = reallocarray(ptr, nelems, size);
57
58 if (!ret && size && nelems)
59 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
60 return ret;
61}
62
aba9e76e
SK
63static inline
64__ul_calloc_size(1, 2)
65__ul_returns_nonnull
d7df7ba2
DB
66void *xcalloc(const size_t nelems, const size_t size)
67{
273ebba1 68 void *ret = calloc(nelems, size);
d7df7ba2 69
273ebba1
SK
70 if (!ret && size && nelems)
71 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
72 return ret;
d7df7ba2
DB
73}
74
aba9e76e
SK
75static inline
76__attribute__((warn_unused_result))
77__ul_returns_nonnull
78char *xstrdup(const char *str)
ecc264bc 79{
273ebba1 80 char *ret;
ecc264bc 81
273ebba1
SK
82 assert(str);
83 ret = strdup(str);
84 if (!ret)
85 err(XALLOC_EXIT_CODE, "cannot duplicate string");
86 return ret;
ecc264bc
KZ
87}
88
aba9e76e
SK
89static inline
90__attribute__((warn_unused_result))
91__ul_returns_nonnull
92char *xstrndup(const char *str, size_t size)
2b88d3d7 93{
273ebba1 94 char *ret;
2b88d3d7 95
273ebba1
SK
96 assert(str);
97 ret = strndup(str, size);
98 if (!ret)
99 err(XALLOC_EXIT_CODE, "cannot duplicate string");
100 return ret;
2b88d3d7
KZ
101}
102
103
aba9e76e
SK
104static inline
105__attribute__((__format__(printf, 2, 3)))
106int xasprintf(char **strp, const char *fmt, ...)
eeb31db9
SK
107{
108 int ret;
109 va_list args;
273ebba1 110
eeb31db9
SK
111 va_start(args, fmt);
112 ret = vasprintf(&(*strp), fmt, args);
113 va_end(args);
114 if (ret < 0)
115 err(XALLOC_EXIT_CODE, "cannot allocate string");
116 return ret;
117}
1b2aa629 118
aba9e76e
SK
119static inline
120__attribute__((__format__(printf, 2, 0)))
121int xvasprintf(char **strp, const char *fmt, va_list ap)
2e6c3a53
KZ
122{
123 int ret = vasprintf(&(*strp), fmt, ap);
273ebba1 124
2e6c3a53
KZ
125 if (ret < 0)
126 err(XALLOC_EXIT_CODE, "cannot allocate string");
127 return ret;
128}
129
bdd28aba
MY
130static inline void xstrappend(char **a, const char *b)
131{
132 if (strappend(a, b) < 0)
133 err(XALLOC_EXIT_CODE, "cannot allocate string");
134}
135
136static inline void xstrputc(char **a, char c)
137{
138 char b[] = {c, '\0'};
139 xstrappend(a, b);
140}
141
142static inline
143__attribute__((__format__(printf, 2, 0)))
144int xstrvfappend(char **a, const char *format, va_list ap)
145{
146 int ret = strvfappend(a, format, ap);
147
148 if (ret < 0)
149 err(XALLOC_EXIT_CODE, "cannot allocate string");
150 return ret;
151
152}
153
154static inline
155__attribute__ ((__format__ (__printf__, 2, 3)))
156int xstrfappend(char **a, const char *format, ...)
157{
158 va_list ap;
159 int ret;
160
161 va_start(ap, format);
162 ret = xstrvfappend(a, format, ap);
163 va_end(ap);
164
165 return ret;
166}
1b2aa629 167
aba9e76e
SK
168static inline
169__attribute__((warn_unused_result))
170char *xgethostname(void)
1b2aa629
KZ
171{
172 char *name;
173 size_t sz = get_hostname_max() + 1;
174
175 name = xmalloc(sizeof(char) * sz);
8362545b
KZ
176 if (gethostname(name, sz) != 0) {
177 free(name);
178 return NULL;
179 }
1b2aa629
KZ
180 name[sz - 1] = '\0';
181 return name;
182}
183
d7df7ba2 184#endif