]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - include/xalloc.h
c620a4921dd2998707c585b62b3cca23722b00d1
2 * No copyright is claimed. This code is in the public domain; do with
5 * Authors 2010 Davidlohr Bueso <dave@gnu.org>
6 * 2010-2025 Karel Zak <kzak@redhat.com>
8 * General memory allocation wrappers for malloc, realloc, calloc and strdup
11 #ifndef UTIL_LINUX_XALLOC_H
12 #define UTIL_LINUX_XALLOC_H
21 #ifndef XALLOC_EXIT_CODE
22 # define XALLOC_EXIT_CODE EXIT_FAILURE
28 void *xmalloc(const size_t size
)
30 void *ret
= malloc(size
);
33 err(XALLOC_EXIT_CODE
, "cannot allocate %zu bytes", size
);
40 void *xrealloc(void *ptr
, const size_t size
)
42 void *ret
= realloc(ptr
, size
);
45 err(XALLOC_EXIT_CODE
, "cannot allocate %zu bytes", size
);
50 __ul_calloc_size(2, 3)
52 void *xreallocarray(void *ptr
, const size_t nelems
, const size_t size
)
54 void *ret
= reallocarray(ptr
, nelems
, size
);
56 if (!ret
&& size
&& nelems
)
57 err(XALLOC_EXIT_CODE
, "cannot allocate %zu bytes", size
);
62 __ul_calloc_size(1, 2)
64 void *xcalloc(const size_t nelems
, const size_t size
)
66 void *ret
= calloc(nelems
, size
);
68 if (!ret
&& size
&& nelems
)
69 err(XALLOC_EXIT_CODE
, "cannot allocate %zu bytes", size
);
74 __attribute__((warn_unused_result
))
77 void *xmemdup(const void *ptr
, size_t size
)
79 void *ret
= xmalloc(size
);
81 memcpy(ret
, ptr
, size
);
86 __attribute__((warn_unused_result
))
88 char *xstrdup(const char *str
)
95 err(XALLOC_EXIT_CODE
, "cannot duplicate string");
100 __attribute__((warn_unused_result
))
102 char *xstrndup(const char *str
, size_t size
)
107 ret
= strndup(str
, size
);
109 err(XALLOC_EXIT_CODE
, "cannot duplicate string");
115 __attribute__((__format__(printf
, 2, 3)))
116 int xasprintf(char **strp
, const char *fmt
, ...)
122 ret
= vasprintf(&(*strp
), fmt
, args
);
125 err(XALLOC_EXIT_CODE
, "cannot allocate string");
130 __attribute__((__format__(printf
, 2, 0)))
131 int xvasprintf(char **strp
, const char *fmt
, va_list ap
)
133 int ret
= vasprintf(&(*strp
), fmt
, ap
);
136 err(XALLOC_EXIT_CODE
, "cannot allocate string");
140 static inline void xstrappend(char **a
, const char *b
)
142 if (ul_strappend(a
, b
) < 0)
143 err(XALLOC_EXIT_CODE
, "cannot allocate string");
146 static inline void xstrputc(char **a
, char c
)
148 char b
[] = {c
, '\0'};
153 __attribute__((__format__(printf
, 2, 0)))
154 int xstrvfappend(char **a
, const char *format
, va_list ap
)
156 int ret
= ul_strvfappend(a
, format
, ap
);
159 err(XALLOC_EXIT_CODE
, "cannot allocate string");
165 __attribute__ ((__format__ (__printf__
, 2, 3)))
166 int xstrfappend(char **a
, const char *format
, ...)
171 va_start(ap
, format
);
172 ret
= xstrvfappend(a
, format
, ap
);
179 __attribute__((warn_unused_result
))
180 char *xgethostname(void)
183 size_t sz
= get_hostname_max() + 1;
185 name
= xmalloc(sizeof(char) * sz
);
186 if (gethostname(name
, sz
) != 0) {
195 __attribute__((warn_unused_result
))
196 char *xgethosturi(const char *proto
)
198 char *n
= xgethostname();
204 return xstrdup(proto
);
206 xasprintf(&uri
, "%s%s", proto
, n
);