//在Linux源码的很多头文件中,经常看到static inline函数的定义
//适用于频繁调用的逻辑, inline消除函数调用开销, 写成函数又可以具有语义, 并且有类型检查
static inline struct proc_inode *PROC_I(const struct inode *inode)
{
return container_of(inode, struct proc_inode, vfs_inode);
}
//inline函数提示编译器进行内联,但不保证一定内联
//inline函数对外部不可见,因此可以在多个.c源文件中定义同一个inline函数
//The inline definition that does not use extern is not externally visible and does not prevent other
//translation units from defining the same function.
static inline void exit_aio(struct mm_struct *mm){}
inline//如果没有内联,则程序中调用该函数的地方,将报错undefined reference
static inline//不保证内联,但还有一个static函数
extern inline//不保证内联,但是还有一个全局函数
//main.c
inline int exit_aio(){
return 1+2;
}
int main() {
printf("retval is:%d\n", exit_aio());
}
//gcc main.c -O2 -o main(ok)
//gcc main.c -o main(err,undefined reference to 'exit_aio')
//这样编译也是可以的
inline __attribute__((always_inline)) int exit_aio(){
return 1+2;
}
static inline int exit_aio(){
return 1+2;
}
extern inline int exit_aio(){
return 1+2;
}
https://godbolt.org/
https://elinux.org/Extern_Vs_Static_Inline
https://kernelnewbies.org/FAQ/ExternAndStaticInlineVariable
https://yarchive.net/comp/linux/gcc_inline.html
//extern "C"是c++的语法,C语言没有这个语法
//表示{}中的函数是使用C语言写的(.c文件),可以在C++中被调用,同时对这些函数禁用重载,函数名是唯一标识符
extern "C" {
void test();
}
extern "C" void foo(int);
extern "C" int g();
int g();//OK, has C language linkage
int h();//has C++ language linkage by default
extern "C" int h();//Error: different language linkages
#ifdef __cplusplus
extern "C" int foo(int, int);//C++ compiler sees this
#else
int foo(int, int);//C compiler sees this
#endif
https://en.cppreference.com/w/cpp/language/language_linkage
https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c
https://www.microforum.cc/blogs/entry/34-using-extern-c/
__attribute__((unused)) int drv_get_peer_status(void){}//即使该函数没有被引用,也不要产生warning
__attribute__((used)) int drv_get_peer_status(void){}//即使该函数没有被引用,也要为其生成机器指令,不能优化掉
int __attribute__((weak)) drv_get_ctrl_mask(mask_t *mask){}
void __attribute__((constructor)) pipe_init_export_func(void){}//在执行main函数之前执行该函数
void __attribute__((destructor)) pipe_uninit_export_func(void){}//main函数或exit执行完,执行该函数
__attribute__((visibility("default")))//通常配合
//被调用时,对函数参数进行类型检查
//a指函数的第几个参数是format_string(索引从1开始),b指根据format_string,从函数的第几个参数开始检查
//对于参数无法被检查的函数,将b指定为0
__attribute__((__format__(printf, a, b)))
__attribute__((__format__(printf, 2, 3))) int dev_set_name(struct device *dev, const char *name, ...);
struct msg_head_info {
u32 status;
u32 buf_len;
u32 version;
}__attribute__((aligned(128)));//该结构体变量的大小将为128字节,且地址也为128字节对齐
struct ipcMsg {
struct msghead head;
unsigned char payload[1024];
unsigned short crc;
} __attribute__((packed));//成员之间不要有padding,按紧凑布局存储结构体
数据过大时,进行切片,每片都有header信息,每片的头信息都会记录(是否是最后一片,数据在原始数据中的偏移offset,以及数据的长度)
分片之间通过链表进行关联

622

被折叠的 条评论
为什么被折叠?



