DAY_18_linux/list.h

/*
/usr/src/xxxxx/include/linux/list.h
LIST_HEAD_INIT(name)
LIST_HEAD(name)
函数
static inline void INIT_LIST_HEAD(struct list_head *list)
__list_add
list_add
list_add_tail
__list_del
list_del
list_empty

container_of  //自己实现
list_entry
list_for_each
list_for_each_safe
*/
#ifndef __LIST_H
#define __LIST_H

struct list_head {

    struct list_head * next;
    struct list_head * prev;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }

//定义了一个名为(name)节点,并且初始化该节点,让该节点,前驱和后继指针都指向自己
#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)

/*
LIST_HEAD(head);
struct list_head head = {&(name), &(name)};
={
    .next = &head,
    .prev = &head
};
*/

//对一个节点进行初始化,前驱和后继指针都指向自己
static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

//把节点new 插入到prev,和next节点之间
static inline void __list_add(struct list_head *new,
                  struct list_head *prev,
                  struct list_head *next)
{
    next->prev = new;
    new->next = next;
    new->prev = prev;
    prev->next = new;
}

//头插,把节点插入到head和head->next之间
static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}

//尾插,把节点插入到尾部
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}

static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}

static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
//    entry->next = LIST_POISON1;
//    entry->prev = LIST_POISON2;
    INIT_LIST_HEAD(entry);    
}

//判断该链表是否为空
static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}

/**
 * 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 m_offsetof(type, member) \
        ((size_t)(&((type *)0)->member))

#define container_of(ptr, type, member) \
    ((type *)((size_t)(ptr)- m_offsetof(type, member)))

#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)

/**
 * 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  -   iterate over list of given type
 * @type:    the type * to use as a loop cursor.
 * @head:   the head for your list.
 * @member: the name of the list_head within the struct.
 */

list_for_each
list_entry

//&f->a
//&(f->a); //f是一个结构体指针,取成员a。取成员a的地址
//(&f)->a; //f是一个结构体,&f是结构体的地址,->
//f.a

//(*f).a //是一个指针, 用点.取成员

//type对象结构体指针,  tpyeof(* type)
//head 内核链表头节点
//member 结构体中成员的名字,list
//struct node *pnode;
//list_for_each_entry(pnode, &head, list)
//for(pnode=list_entry((&head)->next), struct node, list;
//      &(pnode->member) !=&(head);
//    pnode = list_entry(pnode->list.next, struct node, list  ) {
//}
#define list_for_each_entry(type, head, member) \
    for (type = list_entry((head)->next, typeof(*type), member); \
            &((type)->member) != head; \
            type = list_entry((type)->member.next, typeof(*type), member))
#endif

猜你喜欢

转载自blog.csdn.net/qq_22038327/article/details/47359685