Linux 内核链表移植(3)

#define HLIST_HEAD_INIT { .first = NULL }
#define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
#define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)

static inline int hlist_unhashed(const struct hlist_node *h)
{
    return !h->pprev;
}

static inline int hlist_empty(const struct hlist_head *h)
{
    return !h->first;
}

static inline void __hlist_del(struct hlist_node *n)
{
    struct hlist_node *next = n->next;
    struct hlist_node **pprev = n->pprev;
    *pprev = next;
    if (next) {
        next->pprev = pprev;
    }
}

static inline void hlist_del(struct hlist_node *n)
{
    __hlist_del(n);
    n->next = (hlist_node*) LIST_POISON1;
    n->pprev = (hlist_node**) LIST_POISON2;
}

static inline void hlist_del_init(struct hlist_node *n)
{
    if (n->pprev) {
        __hlist_del(n);
        INIT_HLIST_NODE(n);
    }
}

static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
    struct hlist_node *first = h->first;
    n->next = first;
    if (first) {
        first->pprev = &n->next;
    }
    h->first = n;
    n->pprev = &h->first;
}

/* next must be != NULL */
static inline void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
{
    n->pprev = next->pprev;
    n->next = next;
    next->pprev = &n->next;
    *(n->pprev) = n;
}

static inline void hlist_add_after(struct hlist_node *n, struct hlist_node *next)
{
    next->next = n->next;
    n->next = next;
    next->pprev = &n->next;

if (next->next) {
        next->next->pprev  = &next->next;
    }
}

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

#ifdef typeof

#define hlist_for_each(pos, head) \
    for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
        pos = pos->next)

#define hlist_for_each_safe(pos, n, head) \
    for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
        pos = n)

#define hlist_for_each_entry(tpos, pos, head, member) \
    for (pos = (head)->first; \
        pos && ({ prefetch(pos->next); 1;}) && \
            ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
        pos = pos->next)

#define hlist_for_each_entry_continue(tpos, pos, member) \
    for (pos = (pos)->next; \
        pos && ({ prefetch(pos->next); 1;}) && \
            ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
        pos = pos->next)

#define hlist_for_each_entry_from(tpos, pos, member) \
    for (; pos && ({ prefetch(pos->next); 1;}) && \
            ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
        pos = pos->next)

#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
    for (pos = (head)->first; \
        pos && ({ n = pos->next; 1; }) && \
            ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
        pos = n)
#else

#define hlist_for_each(pos, head) \
    for (pos = (head)->first; pos && prefetch(pos->next); pos = pos->next)

#define hlist_for_each_safe(pos, n, head) \
    for (pos = (head)->first; pos && ((n=pos->next) == n); pos = n)

#define hlist_for_each_entry(tpos, typeof_tpos, pos, head, member) \
    for (pos = (head)->first; \
        pos && prefetch(pos->next) && \
          ((tpos = hlist_entry(pos, typeof_tpos, member)) == tpos); \
        pos = pos->next)

#define hlist_for_each_entry_continue(tpos, typeof_tpos, pos, member) \
    for (pos = (pos)->next; \
        pos && prefetch(pos->next) && \
            ((tpos = hlist_entry(pos, typeof_tpos, member)) == tpos); \
        pos = pos->next)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2543bf701438d4aa277864c63ed849c5.html