filetype: Set "groovy" for Jenkinsfile
[vis.git] / util.h
blob08a44a8e6f75536011652e04fa9d7702e116ebed
1 #ifndef UTIL_H
2 #define UTIL_H
4 #include <stdint.h>
5 #include <stdbool.h>
7 #define LENGTH(x) ((int)(sizeof (x) / sizeof *(x)))
8 #define MIN(a, b) ((a) > (b) ? (b) : (a))
9 #define MAX(a, b) ((a) < (b) ? (b) : (a))
11 /* is c the start of a utf8 sequence? */
12 #define ISUTF8(c) (((c)&0xC0)!=0x80)
13 #define ISASCII(ch) ((unsigned char)ch < 0x80)
15 #if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
16 #define addu __builtin_add_overflow
17 #else
18 static inline bool addu(size_t a, size_t b, size_t *c) {
19 if (SIZE_MAX - a < b)
20 return false;
21 *c = a + b;
22 return true;
24 #endif
26 #if !HAVE_MEMRCHR
27 /* MIT licensed implementation from musl libc */
28 static void *memrchr(const void *m, int c, size_t n)
30 const unsigned char *s = m;
31 c = (unsigned char)c;
32 while (n--) if (s[n]==c) return (void *)(s+n);
33 return 0;
35 #endif
37 /* Needed for building on GNU Hurd */
39 #ifndef PIPE_BUF
40 #define PIPE_BUF 4096
41 #endif
43 #ifndef PATH_MAX
44 #define PATH_MAX 4096
45 #endif
47 #endif /* UTIL_H */