注册 登录
编程论坛 Linux教室

字符设备驱动程序设计中的数据结构:struct file?用法看的迷糊啊

GaFu 发布于 2008-11-06 13:46, 2669 次点击
c语言学的不精
struct file_operations mem_fops = {
    .owner = THIS_MODULE,
    .llseek = mem_seek,
    .read = mem_read,
    .write = mem_write,
    .ioctl = mem_ioctl,
    .open = mem_open,
    .release = mem_release,
};
主要是前边的小点,在结构体中这么用什么意思啊(成员变量引用?),前边还有个等号(给结构体赋值?),原定义的file_operations结构体成员变量也不是这样的啊,用的是冒号(:)这在C中不是位定义了吗?
3 回复
#2
rootkit2008-11-06 20:39
This declaration uses the standard C tagged structure initialization syntax. This syntax is preferred because it makes drivers more portable across changes in the definitions of the structures and, arguably, makes the code more compact and readable. Tagged initialization allows the reordering of structure members; in some cases, substantial performance improvements have been realized by placing pointers to frequently accessed members in the same hardware cache line.
#3
GaFu2008-11-07 00:34
楼上,粘的英语解说只说了这样声明结构的好处,还是不明白
#4
rootkit2008-11-07 15:38
The tagged structure initialization syntax allow you to initialize structure member in any order. So your code have no need to be modified  after the change of member definition order. And this feature has been a part of C standard since 1999.

struct file_operations mem_fops = {
    .owner = THIS_MODULE,
    .llseek = mem_seek,
    .read = mem_read,
    .write = mem_write,
    .ioctl = mem_ioctl,
    .open = mem_open,
    .release = mem_release,
};

.owner, .llseek and others are all the member tags.

For example:
struct foo{
int a;
int b;
};

You can declaration and initialize structure variable like this:

struct foo bar{
       .a = 1;
       .b = 2;
};

or in the other order:
struct foo bar{
       .b = 2;
       .a = 1;
};

Have you got it ?

[[it] 本帖最后由 rootkit 于 2008-11-7 15:42 编辑 [/it]]
1