/** * 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_struct within the struct. */ #define list_entry(ptr, type, member) \ container_of(ptr, type, member)
/** * 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_entry((head)->next,typeof(*pos), member); \ prefetch(pos->member.next),&pos->member !=(head); \ pos = list_entry(pos->member.next,typeof(*pos), member))
其中的prefetch是预取当前结点的下一个结点,当链表节点很多时,它能加快运行速度。
关于结构体成员的初始化 一开始没有看懂下面这个代码:
#define LIST_HEAD_INIT(name){&(name),&(name)}
#define LIST_HEAD(name) \ struct list_head name = LIST_HEAD_INIT(name)