util.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #ifndef GIT_COMPAT_UTIL_H
  2. #define GIT_COMPAT_UTIL_H
  3. #ifndef FLEX_ARRAY
  4. /*
  5. * See if our compiler is known to support flexible array members.
  6. */
  7. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
  8. # define FLEX_ARRAY /* empty */
  9. #elif defined(__GNUC__)
  10. # if (__GNUC__ >= 3)
  11. # define FLEX_ARRAY /* empty */
  12. # else
  13. # define FLEX_ARRAY 0 /* older GNU extension */
  14. # endif
  15. #endif
  16. /*
  17. * Otherwise, default to safer but a bit wasteful traditional style
  18. */
  19. #ifndef FLEX_ARRAY
  20. # define FLEX_ARRAY 1
  21. #endif
  22. #endif
  23. #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
  24. #ifdef __GNUC__
  25. #define TYPEOF(x) (__typeof__(x))
  26. #else
  27. #define TYPEOF(x)
  28. #endif
  29. #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
  30. #define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
  31. /* Approximation of the length of the decimal representation of this type. */
  32. #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
  33. #define _ALL_SOURCE 1
  34. #define _BSD_SOURCE 1
  35. #define HAS_BOOL
  36. #include <unistd.h>
  37. #include <stdio.h>
  38. #include <sys/stat.h>
  39. #include <sys/statfs.h>
  40. #include <fcntl.h>
  41. #include <stdbool.h>
  42. #include <stddef.h>
  43. #include <stdlib.h>
  44. #include <stdarg.h>
  45. #include <string.h>
  46. #include <errno.h>
  47. #include <limits.h>
  48. #include <sys/param.h>
  49. #include <sys/types.h>
  50. #include <dirent.h>
  51. #include <sys/time.h>
  52. #include <time.h>
  53. #include <signal.h>
  54. #include <fnmatch.h>
  55. #include <assert.h>
  56. #include <regex.h>
  57. #include <utime.h>
  58. #include <sys/wait.h>
  59. #include <sys/poll.h>
  60. #include <sys/socket.h>
  61. #include <sys/ioctl.h>
  62. #include <inttypes.h>
  63. #include <linux/magic.h>
  64. #include "types.h"
  65. #include <sys/ttydefaults.h>
  66. #include <lk/debugfs.h>
  67. #include <termios.h>
  68. #include <linux/bitops.h>
  69. extern const char *graph_line;
  70. extern const char *graph_dotted_line;
  71. extern char buildid_dir[];
  72. extern char tracing_events_path[];
  73. extern void perf_debugfs_set_path(const char *mountpoint);
  74. const char *perf_debugfs_mount(const char *mountpoint);
  75. const char *find_tracing_dir(void);
  76. char *get_tracing_file(const char *name);
  77. void put_tracing_file(char *file);
  78. /* On most systems <limits.h> would have given us this, but
  79. * not on some systems (e.g. GNU/Hurd).
  80. */
  81. #ifndef PATH_MAX
  82. #define PATH_MAX 4096
  83. #endif
  84. #ifndef PRIuMAX
  85. #define PRIuMAX "llu"
  86. #endif
  87. #ifndef PRIu32
  88. #define PRIu32 "u"
  89. #endif
  90. #ifndef PRIx32
  91. #define PRIx32 "x"
  92. #endif
  93. #ifndef PATH_SEP
  94. #define PATH_SEP ':'
  95. #endif
  96. #ifndef STRIP_EXTENSION
  97. #define STRIP_EXTENSION ""
  98. #endif
  99. #ifndef has_dos_drive_prefix
  100. #define has_dos_drive_prefix(path) 0
  101. #endif
  102. #ifndef is_dir_sep
  103. #define is_dir_sep(c) ((c) == '/')
  104. #endif
  105. #ifdef __GNUC__
  106. #define NORETURN __attribute__((__noreturn__))
  107. #else
  108. #define NORETURN
  109. #ifndef __attribute__
  110. #define __attribute__(x)
  111. #endif
  112. #endif
  113. #define PERF_GTK_DSO "libperf-gtk.so"
  114. /* General helper functions */
  115. extern void usage(const char *err) NORETURN;
  116. extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
  117. extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
  118. extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
  119. #include "../../../include/linux/stringify.h"
  120. #define DIE_IF(cnd) \
  121. do { if (cnd) \
  122. die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \
  123. __stringify(cnd) "\n"); \
  124. } while (0)
  125. extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
  126. extern int prefixcmp(const char *str, const char *prefix);
  127. extern void set_buildid_dir(void);
  128. extern void disable_buildid_cache(void);
  129. static inline const char *skip_prefix(const char *str, const char *prefix)
  130. {
  131. size_t len = strlen(prefix);
  132. return strncmp(str, prefix, len) ? NULL : str + len;
  133. }
  134. #ifdef __GLIBC_PREREQ
  135. #if __GLIBC_PREREQ(2, 1)
  136. #define HAVE_STRCHRNUL
  137. #endif
  138. #endif
  139. #ifndef HAVE_STRCHRNUL
  140. #define strchrnul gitstrchrnul
  141. static inline char *gitstrchrnul(const char *s, int c)
  142. {
  143. while (*s && *s != c)
  144. s++;
  145. return (char *)s;
  146. }
  147. #endif
  148. /*
  149. * Wrappers:
  150. */
  151. extern char *xstrdup(const char *str);
  152. extern void *xrealloc(void *ptr, size_t size) __attribute__((weak));
  153. static inline void *zalloc(size_t size)
  154. {
  155. return calloc(1, size);
  156. }
  157. static inline int has_extension(const char *filename, const char *ext)
  158. {
  159. size_t len = strlen(filename);
  160. size_t extlen = strlen(ext);
  161. return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
  162. }
  163. /* Sane ctype - no locale, and works with signed chars */
  164. #undef isascii
  165. #undef isspace
  166. #undef isdigit
  167. #undef isxdigit
  168. #undef isalpha
  169. #undef isprint
  170. #undef isalnum
  171. #undef islower
  172. #undef isupper
  173. #undef tolower
  174. #undef toupper
  175. #ifndef NSEC_PER_MSEC
  176. #define NSEC_PER_MSEC 1000000L
  177. #endif
  178. int parse_nsec_time(const char *str, u64 *ptime);
  179. extern unsigned char sane_ctype[256];
  180. #define GIT_SPACE 0x01
  181. #define GIT_DIGIT 0x02
  182. #define GIT_ALPHA 0x04
  183. #define GIT_GLOB_SPECIAL 0x08
  184. #define GIT_REGEX_SPECIAL 0x10
  185. #define GIT_PRINT_EXTRA 0x20
  186. #define GIT_PRINT 0x3E
  187. #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
  188. #define isascii(x) (((x) & ~0x7f) == 0)
  189. #define isspace(x) sane_istest(x,GIT_SPACE)
  190. #define isdigit(x) sane_istest(x,GIT_DIGIT)
  191. #define isxdigit(x) \
  192. (sane_istest(toupper(x), GIT_ALPHA | GIT_DIGIT) && toupper(x) < 'G')
  193. #define isalpha(x) sane_istest(x,GIT_ALPHA)
  194. #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
  195. #define isprint(x) sane_istest(x,GIT_PRINT)
  196. #define islower(x) (sane_istest(x,GIT_ALPHA) && (x & 0x20))
  197. #define isupper(x) (sane_istest(x,GIT_ALPHA) && !(x & 0x20))
  198. #define tolower(x) sane_case((unsigned char)(x), 0x20)
  199. #define toupper(x) sane_case((unsigned char)(x), 0)
  200. static inline int sane_case(int x, int high)
  201. {
  202. if (sane_istest(x, GIT_ALPHA))
  203. x = (x & ~0x20) | high;
  204. return x;
  205. }
  206. int mkdir_p(char *path, mode_t mode);
  207. int copyfile(const char *from, const char *to);
  208. int copyfile_mode(const char *from, const char *to, mode_t mode);
  209. s64 perf_atoll(const char *str);
  210. char **argv_split(const char *str, int *argcp);
  211. void argv_free(char **argv);
  212. bool strglobmatch(const char *str, const char *pat);
  213. bool strlazymatch(const char *str, const char *pat);
  214. int strtailcmp(const char *s1, const char *s2);
  215. char *strxfrchar(char *s, char from, char to);
  216. unsigned long convert_unit(unsigned long value, char *unit);
  217. ssize_t readn(int fd, void *buf, size_t n);
  218. ssize_t writen(int fd, void *buf, size_t n);
  219. struct perf_event_attr;
  220. void event_attr_init(struct perf_event_attr *attr);
  221. #define _STR(x) #x
  222. #define STR(x) _STR(x)
  223. /*
  224. * Determine whether some value is a power of two, where zero is
  225. * *not* considered a power of two.
  226. */
  227. static inline __attribute__((const))
  228. bool is_power_of_2(unsigned long n)
  229. {
  230. return (n != 0 && ((n & (n - 1)) == 0));
  231. }
  232. static inline unsigned next_pow2(unsigned x)
  233. {
  234. if (!x)
  235. return 1;
  236. return 1ULL << (32 - __builtin_clz(x - 1));
  237. }
  238. static inline unsigned long next_pow2_l(unsigned long x)
  239. {
  240. #if BITS_PER_LONG == 64
  241. if (x <= (1UL << 31))
  242. return next_pow2(x);
  243. return (unsigned long)next_pow2(x >> 32) << 32;
  244. #else
  245. return next_pow2(x);
  246. #endif
  247. }
  248. size_t hex_width(u64 v);
  249. int hex2u64(const char *ptr, u64 *val);
  250. char *ltrim(char *s);
  251. char *rtrim(char *s);
  252. void dump_stack(void);
  253. extern unsigned int page_size;
  254. void get_term_dimensions(struct winsize *ws);
  255. struct parse_tag {
  256. char tag;
  257. int mult;
  258. };
  259. unsigned long parse_tag_value(const char *str, struct parse_tag *tags);
  260. #define SRCLINE_UNKNOWN ((char *) "??:0")
  261. struct dso;
  262. char *get_srcline(struct dso *dso, unsigned long addr);
  263. void free_srcline(char *srcline);
  264. int filename__read_int(const char *filename, int *value);
  265. int filename__read_str(const char *filename, char **buf, size_t *sizep);
  266. const char *get_filename_for_perf_kvm(void);
  267. #endif /* GIT_COMPAT_UTIL_H */