cdev 初始化是 linux 设备驱动程序:这两种方法有什么区别?
cdev initialization is linux device drivers: what is the difference between the two approaces?
在书Linux Device Drivers (third edition)中,他们说:
There are two ways of allocating and initializing one of these
structures. If you wish to obtain a standalone cdev structure at
runtime, you may do so with code such as:
struct cdev *my_cdev = cdev_alloc( );
my_cdev->ops = &my_fops;
Chances are, however, that you will want to embed the cdev structure
within a device-specific structure of your own; that is what scull
does. In that case, you should initialize the structure that you have
already allocated with:
void cdev_init(struct cdev *cdev, struct file_operations *fops);
第二部分我没看懂。即使我们在特定于设备的结构中有 cdev 结构,为什么我们不能使用第一种方法进行初始化?
例如,如果我有自己的设备特定结构 foo_dev,并且 my_cdev 其中的一部分,
struct foo_dev my_foo_dev{
cdev* my_cdev;
...
}
为什么我不能直接使用
my_foo_dev->my_cdev = cdev_alloc( );
my_foo_dev->my_cdev->ops = &my_fops;
?
具体来说,不使用 cdev_init() 函数,我们可以不使用赋值来初始化 cdev 结构的各个成员吗?还是 cdev_init() 执行任何额外任务?
cdev_alloc
分配一个新的 struct cdev
对象和 returns 指向它的指针。
cdev_init
初始化一个现有的未初始化 struct cdev
,由您分配。
实际上,cdev_alloc
相当于:
struct cdev* p = malloc(sizeof(struct cdev));
cdev_init(p, opts);
在书Linux Device Drivers (third edition)中,他们说:
There are two ways of allocating and initializing one of these structures. If you wish to obtain a standalone cdev structure at runtime, you may do so with code such as:
struct cdev *my_cdev = cdev_alloc( );
my_cdev->ops = &my_fops;
Chances are, however, that you will want to embed the cdev structure within a device-specific structure of your own; that is what scull does. In that case, you should initialize the structure that you have already allocated with:
void cdev_init(struct cdev *cdev, struct file_operations *fops);
第二部分我没看懂。即使我们在特定于设备的结构中有 cdev 结构,为什么我们不能使用第一种方法进行初始化?
例如,如果我有自己的设备特定结构 foo_dev,并且 my_cdev 其中的一部分,
struct foo_dev my_foo_dev{
cdev* my_cdev;
...
}
为什么我不能直接使用
my_foo_dev->my_cdev = cdev_alloc( );
my_foo_dev->my_cdev->ops = &my_fops;
?
具体来说,不使用 cdev_init() 函数,我们可以不使用赋值来初始化 cdev 结构的各个成员吗?还是 cdev_init() 执行任何额外任务?
cdev_alloc
分配一个新的 struct cdev
对象和 returns 指向它的指针。
cdev_init
初始化一个现有的未初始化 struct cdev
,由您分配。
实际上,cdev_alloc
相当于:
struct cdev* p = malloc(sizeof(struct cdev));
cdev_init(p, opts);