• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * erofs-utils/include/erofs/defs.h
4  *
5  * Copyright (C) 2018 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Li Guifu <bluce.liguifu@huawei.com>
8  * Modified by Gao Xiang <gaoxiang25@huawei.com>
9  */
10 #ifndef __EROFS_DEFS_H
11 #define __EROFS_DEFS_H
12 
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <assert.h>
16 #include <inttypes.h>
17 #include <limits.h>
18 #include <stdbool.h>
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #ifdef HAVE_LINUX_TYPES_H
25 #include <linux/types.h>
26 #endif
27 
28 /*
29  * container_of - cast a member of a structure out to the containing structure
30  * @ptr:	the pointer to the member.
31  * @type:	the type of the container struct this is embedded in.
32  * @member:	the name of the member within the struct.
33  */
34 #define container_of(ptr, type, member) ({			\
35 	const typeof(((type *)0)->member) *__mptr = (ptr);	\
36 	(type *)((char *)__mptr - offsetof(type, member)); })
37 
38 typedef uint8_t         u8;
39 typedef uint16_t        u16;
40 typedef uint32_t        u32;
41 typedef uint64_t        u64;
42 
43 #ifndef HAVE_LINUX_TYPES_H
44 typedef u8	__u8;
45 typedef u16	__u16;
46 typedef u32	__u32;
47 typedef u64	__u64;
48 typedef u16	__le16;
49 typedef u32	__le32;
50 typedef u64	__le64;
51 typedef u16	__be16;
52 typedef u32	__be32;
53 typedef u64	__be64;
54 #endif
55 
56 typedef int8_t          s8;
57 typedef int16_t         s16;
58 typedef int32_t         s32;
59 typedef int64_t         s64;
60 
61 
62 #if __BYTE_ORDER == __LITTLE_ENDIAN
63 /*
64  * The host byte order is the same as network byte order,
65  * so these functions are all just identity.
66  */
67 #define cpu_to_le16(x) ((__u16)(x))
68 #define cpu_to_le32(x) ((__u32)(x))
69 #define cpu_to_le64(x) ((__u64)(x))
70 #define le16_to_cpu(x) ((__u16)(x))
71 #define le32_to_cpu(x) ((__u32)(x))
72 #define le64_to_cpu(x) ((__u64)(x))
73 
74 #else
75 #if __BYTE_ORDER == __BIG_ENDIAN
76 #define cpu_to_le16(x) (__builtin_bswap16(x))
77 #define cpu_to_le32(x) (__builtin_bswap32(x))
78 #define cpu_to_le64(x) (__builtin_bswap64(x))
79 #define le16_to_cpu(x) (__builtin_bswap16(x))
80 #define le32_to_cpu(x) (__builtin_bswap32(x))
81 #define le64_to_cpu(x) (__builtin_bswap64(x))
82 #else
83 #pragma error
84 #endif
85 #endif
86 
87 #ifndef __OPTIMIZE__
88 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
89 #else
90 #define BUILD_BUG_ON(condition) assert(condition)
91 #endif
92 
93 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
94 
95 #define __round_mask(x, y)      ((__typeof__(x))((y)-1))
96 #define round_up(x, y)          ((((x)-1) | __round_mask(x, y))+1)
97 #define round_down(x, y)        ((x) & ~__round_mask(x, y))
98 
99 /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
100 #define roundup(x, y) (					\
101 {							\
102 	const typeof(y) __y = y;			\
103 	(((x) + (__y - 1)) / __y) * __y;		\
104 }							\
105 )
106 #define rounddown(x, y) (				\
107 {							\
108 	typeof(x) __x = (x);				\
109 	__x - (__x % (y));				\
110 }							\
111 )
112 
113 #define min(x, y) ({				\
114 	typeof(x) _min1 = (x);			\
115 	typeof(y) _min2 = (y);			\
116 	(void) (&_min1 == &_min2);		\
117 	_min1 < _min2 ? _min1 : _min2; })
118 
119 #define max(x, y) ({				\
120 	typeof(x) _max1 = (x);			\
121 	typeof(y) _max2 = (y);			\
122 	(void) (&_max1 == &_max2);		\
123 	_max1 > _max2 ? _max1 : _max2; })
124 
125 /*
126  * ..and if you can't take the strict types, you can specify one yourself.
127  * Or don't use min/max at all, of course.
128  */
129 #define min_t(type, x, y) ({			\
130 	type __min1 = (x);			\
131 	type __min2 = (y);			\
132 	__min1 < __min2 ? __min1: __min2; })
133 
134 #define max_t(type, x, y) ({			\
135 	type __max1 = (x);			\
136 	type __max2 = (y);			\
137 	__max1 > __max2 ? __max1: __max2; })
138 
139 #define cmpsgn(x, y) ({		\
140 	typeof(x) _x = (x);	\
141 	typeof(y) _y = (y);	\
142 	(_x > _y) - (_x < _y); })
143 
144 #define ARRAY_SIZE(arr)	(sizeof(arr) / sizeof((arr)[0]))
145 
146 #define BIT(nr)             (1UL << (nr))
147 #define BIT_ULL(nr)         (1ULL << (nr))
148 #define BIT_MASK(nr)        (1UL << ((nr) % BITS_PER_LONG))
149 #define BIT_WORD(nr)        ((nr) / BITS_PER_LONG)
150 #define BIT_ULL_MASK(nr)    (1ULL << ((nr) % BITS_PER_LONG_LONG))
151 #define BIT_ULL_WORD(nr)    ((nr) / BITS_PER_LONG_LONG)
152 #define BITS_PER_BYTE       8
153 #define BITS_TO_LONGS(nr)   DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
154 
155 #ifdef __SIZEOF_LONG__
156 #define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
157 #else
158 #define BITS_PER_LONG __WORDSIZE
159 #endif
160 
161 #define BUG_ON(cond)        assert(!(cond))
162 
163 #ifdef NDEBUG
164 #define DBG_BUGON(condition)	((void)(condition))
165 #else
166 #define DBG_BUGON(condition)	BUG_ON(condition)
167 #endif
168 
169 #ifndef __maybe_unused
170 #define __maybe_unused      __attribute__((__unused__))
171 #endif
172 
get_unaligned_le32(const u8 * p)173 static inline u32 get_unaligned_le32(const u8 *p)
174 {
175 	return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
176 }
177 
178 #endif
179 
180