概述
Linux 内核中常用 list_for_each_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_struct 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 作用:所有包含 list_head 的数据结构,均可使用此方法遍历链表;list_head 结构体不包含数据部分,使用该函数进行遍历链表节点,然后在循环体中,对链表的数据部分进行读写操作,通过对链表中 list 成员的遍历,即可定位到链表的相关节点,进而访问链表节点中的数据部分。
用法
如下面用法:
示例 1:
struct input_handler {
void *private;
...
const char *name;
const struct input_device_id *id_table;
struct list_head h_list;
struct list_head node;
};
struct input_handle *handle;
static LIST_HEAD(input_handler_list);
// 定义 list_add_tail(&dev->node, &input_dev_list); //加入链表表
list_for_each_entry(handler, &input_handler_list, node) //遍历
input_attach_handler(dev, handler); //应用
示例 2:
{
u32 scancode;
u32 keycode;
};
size;
len;
alloc;
*name;
lock;
};
};
;
rc_map_list *
{
;
spin_lock(&rc_map_lock);
list_for_each_entry(, &rc_map_list, ) {
(!(name, ->.name)) {
spin_unlock(&rc_map_lock);
;
}
}
spin_unlock(&rc_map_lock);
;
}
{
spin_lock(&rc_map_lock);
list_add_tail(&->, &rc_map_list);
spin_unlock(&rc_map_lock);
;
}
{
. = {
.scan = encore_enltv,
.size = ARRAY_SIZE(encore_enltv),
.rc_type = RC_TYPE_UNKNOWN,
.name = RC_MAP_ENCORE_ENLTV,
}
};
__init
{
rc_map_register(&encore_enltv_map);
}

