Static functions in Linux device driver?

后端 未结 3 835
半阙折子戏
半阙折子戏 2021-02-20 13:25

Is there a reason why most function definition in device driver in linux code is defined as static? Is there a reason for this?

I was told this is for scoping and to pr

相关标签:
3条回答
  • 2021-02-20 14:14

    I concur. This is common and wise practice in any C code - not just kernel code! Don't go thinking this is only appropriate for low level stuff, any C code that stretches past one .c file should have thought given to this.

    0 讨论(0)
  • 2021-02-20 14:27

    Functions declared static are not visible outside the translation unit they are defined in (a translation unit is basically a .c file). If a function does not need to be called from outside the file, then it should be made static so as to not pollute the global namespace. This makes conflicts between names that are the same are less likely to happen. Exported symbols are usually indentified with some sort of subsystem tag, which further reduces scope for conflict.

    Often, pointers to these functions end up in structs, so they are actually called from outside the file they are defined in, but not by their function name.

    0 讨论(0)
  • 2021-02-20 14:29

    For the same reasons you use static in any code. You should only 'publish' your API calls, anything else opens you up to abuse, such as being able to call internal functions from outside the driver, something that would almost certainly be catastrophic.

    It's good programming practice to only make visible to the outside world what's necessary. That's what encapsulation is all about.

    0 讨论(0)
提交回复
热议问题