文件操作结构体file_operations的完整定义见博文 https://blog.youkuaiyun.com/wenhao_ir/article/details/144905840
嵌入式驱动编程中结构体file_operations的第一个成员 struct module *owner 通常被赋值为 THIS_MODULE 这是怎么回事儿?
比如下面的代码:
static const struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};
我们在Linux的源码中(include\linux\export.h
)可以查到THIS_MODULE
是一个宏定义,具体如下:
#ifdef MODULE
extern struct module __this_module;
#define THIS_MODULE (&__this_module)
#else
#define THIS_MODULE ((struct module *)0)
#endif
把这段代码理解了,基本上就理解了,下面是详细讲解。
在 Linux 内核中,THIS_MODULE
是一个宏,用于指向当前驱动模块的 struct module
实例。它的作用是为模块提供元信息和生命周期管理支持。
THIS_MODULE
的定义解读
#ifdef MODULE
extern struct