perf tools: Add trace event information parser
[deliverable/linux.git] / tools / perf / util / util.h
CommitLineData
07800601
IM
1#ifndef GIT_COMPAT_UTIL_H
2#define GIT_COMPAT_UTIL_H
3
4#define _FILE_OFFSET_BITS 64
5
6#ifndef FLEX_ARRAY
7/*
8 * See if our compiler is known to support flexible array members.
9 */
10#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
11# define FLEX_ARRAY /* empty */
12#elif defined(__GNUC__)
13# if (__GNUC__ >= 3)
14# define FLEX_ARRAY /* empty */
15# else
16# define FLEX_ARRAY 0 /* older GNU extension */
17# endif
18#endif
19
20/*
21 * Otherwise, default to safer but a bit wasteful traditional style
22 */
23#ifndef FLEX_ARRAY
24# define FLEX_ARRAY 1
25#endif
26#endif
27
28#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
29
30#ifdef __GNUC__
31#define TYPEOF(x) (__typeof__(x))
32#else
33#define TYPEOF(x)
34#endif
35
36#define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
37#define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
38
39/* Approximation of the length of the decimal representation of this type. */
40#define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
41
42#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && !defined(_M_UNIX)
43#define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
44#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
45#endif
46#define _ALL_SOURCE 1
47#define _GNU_SOURCE 1
48#define _BSD_SOURCE 1
49
50#include <unistd.h>
51#include <stdio.h>
52#include <sys/stat.h>
f6bdafef 53#include <sys/statfs.h>
07800601
IM
54#include <fcntl.h>
55#include <stddef.h>
56#include <stdlib.h>
57#include <stdarg.h>
58#include <string.h>
59#include <errno.h>
60#include <limits.h>
61#include <sys/param.h>
62#include <sys/types.h>
63#include <dirent.h>
64#include <sys/time.h>
65#include <time.h>
66#include <signal.h>
67#include <fnmatch.h>
68#include <assert.h>
69#include <regex.h>
70#include <utime.h>
07800601
IM
71#include <sys/wait.h>
72#include <sys/poll.h>
73#include <sys/socket.h>
74#include <sys/ioctl.h>
75#ifndef NO_SYS_SELECT_H
76#include <sys/select.h>
77#endif
78#include <netinet/in.h>
79#include <netinet/tcp.h>
80#include <arpa/inet.h>
81#include <netdb.h>
82#include <pwd.h>
83#include <inttypes.h>
f6bdafef 84#include "../../../include/linux/magic.h"
07800601 85
1fe2c106 86
07800601
IM
87#ifndef NO_ICONV
88#include <iconv.h>
89#endif
90
07800601
IM
91/* On most systems <limits.h> would have given us this, but
92 * not on some systems (e.g. GNU/Hurd).
93 */
94#ifndef PATH_MAX
95#define PATH_MAX 4096
96#endif
97
98#ifndef PRIuMAX
99#define PRIuMAX "llu"
100#endif
101
102#ifndef PRIu32
103#define PRIu32 "u"
104#endif
105
106#ifndef PRIx32
107#define PRIx32 "x"
108#endif
109
110#ifndef PATH_SEP
111#define PATH_SEP ':'
112#endif
113
114#ifndef STRIP_EXTENSION
115#define STRIP_EXTENSION ""
116#endif
117
118#ifndef has_dos_drive_prefix
119#define has_dos_drive_prefix(path) 0
120#endif
121
122#ifndef is_dir_sep
123#define is_dir_sep(c) ((c) == '/')
124#endif
125
126#ifdef __GNUC__
127#define NORETURN __attribute__((__noreturn__))
128#else
129#define NORETURN
130#ifndef __attribute__
131#define __attribute__(x)
132#endif
133#endif
134
135/* General helper functions */
136extern void usage(const char *err) NORETURN;
137extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
138extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
139extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
140
141extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
142
143extern int prefixcmp(const char *str, const char *prefix);
144extern time_t tm_to_time_t(const struct tm *tm);
145
146static inline const char *skip_prefix(const char *str, const char *prefix)
147{
148 size_t len = strlen(prefix);
149 return strncmp(str, prefix, len) ? NULL : str + len;
150}
151
152#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
153
154#ifndef PROT_READ
155#define PROT_READ 1
156#define PROT_WRITE 2
157#define MAP_PRIVATE 1
158#define MAP_FAILED ((void*)-1)
159#endif
160
161#define mmap git_mmap
162#define munmap git_munmap
163extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
164extern int git_munmap(void *start, size_t length);
165
166#else /* NO_MMAP || USE_WIN32_MMAP */
167
168#include <sys/mman.h>
169
170#endif /* NO_MMAP || USE_WIN32_MMAP */
171
172#ifdef NO_MMAP
173
174/* This value must be multiple of (pagesize * 2) */
175#define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
176
177#else /* NO_MMAP */
178
179/* This value must be multiple of (pagesize * 2) */
180#define DEFAULT_PACKED_GIT_WINDOW_SIZE \
181 (sizeof(void*) >= 8 \
182 ? 1 * 1024 * 1024 * 1024 \
183 : 32 * 1024 * 1024)
184
185#endif /* NO_MMAP */
186
187#ifdef NO_ST_BLOCKS_IN_STRUCT_STAT
188#define on_disk_bytes(st) ((st).st_size)
189#else
190#define on_disk_bytes(st) ((st).st_blocks * 512)
191#endif
192
193#define DEFAULT_PACKED_GIT_LIMIT \
194 ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
195
196#ifdef NO_PREAD
197#define pread git_pread
198extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
199#endif
200/*
201 * Forward decl that will remind us if its twin in cache.h changes.
202 * This function is used in compat/pread.c. But we can't include
203 * cache.h there.
204 */
205extern ssize_t read_in_full(int fd, void *buf, size_t count);
206
207#ifdef NO_SETENV
208#define setenv gitsetenv
209extern int gitsetenv(const char *, const char *, int);
210#endif
211
212#ifdef NO_MKDTEMP
213#define mkdtemp gitmkdtemp
214extern char *gitmkdtemp(char *);
215#endif
216
217#ifdef NO_UNSETENV
218#define unsetenv gitunsetenv
219extern void gitunsetenv(const char *);
220#endif
221
222#ifdef NO_STRCASESTR
223#define strcasestr gitstrcasestr
224extern char *gitstrcasestr(const char *haystack, const char *needle);
225#endif
226
227#ifdef NO_STRLCPY
228#define strlcpy gitstrlcpy
229extern size_t gitstrlcpy(char *, const char *, size_t);
230#endif
231
232#ifdef NO_STRTOUMAX
233#define strtoumax gitstrtoumax
234extern uintmax_t gitstrtoumax(const char *, char **, int);
235#endif
236
237#ifdef NO_HSTRERROR
238#define hstrerror githstrerror
239extern const char *githstrerror(int herror);
240#endif
241
242#ifdef NO_MEMMEM
243#define memmem gitmemmem
244void *gitmemmem(const void *haystack, size_t haystacklen,
245 const void *needle, size_t needlelen);
246#endif
247
248#ifdef FREAD_READS_DIRECTORIES
249#ifdef fopen
250#undef fopen
251#endif
252#define fopen(a,b) git_fopen(a,b)
253extern FILE *git_fopen(const char*, const char*);
254#endif
255
256#ifdef SNPRINTF_RETURNS_BOGUS
257#define snprintf git_snprintf
258extern int git_snprintf(char *str, size_t maxsize,
259 const char *format, ...);
260#define vsnprintf git_vsnprintf
261extern int git_vsnprintf(char *str, size_t maxsize,
262 const char *format, va_list ap);
263#endif
264
265#ifdef __GLIBC_PREREQ
266#if __GLIBC_PREREQ(2, 1)
267#define HAVE_STRCHRNUL
268#endif
269#endif
270
271#ifndef HAVE_STRCHRNUL
272#define strchrnul gitstrchrnul
273static inline char *gitstrchrnul(const char *s, int c)
274{
275 while (*s && *s != c)
276 s++;
277 return (char *)s;
278}
279#endif
280
6f06ccbc
IM
281/*
282 * Wrappers:
283 */
284extern char *xstrdup(const char *str);
285extern void *xmalloc(size_t size);
286extern void *xmemdupz(const void *data, size_t len);
287extern char *xstrndup(const char *str, size_t len);
288extern void *xrealloc(void *ptr, size_t size);
289extern void *xcalloc(size_t nmemb, size_t size);
290extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
291extern ssize_t xread(int fd, void *buf, size_t len);
292extern ssize_t xwrite(int fd, const void *buf, size_t len);
293extern int xdup(int fd);
294extern FILE *xfdopen(int fd, const char *mode);
16f762a2
IM
295extern int xmkstemp(char *template);
296
07800601
IM
297static inline size_t xsize_t(off_t len)
298{
299 return (size_t)len;
300}
301
302static inline int has_extension(const char *filename, const char *ext)
303{
304 size_t len = strlen(filename);
305 size_t extlen = strlen(ext);
306 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
307}
308
309/* Sane ctype - no locale, and works with signed chars */
310#undef isascii
311#undef isspace
312#undef isdigit
313#undef isalpha
314#undef isalnum
315#undef tolower
316#undef toupper
317extern unsigned char sane_ctype[256];
a73c7d84
PZ
318#define GIT_SPACE 0x01
319#define GIT_DIGIT 0x02
320#define GIT_ALPHA 0x04
321#define GIT_GLOB_SPECIAL 0x08
322#define GIT_REGEX_SPECIAL 0x10
323#define GIT_PRINT_EXTRA 0x20
324#define GIT_PRINT 0x3E
07800601
IM
325#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
326#define isascii(x) (((x) & ~0x7f) == 0)
327#define isspace(x) sane_istest(x,GIT_SPACE)
328#define isdigit(x) sane_istest(x,GIT_DIGIT)
329#define isalpha(x) sane_istest(x,GIT_ALPHA)
330#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
a73c7d84 331#define isprint(x) sane_istest(x,GIT_PRINT)
07800601
IM
332#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
333#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
334#define tolower(x) sane_case((unsigned char)(x), 0x20)
335#define toupper(x) sane_case((unsigned char)(x), 0)
336
337static inline int sane_case(int x, int high)
338{
339 if (sane_istest(x, GIT_ALPHA))
340 x = (x & ~0x20) | high;
341 return x;
342}
343
344static inline int strtoul_ui(char const *s, int base, unsigned int *result)
345{
346 unsigned long ul;
347 char *p;
348
349 errno = 0;
350 ul = strtoul(s, &p, base);
351 if (errno || *p || p == s || (unsigned int) ul != ul)
352 return -1;
353 *result = ul;
354 return 0;
355}
356
357static inline int strtol_i(char const *s, int base, int *result)
358{
359 long ul;
360 char *p;
361
362 errno = 0;
363 ul = strtol(s, &p, base);
364 if (errno || *p || p == s || (int) ul != ul)
365 return -1;
366 *result = ul;
367 return 0;
368}
369
370#ifdef INTERNAL_QSORT
371void git_qsort(void *base, size_t nmemb, size_t size,
372 int(*compar)(const void *, const void *));
373#define qsort git_qsort
374#endif
375
376#ifndef DIR_HAS_BSD_GROUP_SEMANTICS
377# define FORCE_DIR_SET_GID S_ISGID
378#else
379# define FORCE_DIR_SET_GID 0
380#endif
381
382#ifdef NO_NSEC
383#undef USE_NSEC
384#define ST_CTIME_NSEC(st) 0
385#define ST_MTIME_NSEC(st) 0
386#else
387#ifdef USE_ST_TIMESPEC
388#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))
389#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))
390#else
391#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))
392#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))
393#endif
394#endif
395
396#endif
This page took 0.060974 seconds and 5 git commands to generate.