Linux 中C语言的一点高级用法

本文介绍了Linux内核中C语言的高级用法,特别是container_of宏。这个宏能通过结构体成员的指针反推出整个结构体的首地址,模拟了C++的多态效果。文章详细讲解了宏的作用、实现原理,并探讨了typeof关键字的用途以及强制类型转换的应用。
摘要由CSDN通过智能技术生成

在LINUX中内核中有很多C语言的奇技淫巧,比如container_of()这个宏, 在很多地方会用到,比如在定义宏to_i2c_driver, to_spi_driver时都用到这个,有些就直接在函数中调用 container_of宏。

1. 作用

这个宏的作用就是通过一个结构体的某个成员的指针,反推出这个结构体的首地址。
这种操作非常有用,尤其在C语言中写面向对象的程序时,相当模拟了C++多态的效果。以LINUX内核中I2C子系统为例, i2c驱动结构体如下:

struct i2c_driver {
    unsigned int class;

    /* Standard driver model interfaces */
    int (*probe)(struct i2c_client *client, const struct i2c_device_id *id);
    void (*remove)(struct i2c_client *client);

    /* New driver model interface to aid the seamless removal of the
     * current probe()'s, more commonly unused than used second parameter.
     */
    int (*probe_new)(struct i2c_client *client);

    /* driver model interfaces that don't relate to enumeration  */
    void (*shutdown)(struct i2c_client *client);

    /* Alert callback, for example for the SMBus alert protocol.
     * The format and meaning of the data value depends on the protocol.
     * For the SMBus alert protocol, there is a single bit of data passed
     * as the alert response's low bit ("event flag").
     * For the SMBus Host Notify protocol, the data corresponds to the
     * 16-bit payload data reported by the slave device acting as master.
     */
    void (*alert)(struct i2c_client *client, enum i2c_alert_protocol protocol,
              unsigned int data);

    /* a ioctl like command that can be used to perform specific functions
     * with the device.
     */
    int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);

    struct device_driver driver;
    const struct i2c_device_id *id_table;

    /* Device detection callback for automatic device creation */
    int (*detect)(struct i2c_client *client, struct i2c_board_info *info);
    const unsigned short *address_list;
    struct list_head clients;

    u32 flags;
};

在struct i2c_driver 中有一个成员struct device_driver driver, 这相当于“is-a”的关系,即 i2c_driver 是一种 device_driver. 这种操作实际就是继承。

紧接着定义了一个i2c专用宏

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值