~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/lib/subcmd/subcmd-util.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: GPL-2.0 */
  2 #ifndef __SUBCMD_UTIL_H
  3 #define __SUBCMD_UTIL_H
  4 
  5 #include <stdarg.h>
  6 #include <stdlib.h>
  7 #include <stdio.h>
  8 #include <linux/compiler.h>
  9 
 10 static inline void report(const char *prefix, const char *err, va_list params)
 11 {
 12         char msg[1024];
 13         vsnprintf(msg, sizeof(msg), err, params);
 14         fprintf(stderr, " %s%s\n", prefix, msg);
 15 }
 16 
 17 static __noreturn inline void die(const char *err, ...)
 18 {
 19         va_list params;
 20 
 21         va_start(params, err);
 22         report(" Fatal: ", err, params);
 23         exit(128);
 24         va_end(params);
 25 }
 26 
 27 #define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
 28 
 29 #define alloc_nr(x) (((x)+16)*3/2)
 30 
 31 /*
 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.
 35  *
 36  * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
 37  */
 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)
 48 
 49 static inline void *xrealloc(void *ptr, size_t size)
 50 {
 51         void *ret = realloc(ptr, size);
 52         if (!ret)
 53                 die("Out of memory, realloc failed");
 54         return ret;
 55 }
 56 
 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);                                                      \
 63 })
 64 
 65 static inline void astrcat(char **out, const char *add)
 66 {
 67         char *tmp = *out;
 68 
 69         if (asprintf(out, "%s%s", tmp ?: "", add) == -1)
 70                 die("asprintf failed");
 71 
 72         free(tmp);
 73 }
 74 
 75 #endif /* __SUBCMD_UTIL_H */
 76 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php