内核模块中的静态函数原型
Static function prototypes in kernel modules
我正在阅读 LKM
开发手册并在 writing a device driver 时遇到以下示例:
/*
* Prototypes - this would normally go in a .h file
*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
让我感到困惑的是,建议将 static
non-inline
声明放入头文件中。这是什么原因?
如果我们不应该在除
之外的任何地方使用与驱动程序相关的这些功能
struct file_operations fops = {
.open = device_open,
.release = device_release,
.read = device_read,
.write = device_write
};
static int init_module(void){
int register_result = register_chrdev(&fops);
//...
}
在模块初始化期间,我们可以简单地将它们的静态定义放在这里,避免原型声明。否则他们应该(应该?)用外部链接声明。
完全没有必要将 static
非 inline
声明放入单独的头文件中。
如果驱动程序非常复杂,device_*
方法的定义可以安排到不同的源文件中(并且 file_operations
结构的定义也可以安排到它自己的源文件中),但是在那case static
说明符应该被删除。
我正在阅读 LKM
开发手册并在 writing a device driver 时遇到以下示例:
/*
* Prototypes - this would normally go in a .h file
*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
让我感到困惑的是,建议将 static
non-inline
声明放入头文件中。这是什么原因?
如果我们不应该在除
之外的任何地方使用与驱动程序相关的这些功能struct file_operations fops = {
.open = device_open,
.release = device_release,
.read = device_read,
.write = device_write
};
static int init_module(void){
int register_result = register_chrdev(&fops);
//...
}
在模块初始化期间,我们可以简单地将它们的静态定义放在这里,避免原型声明。否则他们应该(应该?)用外部链接声明。
完全没有必要将 static
非 inline
声明放入单独的头文件中。
如果驱动程序非常复杂,device_*
方法的定义可以安排到不同的源文件中(并且 file_operations
结构的定义也可以安排到它自己的源文件中),但是在那case static
说明符应该被删除。