gpt4 book ai didi

c - "do { ... } while (0)"在内核代码中到底做了什么?

转载 作者:太空狗 更新时间:2023-10-29 16:27:18 27 4
gpt4 key购买 nike

<分区>

Possible Duplicates:
What’s the use of do while(0) when we define a macro?
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
C multi-line macro: do/while(0) vs scope block

我见过很多这样的用法,以前我以为程序员想轻松地跳出一段代码。为什么这里需要一个 do { ... } while (0) 循环?我们是不是想告诉编译器一些事情?

例如Linux内核2.6.25,include/asm-ia64/system.h

/*
* - clearing psr.i is implicitly serialized (visible by next insn)
* - setting psr.i requires data serialization
* - we need a stop-bit before reading PSR because we sometimes
* write a floating-point register right before reading the PSR
* and that writes to PSR.mfl
*/
#define __local_irq_save(x) \
do { \
ia64_stop(); \
(x) = ia64_getreg(_IA64_REG_PSR); \
ia64_stop(); \
ia64_rsm(IA64_PSR_I); \
} while (0)

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com