PageRenderTime 67ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/perf/util/util.h

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