linux中没有初始化变量,Linux:为啥内核有的变量没有初始化就敢直接使用?

50 wait_event(wq, condition)

51         do {

52             if (condition)

53                 break;

54                     __wait_event(wq, condition);

55         } while (0)

56

57

58

59 test()

60 {

62     wait_event(wq,flag == 0);

64 }

编译与处理结果如下:

root@ubuntu:/home/peng/test# gcc wait.c -E

# 1 "wait.c"

# 1 "<built-in>"

# 1 "<command-line>"

# 1 "/usr/include/stdc-predef.h" 1 3 4

# 1 "<command-line>" 2

# 1 "wait.c"

# 71 "wait.c"

test()

do { if (flag == 0) break; (void)({ __label__ __out; wait_queue_t __wait; long __ret = 0; INIT_LIST_HEAD(&__wait.task_list); if (0) __wait.flags = WQ_FLAG_EXCLUSIVE; else { __wait.flags = 0; for (;;) { long __int = prepare_to_wait_event(&wq, &__wait, 2); if (flag == 0) break; if (___wait_is_interruptible(2) && __int) { __ret = __int; if (0) { abort_exclusive_wait(&wq, &__wait, 2, NULL); goto __out; } break; } schedule(); } finish_wait(&wq, &__wait); __out: __ret; }) }; } while (0);

函数test()整理如下:

test(){

do {

if (flag == 0)

break;

(void)(

__label__ __out;

wait_queue_t __wait;

long __ret = 0;

INIT_LIST_HEAD(&__wait.task_list);

if (0) __wait.flags = WQ_FLAG_EXCLUSIVE;

else {

__wait.flags = 0;

for (;;)

long __int = prepare_to_wait_event(&wq, &__wait, 2);

if (flag == 0)

break;

if (___wait_is_interruptible(2) && __int)

__ret = __int;

if (0)

abort_exclusive_wait(&wq, &__wait, 2, NULL);

goto __out;

break;

schedule();

finish_wait(&wq, &__wait);

__out:

__ret;

})

};

}while (0);

这就是wait_event()最终被替换后的代码,你学会了吗?

六、15个经典宏定义小例子

数值相关的宏定义

闰年的判断 ,年份可以整除4并且不能整除100,或者可以整除400,则为闰年;#define IS_LEAP_YEAR(y) (((((y) % 4) == 0) && (((y) % 100) != 0))

|| (((y) % 400) == 0))判断是否是闰年

**MAX 与 MIN ** ;

#define MAX(x, y)   (((x) < (y)) ? (y) : (x)) 两数取最大数

#define MIN(x, y)   (((x) < (y)) ? (x) : (y)) 两数取最小数

BCD码;#define BCD2HEX(x) (((x) >> 4) * 10 + ((x) & 0x0F))       BCD码转数值, 20H -> 20

#define HEX2BCD(x) (((x) % 10) + ((((x) / 10) % 10) << 4))  数值转BCD码, 20 -> 20H

字符相关的宏定义

字符范围判断

字符是否在某个区间范围内

#define in_range(c, lo, up)  ((uint8)c >= lo && (uint8)c <= up)

#define isprint(c)           in_range(c, 0x20, 0x7f)

十进制内字符

#define isdigit(c)           in_range(c, '0', '9')

十六进制内字符

#define isxdigit(c)          (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))

是否是小写

#define islower(c)           in_range(c, 'a', 'z')

是否是空格

#define isspace(c)           (c == ' ' || c == '' || c == '' || c == '' || c == '' || c == '')

是否为ASCII码

#define isascii(c)          ((unsigned) (c) <= 0177)

byte相关的宏定义#define MSB(x) (((x) >> 8) & 0xff)  x占2byte(如short)2byte的高地址的1byte

#define LSB(x) ((x) & 0xff)  x占2byte(如short)2byte的低地址的1byte

#define MSW(x) (((x) >> 16) & 0xffff)  x占4byte(如int)  4byte的高地址的2byte

#define LSW(x) ((x) & 0xffff)

#define WORDSWAP(x) (MSW(x) | (LSW(x) << 16))  x占4byte(如int) 低2字节与高2字节内容交换

#define LLSB(x) ((x) & 0xff) x占4byte(如int) 取低地址1byte

#define LNLSB(x) (((x) >> 8) & 0xff)

#define LNMSB(x) (((x) >> 16) & 0xff)

#define LMSB(x)  (((x) >> 24) & 0xff)

x占4byte(如int) 4字节逆序

#define LONGSWAP(x) ((LLSB(x) << 24)

|(LNLSB(x) << 16)

|(LNMSB(x) << 8)

|(LMSB(x)))

bit相关的宏定义 判断某位是否为1

#define BIT_IS_1(x,y) (((x)>>(y))&0x01u)

#define SETBITS(x,y,n) (x) = (n) ? ((x)|(1 << (y))) : ((x) &(~(1 << (y))));

给某位置反

#define BIT_INVERSE(x,y)    ((x)=(x)^(1<<(y)))

字节串中某BIT值

#define BIT_OF_BYTES(x, y) (BITS(x[(y)/8], (y)%8))

字节串中设置某BIT为0

#define SETBITSTO0_OF_BYTES(x, y) (x[(y)/8]) &= (~(1 << ((y)%8)))

字节串中设置某BIT为1

#define SETBITSTO1_OF_BYTES(x, y) (x[(y)/8]) |= (1 << ((y)%8))

数组与结构体相关的宏定义 number of elements in an array

#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))

byte offset of member in structure

#define MOFFSET(structure, member) ((int) &(((structure *) 0) -> member))

size of a member of a structure

#define MEMBER_SIZE(structure, member) (sizeof (((structure *) 0) -> member))

对齐的宏定义向上对齐,~(align - 1)为对齐掩码,例如align=8,~(align - 1) = ~7,

(~7)二进制后三位为000,&(~(align - 1)) = &(~7),就是去掉余数,使其能被8整除

#define ALIGN_UP(x, align)  (((int) (x) + (align - 1)) & ~(align - 1))

向下对齐

#define ALIGN_DOWN(x, align)    ((int)(x) & ~(align - 1))

是否对齐

#define ALIGNED(x, align)   (((int)(x) & (align - 1)) == 0)

页面对齐相关的宏,一页为4096字节

#define PAGE_SIZE         4096

#define PAGE_MASK         (~(PAGE_SIZE-1))

#define PAGE_ALIGN(addr) -(((addr)+PAGE_SIZE-1) & PAGE_MASK)

防止头文件被重复包含#ifndef BODY_H   //保证只有未包含该头文件才会将其内容展开

#define BODY_H

//头文件内容

#endif

得到指定地址上的一个字节或者一个字#define MEM_B(x) (*(byte*)(x))    //得到x表示的地址上的一个字节

#define MEM_W(x) (*(word*)(x))    //得到x表示的地址上的一个字

得到一个field在结构体中的偏移量#define OFFSET(type,field) (size_t)&((type*)0->field)

得到一个结构体中field所占用的字节数#define FSIZ(type,field) sizeof(((type*)0)->field)

得到一个变量的地址#defien B_PTR(var) ((byte*)(void*)&(var))   //得到字节宽度的地址

#define W_PTR(var) ((word*)(void*)&(var))   //得到字宽度的地址

将一个字母转换成大写#define UPCASE(c) (((c) >= "a" && (c) <= "z") ? ((c) - 0x20) : (c) )

防止溢出#define INC_SAT(val) (val = ((val) +1 > (val)) ? (val) + 1 : (val))

返回数组元素的个数#define ARR_SIZE(a) (sizeof( (a) ) / sizeof(a[0]) ) )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值