]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/xalloc.h
setterm: disallow "default" for --ulcolor/--hbcolor
[thirdparty/util-linux.git] / include / xalloc.h
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 *
7 * General memory allocation wrappers for malloc, realloc, calloc and strdup
8 */
9
10 #ifndef UTIL_LINUX_XALLOC_H
11 #define UTIL_LINUX_XALLOC_H
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "c.h"
17
18 #ifndef XALLOC_EXIT_CODE
19 # define XALLOC_EXIT_CODE EXIT_FAILURE
20 #endif
21
22 static inline void __attribute__((__noreturn__))
23 __err_oom(const char *file, unsigned int line)
24 {
25 err(XALLOC_EXIT_CODE, "%s: %u: cannot allocate memory", file, line);
26 }
27
28 #define err_oom() __err_oom(__FILE__, __LINE__)
29
30 static inline __ul_alloc_size(1) __ul_returns_nonnull
31 void *xmalloc(const size_t size)
32 {
33 void *ret = malloc(size);
34
35 if (!ret && size)
36 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
37 return ret;
38 }
39
40 static inline __ul_alloc_size(2) __ul_returns_nonnull
41 void *xrealloc(void *ptr, const size_t size)
42 {
43 void *ret = realloc(ptr, size);
44
45 if (!ret && size)
46 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
47 return ret;
48 }
49
50 static inline __ul_calloc_size(1, 2) __ul_returns_nonnull
51 void *xcalloc(const size_t nelems, const size_t size)
52 {
53 void *ret = calloc(nelems, size);
54
55 if (!ret && size && nelems)
56 err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
57 return ret;
58 }
59
60 static inline char __attribute__((warn_unused_result)) __ul_returns_nonnull
61 *xstrdup(const char *str)
62 {
63 char *ret;
64
65 if (!str)
66 return NULL;
67
68 ret = strdup(str);
69
70 if (!ret)
71 err(XALLOC_EXIT_CODE, "cannot duplicate string");
72 return ret;
73 }
74
75 static inline char * __attribute__((warn_unused_result)) __ul_returns_nonnull
76 xstrndup(const char *str, size_t size)
77 {
78 char *ret;
79
80 if (!str)
81 return NULL;
82
83 ret = strndup(str, size);
84
85 if (!ret)
86 err(XALLOC_EXIT_CODE, "cannot duplicate string");
87 return ret;
88 }
89
90
91 static inline int __attribute__ ((__format__(printf, 2, 3)))
92 xasprintf(char **strp, const char *fmt, ...)
93 {
94 int ret;
95 va_list args;
96 va_start(args, fmt);
97 ret = vasprintf(&(*strp), fmt, args);
98 va_end(args);
99 if (ret < 0)
100 err(XALLOC_EXIT_CODE, "cannot allocate string");
101 return ret;
102 }
103
104 static inline int __attribute__ ((__format__(printf, 2, 0)))
105 xvasprintf(char **strp, const char *fmt, va_list ap)
106 {
107 int ret = vasprintf(&(*strp), fmt, ap);
108 if (ret < 0)
109 err(XALLOC_EXIT_CODE, "cannot allocate string");
110 return ret;
111 }
112
113
114 static inline char * __attribute__((warn_unused_result)) xgethostname(void)
115 {
116 char *name;
117 size_t sz = get_hostname_max() + 1;
118
119 name = xmalloc(sizeof(char) * sz);
120
121 if (gethostname(name, sz) != 0) {
122 free(name);
123 return NULL;
124 }
125 name[sz - 1] = '\0';
126 return name;
127 }
128
129 #endif