-
Notifications
You must be signed in to change notification settings - Fork 67
/
compat.h
87 lines (68 loc) · 2.04 KB
/
compat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifdef __FreeBSD__
# include <sys/endian.h>
#elif defined(__APPLE_CC_) || defined(__MACH__) /* MacOS/X support */
# include <machine/endian.h>
#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
# define htole16(x) (x)
# define le32toh(x) (x)
#elif __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN
# define htole16(x) __DARWIN_OSSwapInt16(x)
# define le32toh(x) __DARWIN_OSSwapInt32(x)
#else
# error "Endianness is undefined"
#endif
#elif !defined(__WIN32__)
# include <endian.h>
#endif
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <limits.h>
#ifndef __WIN32__
#include <sys/uio.h>
#endif
#ifdef __ANDROID__
#define le32toh letoh32
#endif
#if defined(__WIN32__) && defined(SG)
struct iovec {
void *iov_base; /* Pointer to data. */
size_t iov_len; /* Length of data. */
};
#endif
#define get_unaligned_memcpy(x) ({ \
typeof(*(x)) _ret; \
memcpy(&_ret, (x), sizeof(*(x))); \
_ret; })
#define put_unaligned_memcpy(v,x) ({ \
typeof((v)) _v = (v); \
memcpy((x), &_v, sizeof(*(x))); })
#define get_unaligned get_unaligned_memcpy
#define put_unaligned put_unaligned_memcpy
#define get_unaligned64 get_unaligned_memcpy
#define put_unaligned64 put_unaligned_memcpy
#define get_unaligned_le32(x) (le32toh(get_unaligned((u32 *)(x))))
#define put_unaligned_le16(v,x) (put_unaligned(htole16(v), (u16 *)(x)))
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned u32;
typedef unsigned long long u64;
#define BUG_ON(x) assert(!(x))
#define vmalloc(x) malloc(x)
#define vfree(x) free(x)
#define EXPORT_SYMBOL(x)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
#define min_t(t,x,y) ((x) < (y) ? (x) : (y))
#define max_t(t,x,y) ((x) > (y) ? (x) : (y))
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define __LITTLE_ENDIAN__ 1
#endif
#if __LITTLE_ENDIAN__ == 1 && (defined(__LSB_VERSION__) || defined(__WIN32__))
#define htole16(x) (x)
#define le32toh(x) (x)
#endif
#define BITS_PER_LONG (__SIZEOF_LONG__ * 8)