Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / lib / subcmd / subcmd-util.h
blobc742b08815dcbfeb66780f66a1db23c5a0b895f5
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __SUBCMD_UTIL_H
3 #define __SUBCMD_UTIL_H
5 #include <stdarg.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <linux/compiler.h>
10 static inline void report(const char *prefix, const char *err, va_list params)
12 char msg[1024];
13 vsnprintf(msg, sizeof(msg), err, params);
14 fprintf(stderr, " %s%s\n", prefix, msg);
17 static __noreturn inline void die(const char *err, ...)
19 va_list params;
21 va_start(params, err);
22 report(" Fatal: ", err, params);
23 va_end(params);
24 exit(128);
27 #define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
29 #define alloc_nr(x) (((x)+16)*3/2)
32 * Realloc the buffer pointed at by variable 'x' so that it can hold
33 * at least 'nr' entries; the number of entries currently allocated
34 * is 'alloc', using the standard growing factor alloc_nr() macro.
36 * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
38 #define ALLOC_GROW(x, nr, alloc) \
39 do { \
40 if ((nr) > alloc) { \
41 if (alloc_nr(alloc) < (nr)) \
42 alloc = (nr); \
43 else \
44 alloc = alloc_nr(alloc); \
45 x = xrealloc((x), alloc * sizeof(*(x))); \
46 } \
47 } while(0)
49 static inline void *xrealloc(void *ptr, size_t size)
51 void *ret = realloc(ptr, size);
52 if (!ret)
53 die("Out of memory, realloc failed");
54 return ret;
57 #define astrcatf(out, fmt, ...) \
58 ({ \
59 char *tmp = *(out); \
60 if (asprintf((out), "%s" fmt, tmp ?: "", ## __VA_ARGS__) == -1) \
61 die("asprintf failed"); \
62 free(tmp); \
65 static inline void astrcat(char **out, const char *add)
67 char *tmp = *out;
69 if (asprintf(out, "%s%s", tmp ?: "", add) == -1)
70 die("asprintf failed");
72 free(tmp);
75 #endif /* __SUBCMD_UTIL_H */