Linux内核之双向链表 - 通过list_head指针找到wrapper结构指针

转载自:

Linux内核之双向链表 - list_head(3) (mudongliang.github.io)icon-default.png?t=M4ADhttps://mudongliang.github.io/2015/10/05/linux-list_head3.html#:~:text=list_first_entry,%E5%AE%8F%EF%BC%9A%E8%8E%B7%E5%BE%97%E9%93%BE%E8%A1%A8%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E6%89%80%E5%9C%A8%E7%9A%84%E7%9C%9F%E6%AD%A3%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%9A%84%E8%B5%B7%E5%A7%8B%E5%9C%B0%E5%9D%80%EF%BC%81

struct list_head使用场景

struct a {
        struct list_head
        other data member ......
}

所以,通过struct list_head,我们可以把struct a串成一个链表,这样一来,就可以遍历链表实现对每一个struct a中的other data member访问和管理。

那如何通过struct list_head的指针找到struct a的指针呢?就是通过下面介绍的list_head操作宏实现: 

list_entry

内核中的链表仅仅保存了list_head结构的地址,我们如何通过它获取一个链表节点真正的数据项的地址?

以下的list_entry宏可以完成这个目的,这个宏里面使用内核中比较重要的一个宏,container_of 。

list_entry宏:从链表项地址获得包含这个链表项的真正数据结构的地址!

/**
 * list_entry - get the struct for this entry
 * @ptr: the &struct list_head pointer.
 * @type: the type of the struct this is embedded in.
 * @member: the name of the list_head within the struct.
 */
#define list_entry(ptr, type, member) \
           container_of(ptr, type, member)

list_first_entry & list_last_entry

获取第一个和最后一个元素所以真正数据结构的起始地址,可以用于遍历时初始循环变量和最后比对。

list_first_entry宏:获得链表第一个元素所在的真正数据结构的起始地址!

list_last_entry宏:获得链表最后一个元素所在的真正数据结构的起始地址!

/**
 * list_first_entry - get the first element from a list
 * @ptr: the list head to take the element from.
 * @type: the type of the struct this is embedded in.
 * @member: the name of the list_head within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define list_first_entry(ptr, type, member) \
             list_entry((ptr)->next, type, member)
/**
 * list_last_entry - get the last element from a list
 * @ptr: the list head to take the element from.
 * @type: the type of the struct this is embedded in.
 * @member: the name of the list_head within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define list_last_entry(ptr, type, member) \
           list_entry((ptr)->prev, type, member)

list_next_entry & list_prev_entry

获取下一个和上一个元素所以真正数据结构的起始地址,可以用于遍历时初始循环变量向后或向前。

list_next_entry宏:获得链表下一个元素所在的真正数据结构的起始地址!

list_prev_next宏:获得链表上一个元素所在的真正数据结构的起始地址!

/**
 * list_next_entry - get the next element in list
 * @pos: the type * to cursor
 * @member: the name of the list_head within the struct.
 */
#define list_next_entry(pos, member) \
        list_entry((pos)->member.next, typeof(*(pos)), member)
/**
 * list_prev_entry - get the prev element in list
* @pos: the type * to cursor
 * @member: the name of the list_head within the struct.
 */
 #define list_prev_entry(pos, member) \
        list_entry((pos)->member.prev, typeof(*(pos)), member)
/**
 * list_first_entry_or_null - get the first element from a list
 * @ptr: the list head to take the element from.
 * @type: the type of the struct this is embedded in.
 * @member: the name of the list_head within the struct.
 *
 * Note that if the list is empty, it returns NULL.
 */
#define list_first_entry_or_null(ptr, type, member) \
       (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)

list_for_each

head是整个链表的头指针,同时pos是链表类型的指针,且通过next指针不停地往后移动。用于简单的链表循环。

/**
 * list_for_each - iterate over a list
 * @pos: the &struct list_head to use as a loop cursor.
 * @head: the head for your list.
 */
#define list_for_each(pos, head) \
        for (pos = (head)->next; pos != (head); pos = pos->next)
/**
 * list_for_each_safe - iterate over a list safe against removal of list entry
 * @pos: the &struct list_head to use as a loop cursor.
 * @n: another &struct list_head to use as temporary storage
 * @head: the head for your list.
 */
#define list_for_each_safe(pos, n, head) \
    for (pos = (head)->next, n = pos->next; pos != (head); \
            pos = n, n = pos->next)

list_for_each_entry

head是整个链表的头指针,同时pos是真正数据结构类型的指针,且通过next指针和list_next_entry不停地 往后移动,而member是真正数据类型中链表类型的名字,这个变量真正需要用在container_of宏中。用于很好用的链表循环,无需自己使用list_entry来获得真正数据结构类型的起始地址。

/**
 * list_for_each_entry - iterate over list of given type
 * @pos: the type * to use as a loop cursor.
 * @head: the head for your list.
 * @member: the name of the list_head within the struct.
 */
#define list_for_each_entry(pos, head, member)                     \
    for (pos = list_first_entry(head, typeof(*pos), member);        \
            &pos->member != (head);                            \
            pos = list_next_entry(pos, member))
list_for_each_entry(fmt, &formats, lh) {
    if (!try_module_get(fmt->module))
        continue;
    read_unlock(&binfmt_lock);
    bprm->recursion_depth++;
    retval = fmt->load_binary(bprm);
    read_lock(&binfmt_lock);
    ......
}

以上的代码的内容是遍历formats链接的双向链表,使用的就是list_for_each_entry.

list_for_each_prev & list_for_each_prev_safe

/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
 * @head: the head for your list.
 */
 #define list_for_each_prev(pos, head) \
        for (pos = (head)->prev; pos != (head); pos = pos->prev)

/**
 * list_for_each_prev_safe - iterate over a list backwards 
 * safe against removal of list entry
 * @pos: the &struct list_head to use as a loop cursor.
 * @n: another &struct list_head to use as temporary storage
 * @head: the head for your list.
 */
#define list_for_each_prev_safe(pos, n, head) \
    for (pos = (head)->prev, n = pos->prev; \
         pos != (head); \
         pos = n, n = pos->prev)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值