该作业是使用Compund Literals的示例.根据C99 Section #6.5.2.5:
A postfix expression that consists of a parenthesized type name
followed by a brace- enclosed list of initializers is a compound
literal. It provides an unnamed object whose value is given by the
initializer list.
A compound literal looks like a cast of a brace-enclosed aggregate
initializer list. Its value is an object of the type specified in the
cast, containing the elements specified in the initializer. Unlike the
result of a cast, a compound literal is an lvalue. ISO C99 and later
support compound literals. As an extension, GCC supports compound
literals also in C90 mode and in C++, although as explained below, the
C++ semantics are somewhat different.
一个简单的例子:
struct foo { int x; int y; };
func() {
struct foo var = { .x = 2, .y = 3 };
...
}
在问题的示例中,struct file_operations在include/linux/fs.h中定义,而tracing_fops在Linux源树中的kernel/trace/trace.c文件中.
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
...
};
打开,读取,写入是Function Pointers,它们是指向函数的指针.解引用函数指针后,它可用作常规函数调用. tracing_fops结构是file_operations类型.使用复合文字将函数指针成员的值分配给同一trace.c文件中的函数.
使用复合文字,我们不必显式指定/分配结构类型中的所有成员,因为其他成员设置为零或null.使用复合文字创建的结构对象可以传递给函数,而无需依赖于成员顺序.双方的功能参数应相同.例如,参数
int (*open) (struct inode *, struct file *);
与…相同
int tracing_open(struct inode *inode, struct file *file);