当前位置: 首页 > news >正文

编程参考 - 头文件中使用static inline

在Linux kernel的头文件中,经常使用static inline来声明一个函数。

比如include/linux/delay.h中,

static inline void ssleep(unsigned int seconds)

{

    msleep(seconds * 1000);

}

static Keyword

* 范围限制: 当应用于函数或变量时,static 会将该实体的可见性限制在声明它的文件中。这意味着不能从其他文件(翻译单元)访问该函数或变量。

* 链接:它赋予函数或变量内部链接,这意味着它是文件的私有部分,不会与其他文件中的同名函数或变量发生冲突。

* Scope Limitation: When applied to a function or variable, static limits the visibility of that entity to the file in which it is declared. This means that the function or variable cannot be accessed from other files (translation units).

* Linkage: It gives the function or variable internal linkage, meaning it is private to the file and does not conflict with functions or variables of the same name in other files.

inline Keyword

* 内联扩展: 内联关键字建议编译器在调用函数时就地展开,而不是通过常规函数调用来调用。这可以消除函数调用的开销,从而提高频繁调用的小型函数的性能。

* 不保证内联: 需要注意的是,内联是一种请求,而不是命令。如果编译器认为不适合内联(例如函数太大或内联效率不高),则可能忽略此建议。

* Inline Expansion: The inline keyword suggests to the compiler that the function should be expanded in place where it is called, rather than being invoked through a regular function call. This can eliminate the overhead of a function call, potentially improving performance for small, frequently called functions.

* No Guaranteed Inlining: It's important to note that inline is a request, not a command. The compiler might ignore this suggestion if it deems inlining inappropriate (e.g., the function is too large, or inlining would not be efficient).

static inline Together

当静态和内联同时使用时,它们结合了两个关键字的优点:

* 范围限制 + 内联扩展: 函数仅限于定义它的文件(不会与其他文件名冲突),鼓励编译器将其内联,尽可能用函数体代替对函数的调用。

* 无链接开销: 由于函数是静态的,编译器无需为其生成外部符号,这可以减少链接开销。

* 内核代码中的用例: 在内核编程中,静态内联通常用于在头文件中定义的小型实用功能或宏。通过将这些函数标记为内联,编译器可以将函数体直接替换到调用代码中,避免了函数调用的开销。static 关键字可确保每个翻译单元(源文件)都能获得该函数的私有副本,避免与其他文件中的同名函数发生冲突。

When static and inline are used together, they combine the benefits of both keywords:

* Scope Limitation + Inline Expansion: The function is limited to the file where it's defined (no name conflicts with other files), and the compiler is encouraged to inline it, replacing calls to the function with its body wherever possible.

* No Linkage Overhead: Since the function is static, there's no need for the compiler to generate an external symbol for it, which can reduce linkage overhead.

* Use Case in Kernel Code: In kernel programming, static inline is often used for small utility functions or macros that are defined in header files. By marking these functions as inline, the compiler can substitute the function body directly into the calling code, avoiding the overhead of a function call. The static keyword ensures that each translation unit (source file) gets its own private copy of the function, avoiding conflicts with functions of the same name in other files.

Summary

* static 将函数或变量的作用域限制在声明它的文件中。

* inline 建议编译器用函数体代替函数调用,以避免函数调用开销。

* static inline 将两者结合在一起,创建了一个对文件私有的函数,并有可能被内联,因此非常适合 Linux 内核等对性能敏感的代码中频繁使用的小型实用程序函数。

* static restricts the scope of a function or variable to the file in which it is declared.

* inline suggests that the compiler should replace function calls with the function body to avoid function call overhead.

* static inline combines both, creating a function that is private to the file and likely to be inlined, making it ideal for small, frequently used utility functions in performance-sensitive code like the Linux kernel.

http://www.lryc.cn/news/426800.html

相关文章:

  • Uniapp使用antd组件库
  • 计算机毕业设计选题推荐-高校实验室管理系统-Java/Python项目实战
  • nest定义响应码message文本
  • Java | Leetcode Java题解之第342题4的幂
  • 【日常开发】java中一个list对象集合 将字段a为 大豆 小麦 玉米等元素放在最前面 并组成新集合
  • C++ 设计模式——原型模式
  • 【Harmony OS 4.0】待办列表案例
  • 快速把文件名统计到excel表的方法
  • 开源通用验证码识别OCR —— DdddOcr 源码赏析(一)
  • 上升ECMAScript性能优化技巧与陷阱(下)
  • 用7EPhone云手机进行TikTok的矩阵运营
  • 谷歌浏览器下载文件被阻止怎么解除
  • apt E: 无法定位软件包 winehq-stable
  • P2460[SDOI2007] 科比的比赛
  • linux学习--第二天
  • 使用 Flask、Celery 和 Python 实现每月定时任务
  • 【c语言】整数在内存中的储存(大小端字节序)
  • 浅谈SIMD、向量化处理及其在StarRocks中的应用
  • 【ML】Image Augmentation)的作用、使用方法及其分类
  • 设计模式六大原则(一)--单一职责原则
  • c语言学习,malloc()函数分析
  • 【运维项目经历|041】上云项目-物理机迁移到阿里云
  • 分组并合并其它列的非空值 --Excel难题#83
  • VM相关配置及docker
  • Redis中Set数据类型常用命令
  • mysql误删数据恢复记录
  • 论文阅读:Real-time Controllable Denoising for Image and Video
  • 【Kubernetes】虚拟 IP 与 Service 的代理模式
  • 深度学习·Pytorch
  • fastzdp_sqlmodel新增get_first和is_exitsts方法