]> git.ipfire.org Git - thirdparty/util-linux.git/blame - include/xalloc.h
libmount: don't call hooks after mount.<type> helper
[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
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
25static inline
26__ul_alloc_size(1)
27__ul_returns_nonnull
d7df7ba2
DB
28void *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
37static inline
38__ul_alloc_size(2)
39__ul_returns_nonnull
d7df7ba2
DB
40void *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
aba9e76e
SK
49static inline
50__ul_calloc_size(1, 2)
51__ul_returns_nonnull
d7df7ba2
DB
52void *xcalloc(const size_t nelems, const size_t size)
53{
273ebba1 54 void *ret = calloc(nelems, size);
d7df7ba2 55
273ebba1
SK
56 if (!ret && size && nelems)
57 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
58 return ret;
d7df7ba2
DB
59}
60
aba9e76e
SK
61static inline
62__attribute__((warn_unused_result))
63__ul_returns_nonnull
64char *xstrdup(const char *str)
ecc264bc 65{
273ebba1 66 char *ret;
ecc264bc 67
273ebba1
SK
68 assert(str);
69 ret = strdup(str);
70 if (!ret)
71 err(XALLOC_EXIT_CODE, "cannot duplicate string");
72 return ret;
ecc264bc
KZ
73}
74
aba9e76e
SK
75static inline
76__attribute__((warn_unused_result))
77__ul_returns_nonnull
78char *xstrndup(const char *str, size_t size)
2b88d3d7 79{
273ebba1 80 char *ret;
2b88d3d7 81
273ebba1
SK
82 assert(str);
83 ret = strndup(str, size);
84 if (!ret)
85 err(XALLOC_EXIT_CODE, "cannot duplicate string");
86 return ret;
2b88d3d7
KZ
87}
88
89
aba9e76e
SK
90static inline
91__attribute__((__format__(printf, 2, 3)))
92int xasprintf(char **strp, const char *fmt, ...)
eeb31db9
SK
93{
94 int ret;
95 va_list args;
273ebba1 96
eeb31db9
SK
97 va_start(args, fmt);
98 ret = vasprintf(&(*strp), fmt, args);
99 va_end(args);
100 if (ret < 0)
101 err(XALLOC_EXIT_CODE, "cannot allocate string");
102 return ret;
103}
1b2aa629 104
aba9e76e
SK
105static inline
106__attribute__((__format__(printf, 2, 0)))
107int xvasprintf(char **strp, const char *fmt, va_list ap)
2e6c3a53
KZ
108{
109 int ret = vasprintf(&(*strp), fmt, ap);
273ebba1 110
2e6c3a53
KZ
111 if (ret < 0)
112 err(XALLOC_EXIT_CODE, "cannot allocate string");
113 return ret;
114}
115
1b2aa629 116
aba9e76e
SK
117static inline
118__attribute__((warn_unused_result))
119char *xgethostname(void)
1b2aa629
KZ
120{
121 char *name;
122 size_t sz = get_hostname_max() + 1;
123
124 name = xmalloc(sizeof(char) * sz);
8362545b
KZ
125 if (gethostname(name, sz) != 0) {
126 free(name);
127 return NULL;
128 }
1b2aa629
KZ
129 name[sz - 1] = '\0';
130 return name;
131}
132
d7df7ba2 133#endif