C静态内联函数调用extern函数的动机

标签 c static inline extern

看看底部的补丁。

---
 drivers/iommu/iommu.c             | 4 ++--
 drivers/iommu/msm_iommu_domains.c | 2 +-
 include/linux/iommu.h             | 6 +++++-
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 7848f47..c2f694c 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -670,7 +670,7 @@ void iommu_set_fault_handler(struct iommu_domain *domain,
 }
 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);

-struct iommu_domain *iommu_domain_alloc(struct bus_type *bus, int flags)
+struct iommu_domain *iommu_domain_alloc_flags(struct bus_type *bus, int flags)
 {
    struct iommu_domain *domain;
    int ret;
@@ -695,7 +695,7 @@ out_free:

    return NULL;
 }
-EXPORT_SYMBOL_GPL(iommu_domain_alloc);
+EXPORT_SYMBOL_GPL(iommu_domain_alloc_flags);

 void iommu_domain_free(struct iommu_domain *domain)
 {
diff --git a/drivers/iommu/msm_iommu_domains.c b/drivers/iommu/msm_iommu_domains.c
index 26a3f85..7619e66 100644
--- a/drivers/iommu/msm_iommu_domains.c
+++ b/drivers/iommu/msm_iommu_domains.c
@@ -506,7 +506,7 @@ int msm_register_domain(struct msm_iova_layout *layout)
    if (data->domain_num < 0)
        goto free_pools;

-   data->domain = iommu_domain_alloc(bus, layout->domain_flags);
+   data->domain = iommu_domain_alloc_flags(bus, layout->domain_flags);
    if (!data->domain)
        goto free_domain_num;

diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index fb1efec..8bd9d3f 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -131,7 +131,11 @@ struct iommu_ops {

 extern int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops);
 extern bool iommu_present(struct bus_type *bus);
-extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus, int flags);
+extern struct iommu_domain *iommu_domain_alloc_flags(struct bus_type *bus, int flags);
+static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
+{
+   return iommu_domain_alloc_flags(bus, 0);
+}
 extern struct iommu_group *iommu_group_get_by_id(int id);
 extern void iommu_domain_free(struct iommu_domain *domain);
 extern int iommu_attach_device(struct iommu_domain *domain,
-- 

此补丁有什么好处,即是否有任何性能提升?

inline static 函数调用 extern 是一种好的编程习惯吗?

如果 extern 表示外部链接,即函数定义在其他地方,那么在 static 函数中使用它是否安全,它表示“此函数应该仅在此内部可见翻译单元”?

根据这个答案判断:Inline functions and external linkage

So "external linkage" and "inline" are not exclusive; "external linkage" means that the function may be referred to in any translation unit, and "inline" means that it must be defined in any translation unit that calls it.

inlineexternal 显然不是在打架,但是 static 的存在才是困扰我的地方。

谢谢

最佳答案

What is the benefit of this patch, i.e. are there any performance wins?

好像换了个接口(interface),改了一个函数的名字和参数,然后又多了一个函数。这是功能的改变,与性能无关。

Is it good programming practice to have inline static function calling one which is extern?

当然是,它只是意味着 static 函数在声明它的模块内部,调用者不可用。

is it safe that to use it in static function which says "this function should be visible only within this translation unit"?

当使用外部链接调用函数时,从 static 函数或从其他任何地方调用函数没有区别。调用者的链接与被调用函数的链接无关。

关于C静态内联函数调用extern函数的动机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202274/

相关文章:

java - Initialization On Demand Holder 成语

java - 我应该使用静态绑定(bind)吗?

java - 如何避免静态方法的冗余代码

Vim 内联重映射以检查第一个字符

c - 将数组中的字符追加到 char 指针

C 改变函数中的指针

c - for 循环在 pro*C 中包含 PL/SQL block

c++ - 如何在 x86-64 上优化 C 和 C++ 中的函数返回值?

css - 为什么不鼓励内联样式?

c - 通过宏将变量声明为内联函数