forked from gatieme/AderXCoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
339 lines (247 loc) · 12 KB
/
common.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Author: HIT CS HDMC team.
* Last modified by zhaozhilong: 2010-1-14
*/
#ifndef _COMMON_H_
#define _COMMON_H_
#ifdef __cplusplus
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#else
//C header files
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#endif
/// 当前字符是数字
#define IsDigit(c) (c >= '0' && c <= '9')
/// 当前字符是8进制数据
#define IsOctDigit(c) (c >= '0' && c <= '7')
/// 当前字符是16进制数据
#define IsHexDigit(c) (IsDigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
/// 当前字符是字母或者_
#define IsLetter(c) ((c >= 'a' && c <= 'z') || (c == '_') || (c >= 'A' && c <= 'Z'))
/// 当前自负是否满足C的变量命名规则
#define IsLetterOrDigit(c) (IsLetter(c) || IsDigit(c))
#define IsIdentifier IsLetterOrDigit
/// 当前字符是空白字符
#define IsSpace(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
/// 取出当前信息的高4位
#define HIGH_4BIT(v) ((v) >> (8 * sizeof(int) - 4) & 0x0f)
/// 取出当前信息的高3位
#define HIGH_3BIT(v) ((v) >> (8 * sizeof(int) - 3) & 0x07)
/// 取出当前信息的高1位
#define HIGH_1BIT(v) ((v) >> (8 * sizeof(int) - 1) & 0x01)
#define ALIGN(size, align) ((size + align - 1) & (~(align - 1)))
#define BitGet(number, pos) ((number) >> (pos) & 1) /// 用宏得到某数的某位
#define BitSet(number, pos) ((number) |= 1 << (pos)) /// 把某位置1
#define BitClr(number, pos) ((number) &= ~(1 << (pos))) /// 把某位清0
#define BitCpl(number, pos) ((number) ^= 1 << (pos)) /// 把number的POS位取反
///03: 得到指定地址上的一个字节或字
#define MEM_B(x) (*((byte *)(x)))
#define MEM_W(x) (*((word *)(x)))
///04: 求最大值和最小值
#define MAX(x,y) (((x)>(y)) ? (x) : (y))
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
///05: 得到一个field在结构体(struct)中的偏移量
#define FPOS(type,field) ((dword)&((type *)0)->field)
///06: 得到一个结构体中field所占用的字节数
#define FSIZ(type,field) sizeof(((type *)0)->field)
///07: 按照LSB格式把两个字节转化为一个Word
#define FLIPW(ray) ((((word)(ray)[0]) * 256) + (ray)[1])
///08: 按照LSB格式把一个Word转化为两个字节
#define FLOPW(ray,val) (ray)[0] = ((val)/256); (ray)[1] = ((val) & 0xFF)
///09: 得到一个变量的地址(word宽度)
#define B_PTR(var) ((byte *) (void *) &(var))
#define W_PTR(var) ((word *) (void *) &(var))
///10: 得到一个字的高位和低位字节
#define WORD_LO(xxx) ((byte) ((word)(xxx) & 255))
#define WORD_HI(xxx) ((byte) ((word)(xxx) >> 8))
///11: 返回一个比X大的最接近的8的倍数
#define RND8(x) ((((x) + 7)/8) * 8)
///12: 将一个字母转换为大写
#define UPCASE(c) (((c)>='a' && (c) <= 'z') ? ((c) - 0x20) : (c))
#define ToUpper(c) (c & ~0x20)
///13: 判断字符是不是10进值的数字
#define DECCHK(c) ((c)>='0' && (c)<='9')
///14: 判断字符是不是16进值的数字
#define HEXCHK(c) (((c) >= '0' && (c)<='9') ((c)>='A' && (c)<= 'F') \
((c)>='a' && (c)<='f'))
///15: 防止溢出的一个方法
#define INC_SAT(val) (val=((val)+1>(val)) ? (val)+1 : (val))
///16: 返回数组元素的个数
#define ARR_SIZE(a) (sizeof((a))/sizeof((a[0])))
///17: 返回一个无符号数n尾的值MOD_BY_POWER_OF_TWO(X,n)=X%(2^n)
#define MOD_BY_POWER_OF_TWO( val, mod_by ) ((dword)(val) & (dword)((mod_by)-1))
///18: 对于IO空间映射在存储空间的结构,输入输出处理
#define inp(port) (*((volatile byte *)(port)))
#define inpw(port) (*((volatile word *)(port)))
#define inpdw(port) (*((volatile dword *)(port)))
#define outp(port,val) (*((volatile byte *)(port))=((byte)(val)))
#define outpw(port, val) (*((volatile word *)(port))=((word)(val)))
#define outpdw(port, val) (*((volatile dword *)(port))=((dword)(val)))
//#define DEBUG
//#define MMAP_INVALID_ARGUMENT /* invalid argument when mmap */
/**
* Macros to help with debugging. Set SCULL_DEBUG to 1 enable
* debugging (which you can do from the Makefile); these macros work
* in both kernelspace and userspace.
*/
/* undef it, just in case someone else defined it. */
#ifdef dbgprint
#undef dbgprint
#endif // dbgprint
#ifdef dprint
#undef dprint
#endif // dprint
#ifdef dout
#undef dount
#endif // dout
/**when you define DEBUG macro
dbgprint to use printk with line and funcitonname in kernel
dprint to use print without line and funcitonname in kernel
dbgprint to use printf with line and funcitonname in userspace
dprint to use printf without line and funcitonname in userspace
it's the same to dbgcout and dcout in userspace
*/
#ifdef DEBUG
#if defined(dbgprint) || defined(dprint) || defined(dprintk)
#error "dbgprint, dprint or dprintk has been defined already..."
#endif
#ifdef __KERNEL__ /* in kernel modules */
#if defined(__FILE__) && defined(__LINE__)
//printk with line and function name
#define dbgprint(format, args...) \
printk(KERN_INFO "[%20s, %4d] : "format, __FILE__, __LINE__, ##args)
#endif // ifdef defined(__FILE__) && defined(__LINE__)
//printk without line and function name
#define dprint(format,args...) printk(KERN_INFO format, ##args)
#define dprintk(format,args...) printk(KERN_INFO format, ##args)
#else // userspace
#if defined(dbgprint) || defined(dprint) || defined(dprintf)
#error "dbgprint, dprint or dprintk has been defined already..."
#endif
//////////
/// printf for C language
///
/// dbgprint PRINT "[file, line], information
///
/// dprint PRINT "information"
/// dprintf PRINT "information"
//////////
/* Debugging is on and we are in userspace. */
#define dprint(format, args...) printf(format, ## args)
#define dprintf(format, args...) printf(format, ## args)
#if defined(__FILE__) && defined(__LINE__)
//printf with line and function name
#define dbgprint(format, args...) \
printf("[%20s,%4d] : "format, __FILE__, __LINE__, ##args)
#endif // ifdef defined(__FILE__) && defined(__LINE__)
//////////
/// cout for C++ language
//
// dbgout PRINT "[file, line], information
//
// dcout PRINT "information"
// debug PRINT "information"
//////////
#ifdef __cplusplus
#if defined(dbgout) || defined(cout) || defined(debug)
#error "dbgprint, dprint or dprintk has been defined already..."
#endif
#if defined(__FILE__) && defined(__LINE__)
#define dbgcout std::cout <<"[" <<std::setw(20)<<__FILE__ <<"," <<std::setw(4)<<__LINE__ <<"] : "
#endif
#define dcout cout
#define debug cout
#endif // __cplusplus
#ifdef __tmain
#error "__tmain has been defined already"
#endif
#define __tmain main
#endif // __KERNEL__ kernel and userspace
#else /* Not debugging: do nothing. */
#define dbgprint(format,args...)
#define dprint(format, args...)
#define dprintk(format,args...)
#define dprintf(format, args...)
#ifdef __cplusplus
#define dcout 0 && cout
#define dbgcout 0 && cout
#define debug 0 && cout
#endif
#endif
/* PDEBUGG is a placeholder that makes it easy to "comment out" the debugging
statements without deleting them. */
#undef undprint
#define undprint(format, args...)
#undef undcout
#define undcout 0 && count
#undef undbgprint
#define undprint(format, args...)
#undef undbgcout
#define undbgcout 0 && cout
//////////////////////////////////////////////////////////////////////////////////////////////
#define SHOW_FILE_NAME(fileName) do \
{ int i = strlen(fileName) - 1; \
for( ; fileName[i] != '\\' \
&& fileName[i] != '/' && i >= 0; i--); \
char *fname = malloc(strlen(fileName) - i); \
strcpy(fname, fileName + i + 1); \
printf("File: %s ", fname); \
free(fname); \
}while( 0 );
#
#
#define __file__ __FILE__
#define FILENAME() do \
{ int i = strlen(__FILE__) - 1; \
for( ; \
__FILE__[i] != '\\' && __FILE__[i] != '/'; \
i--); \
char *fname = malloc(strlen(__FILE__) - i); \
strcpy(fname, __FILE__ + i + 1); \
printf("FileName: %s", fname); \
free(fname); \
}while( 0 );
#
#
#define __line__ __LINE__ ///
#define LINE( ) do \
{ \
printf("Line: %d", __LINE__); \
}while( 0 );
#
#
#define __FUNC__ __func__ ///
#define FUNC( ) do \
{ \
printf("Function: %s", __FUNC__); \
}while( 0 );
#
#
#define FILE_FUNC_LINE( ) do \
{ \
FILENAME( ); \
putchar(' '); \
FUNC( ); \
putchar(' '); \
LINE( ); \
putchar('\n'); \
}while(0);
#if !defined(offset) && !defined(container_of)
#define offsetof(type, member) ((size_t) &((type *)0)->member)
/*
* get the struct(type) from the pointer pstr(one of his member)
* ptr : the pointer point to the member whose type is member you know
* type : the type of the struct you want get
* member : the member's name in type struct
*/
#define container_of(ptr, type, member) \
({ \
const typeof( ((type *)0)->member) *m_ptr = (ptr); \
(type *)( (char *)m_ptr - offsetof(type, member) ); \
})
#endif
#endif /* _COMMON_H_ */