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