当前位置: 首页 > 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.

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Spring Cloud Gateway 请求转发源码分析
  • AI预测福彩3D采取888=3策略+和值012路或胆码测试8月17日新模型预测第59弹
  • 学习笔记--算法(滑动窗口)9
  • 2.1算法的时间复杂度与空间复杂度
  • Keepalived 企业应用示例
  • tcpdump的使用
  • 思二勋:web3.0是打造应对复杂市场敏捷组织的关键
  • SpringBoot依赖之Spring Data Redis 一 Hash类型
  • VirtualBox安装Oracle Linux 7.9全流程
  • 【秋招笔试】8.17京东秋招第二场(后端岗)-三语言题解
  • 什么是OpenTiny?
  • 使用Nexus搭建Maven私服仓库
  • C++之STL容器详解(包含十种常用容器)
  • 给既有exe程序添加一机一码验证
  • 共享内存、消息队列、信号量
  • JavaScript 如何正确处理 Unicode 编码问题!
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • LeetCode541. Reverse String II -- 按步长反转字符串
  • Quartz初级教程
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • 从0到1:PostCSS 插件开发最佳实践
  • 一个项目push到多个远程Git仓库
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 再谈express与koa的对比
  • Android开发者必备:推荐一款助力开发的开源APP
  • raise 与 raise ... from 的区别
  • ​浅谈 Linux 中的 core dump 分析方法
  • #define、const、typedef的差别
  • #LLM入门|Prompt#3.3_存储_Memory
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (1)(1.8) MSP(MultiWii 串行协议)(4.1 版)
  • (1)Jupyter Notebook 下载及安装
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (libusb) usb口自动刷新
  • (二)Kafka离线安装 - Zookeeper下载及安装
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (十六)视图变换 正交投影 透视投影
  • (四)鸿鹄云架构一服务注册中心
  • (五)网络优化与超参数选择--九五小庞
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (译)2019年前端性能优化清单 — 下篇
  • (转)iOS字体
  • (转)程序员疫苗:代码注入
  • ******之网络***——物理***
  • **python多态
  • .htaccess配置重写url引擎
  • .net core 调用c dll_用C++生成一个简单的DLL文件VS2008
  • .NET DataGridView数据绑定说明
  • .NET开发人员必知的八个网站
  • .Net中ListT 泛型转成DataTable、DataSet
  • .sh文件怎么运行_创建优化的Go镜像文件以及踩过的坑
  • .考试倒计时43天!来提分啦!