Linux内核中的一个宏函数例子

在移植linux到龙芯3210的过程中,调试串口的时候,遇到了一个outb函数,却找不到这个函数的原型。当时是用VIM的跳转功能来看的代码。直接用grep工具也找不到这个函数。后问人才发现其实outb实则上是一个宏函数,而这宏函数的写法还真是少见,可能是见识少了。

相关阅读:移植linux到龙芯3210笔记

在./include/asm/io.h(其实asm是一个软链接,实际上是./include/asm-mips/io.h)中:

363 #define __BUILD_IOPORT_SINGLE(pfx, bwlq, type, p, slow)                 \

364                                                                         \

365 static inline void pfx##out##bwlq##p(type val, unsigned long port)      \

366 {                                                                       \

367         volatile type *__addr;                                          \

368         type __val;                                                     \

369                                                                         \

370         __addr = (void *)__swizzle_addr_##bwlq(mips_io_port_base + port); \

371                                                                         \

372         __val = pfx##ioswab##bwlq(__addr, val);                         \

373                                                                         \

374         /* Really, we want this to be atomic */                         \

375         BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long));             \

376                                                                         \

377         *__addr = __val;                                                \

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/24336.html