linux内核 struct,linux-kernel-具有Linux源代码中方法的Struct结构

该作业是使用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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值