内核LIST分析

由于经常对list里的函数使用不是很熟练,因此写此文作为备用和记录,也方便记不清怎么利用这些函数的同学的需要。

1:list_for_each_entry()

struct i2c_dev {
    struct list_head list;
    struct i2c_adapter *adap;
    struct device *dev;
};

struct i2c_dev *i2c_dev;

static LIST_HEAD(i2c_dev_list);

list_for_each_entry(i2c_dev, &i2c_dev_list, list) {
        if (i2c_dev->adap->nr == index)
            goto found;
    }

速记方法:list_for_each_entry(大结构体,头节点,大结构体里的小结构体)

其中头节点,大结构体里的小结构体为同种结构体类型,这里为struct list_head

2:container_of()

static int i2c_device_probe(struct device *dev)
{
    struct i2c_client    *client = i2c_verify_client(dev);
    struct i2c_driver    *driver;
    int status;

    if (!client)
        return 0;

    driver = to_i2c_driver(dev->driver);

//#define to_i2c_driver(d) container_of(d, struct i2c_driver, driver)

//#define offset(type,member)  ((size_t )&(((type *) 0)->member))

............................

}

速记方法:container_of(传入的被__mptr指向指针,大结构体类型,大结构体的小结构体)

返回的是指向大结构体类型的指针。

其中:传入的被__mptr指向指针和大结构体的小结构体类型相同。

3:list_for_each_safe(大体1,大体2,头节点,大体中的小体)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值