list_head.h

typedef struct list_head
{
    struct list_head *prev;
    struct list_head *next;
}list_head_t;

#define container_of(ptr, type, member, ret) do { 
    ret = (type *)((char *)ptr - (long long)(&((type *)0)->member));}while(0)

static inline void list_init_head(list_head_t *head)
{
    head->next = head;
    head->prev = head;
}

static inline void __list_add(list_head_t *new_node,
                              list_head_t *prev_node,
                              list_head_t *next_node)
{
    new_node->prev = prev_node;
    new_node->next = next_node;

    prev_node->next = new_node;
    next_node->prev = new_node;
}

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

static inline void list_add_prev(list_head_t *new_node,
                                 list_head_t *head)
{
    __list_add(new_node, head->prev, head);
}

static inline void list_add_next(list_head_t *new_node,
                                 list_head_t *head)
{
    __list_add(new_node, head, head->next);
}

static inline void list_del_node(list_head_t *node)
{
    __list_del(node->prev, node->next);
    list_init_head(node);
}

static inline int  list_is_empty(list_head_t *head)
{
    if ( (head->next == head) && (head->prev == head) )
    {
        return 1;
    }
    
    return 0;
}

static inline list_head_t *list_del_first_node(list_head_t *head)
{
    list_head_t *node = NULL;

    node = head->next;
    if ( node == head )
    {
        return NULL;
    }

    list_del_node(node);

    return node;
}
原文地址:https://www.cnblogs.com/liuzc/p/6522423.html