打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
从内核工具中分离出来的链表工具(续代码一)
我从内核工具中分离出来的链表工具(续代码一)
2007年11月19日 星期一 下午 3:29


最近写东西老要用到链表和开链散列,又懒得自己写。于是想到Linux内核里有很完善的链表的实现可以利用,所以花了点时间将内核里的list和hlist的实现分离出来放在自己的代码树立用。

弄 完了又发现其实这个东西可以到处利用,这就又完善了一下。这里发给大家,抛砖引玉,也算是代码复用。大家有机会可以测试一下看还有什么问题没有。结构和原 理很简单,主要是移植性方面要考虑的比较多,一些多和硬件相关的地方我就写了宏直接跳过去没管了。经过我的测试目前还没有什么问题,C和C++都应该可以 用。见笑。

它的好处嘛,我认为有三点,一是空间利用比较精细,list和hlist也就一个指针空间的差别,看来内核中还是很注意空间节 省。二是比较灵活,只要你想链起来的结构,都能用。另外呢,它提供了很多好用的函数和宏,用法也很简单(上网一搜有很多分析的)。这些函数和宏里面有一些 在一般用户空间是没有多大意义的(比如_rcu类的函数主要是避免多CPU并行时互斥的,prefetch和rcu_dereference也是跟硬件有 关的),除非你在共享内存里并发操作,或者线程操作。大家也可以考虑在这方面完善一下。

由于文章长度有限制,所以下一篇是代码,我稍微做过一些修改,并加上了中文注释。

经过提醒,我还是把代码的许可证发上来一下好了:

/*
    <list.h>
    Copyright (C) 2007 Li Yi (pAnk7.yArdbird) <pank7yardbird (AT) gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

*/
代码还是太长,所以只好分两部分了……

这一部分是list的。

/*

* 描述:                       双循环链表的实现,拷贝自Linux内核(2.6.20.1)。
*                             稍作修改。头文件,使用时只需包含此头文件即可。
* 文件名:                     list.h
* 作者:                       pAnk7.yArdbird <pank7yardbird (AT) gmail.com>
* 日期 & 时间:                Tue Nov 13 12:49:25 2007
*/

#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H


#ifdef __cplusplus              /* 使C++程序也能用。 */

extern "C" {
#endif
#if !defined(__cplusplus) && defined(WIN32)
#define inline
#endif

#define LIST_POISON1    ((void *) 0x00100100)
/* 链表节点指针的特殊标志。 */
#define LIST_POISON2    ((void *) 0x00200200)
#ifndef NULL                   
/* C语言中没有定义NULL。 */
#define NULL            ((void *)0)
#endif /* NULL */


#ifndef offsetof                /* 宏container_of需要使用。 */
#ifdef __compiler_offsetof     /* 有的系统自带有此功能的宏。 */
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
#endif /* offsetof */

#define container_of(ptr, type, member) ({ \
      const typeof( ((type *)0)->member ) *__mptr = (ptr); \
      (type *)( (char *)__mptr - offsetof(type,member) );})

/* 并发互斥的处理,在用户空间的程序大多用不着。可考虑改善这里的功能。 */

#define smp_read_barrier_depends()      do { } while(0)
#define rcu_dereference(p)      ({ \
      typeof(p) _________p1 = p; \
      smp_read_barrier_depends(); \
      (_________p1); \
    })

/* 利用CPU的cache来提高效率。 */

static inline void prefetch(const void *x) { ; }

/*
   * Simple doubly linked list implementation.
   * 简单的双循环链表的实现。
   *
   * 当处理整个链表而不是单个节点的时候,有一些内部函数(“__xxx”)更有用,
   * 因为有时候我们已经知道了前后节点,所以直接使用内部函数我们可以让写出
   * 更好的代码。
   */


/*
   * 在你需要链接起来的数据结构中加入一个此结构体(注意不能是list_head指针)
   * 成员。你也可以加入多个此结构体的成员,从而可以让同一个数据结构可以加入多
   * 个链表。
   */

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

#define LIST_HEAD_INIT(name) { &(name), &(name) } /* 初始化。 */

/* 定义变量并初始化。 */

#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)


/* 初始化函数。 */

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

/*
   * 在两个已知的连续的节点中插入一个新的节点。
   *
   * 此函数仅作为链表内部操作使用,链表内部我们已经知道了前后节点。
   */

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

/**
   * 加入一个新的节点。
   * @newentry: new entry to be added
   * @head: list head to add it after
   *
   * 在指定链表头部后面加入一个新的节点。可以方便地用来实现栈。
   */

static inline void list_add(struct list_head *newentry, struct list_head *head)
{
    __list_add(newentry, head, head->next);
}

/**
   * 加入一个新的节点。
   * @newentry: new entry to be added
   * @head: list head to add it before
   *
   * 在指定链表头部前面加入一个新的节点(即加在尾部)。可以方便地用来实现队列。
   */

static inline void list_add_tail(struct list_head *newentry,
                                   struct list_head *head)
{
    __list_add(newentry, head->prev, head);
}

/*
   * 在两个已知的连续的节点之间插入一个新的节点。由于我的修改此函数和之前的
   * __list_add没有区别,但是为后续开发留下接口。
   *
   * 此函数仅作为链表的内部操作使用。
   */

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

/**
   * 在一个rcu保护下的链表中加入一个新的节点。与之前的list_add没有区别,
   * 但是为后续开发留下接口。
   * @newentry: new entry to be added
   * @head: list head to add it after
   */

static inline void list_add_rcu(struct list_head *newentry,
                                  struct list_head *head)
{
    __list_add_rcu(newentry, head, head->next);
}
/**
   * 在一个rcu保护下的链表中加入一个新的节点。与之前的list_add_tail没有区别,
   * 但是为后续开发留下接口。
   * @newentry: new entry to be added
   * @head: list head to add it before
   */

static inline void list_add_tail_rcu(struct list_head *newentry,
                                       struct list_head *head)
{
    __list_add_rcu(newentry, head->prev, head);
}

/*
   * 使前后节点相互指向对方,从而删掉一个节点。但是此节点的空间还得自己释放。
   *
   * 此函数仅作为链表的内部操作使用。
   */

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

/**
   * 从链表中删除一个节点。
   * @entry: the element to delete from the list.
   * 注意:调用此函数后list_empty并不返回真,节点处于一个未知状态。
   */

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

/**
   * 从链表中删除一个节点,无需重新初始化。
   * @entry: the element to delete from the list.
   */

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

/**
   * 用新的节点替换旧的节点。
   * @old : the element to be replaced
   * @newentry : the new element to insert
   * Note: if 'old' was empty, it will be overwritten.
   */

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

/* 用新的节点替换旧的节点,并将旧的节点初始化。 */

static inline void list_replace_init(struct list_head *old,
                                       struct list_head *newentry)
{
    list_replace(old, newentry);
    INIT_LIST_HEAD(old);
}

/**
   * list_replace_rcu - replace old entry by new one
   * 用新的节点替换旧的节点。
   * @old : the element to be replaced
   * @newentry : the new element to insert
   *
   * The @old entry will be replaced with the @newentry entry atomically.
   * Note: @old should not be empty.
   */

static inline void list_replace_rcu(struct list_head *old,
                                      struct list_head *newentry)
{
    newentry->next = old->next;
    newentry->prev = old->prev;
    newentry->next->prev = newentry;
    newentry->prev->next = newentry;
    old->prev = (list_head *)LIST_POISON2;
}

/**
   * list_del_init - deletes entry from list and reinitialize it.
   * 从链表中删除一个节点,并重新初始化该节点。
   * @entry: the element to delete from the list.
   */

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

/**
   * list_move - delete from one list and add as another's head
   * 将一个节点从链表中删除,并加入到另外一个链表的头部(其实完全可以是同一个链表)。
   * @list: the entry to move
   * @head: the head that will precede our entry
   */

static inline void list_move(struct list_head *list, struct list_head *head)
{
    __list_del(list->prev, list->next);
    list_add(list, head);
}

/**
   * list_move_tail - delete from one list and add as another's tail
   * 将一个节点从链表中删除,并加入到另外一个链表的尾部(其实完全可以是同一个链表)。
   *
   * @list: the entry to move
   * @head: the head that will follow our entry
   */

static inline void list_move_tail(struct list_head *list,
                                    struct list_head *head)
{
    __list_del(list->prev, list->next);
    list_add_tail(list, head);
}

/**
   * list_is_last - tests whether @list is the last entry in list @head
   * 测试一个节点是否是一个链表的最后一个节点。
   * @list: the entry to test
   * @head: the head of the list
   */

static inline int list_is_last(const struct list_head *list,
                                 const struct list_head *head)
{
    return list->next == head;
}

/**
   * list_empty - tests whether a list is empty
   * 测试一个链表是否为空。
   * @head: the list to test.
   */

static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}

/**
   * list_empty_careful - tests whether a list is empty and not being modified
   * 测试一个链表是否为空,并且没有在被修改。
   * @head: the list to test
   *
   * Description:
   * tests whether a list is empty _and_ checks that no other CPU might be
   * in the process of modifying either member (next or prev)
   *
   * NOTE: using list_empty_careful() without synchronization
   * can only be safe if the only activity that can happen
   * to the list entry is list_del_init(). Eg. it cannot be used
   * if another CPU could re-list_add() it.
   */

static inline int list_empty_careful(const struct list_head *head)
{
    struct list_head *next = head->next;
    return (next == head) && (next == head->prev);
}

/* 将两个链表合二为一。list的节点将被剔除。 */

static inline void __list_splice(struct list_head *list,
                                   struct list_head *head)
{
    struct list_head *first = list->next;
    struct list_head *last = list->prev;
    struct list_head *at = head->next;

    first->prev = head;
    head->next = first;

    last->next = at;
    at->prev = last;
}

/**
   * list_splice - join two lists
   * 将两个链表合二为一。list的节点将被剔除。
   * @list: the new list to add.
   * @head: the place to add it in the first list.
   */

static inline void list_splice(struct list_head *list,
                                 struct list_head *head)
{
    if (!list_empty(list))
      __list_splice(list, head);
}

/**
   * list_splice_init - join two lists and reinitialise the emptied list.
   * 将两个链表合二为一,并重新初始化空链表(list)。
   * @list: the new list to add.
   * @head: the place to add it in the first list.
   *
   * The list at @list is reinitialised
   */

static inline void list_splice_init(struct list_head *list,
                                      struct list_head *head)
{
    if (!list_empty(list)) {
      __list_splice(list, head);
      INIT_LIST_HEAD(list);
    }
}

/**
   * 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    -    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; prefetch(pos->next), pos != (head); \
            pos = pos->next)


/**
   * __list_for_each    -    iterate over a list
   * 迭代一个链表。
   * @pos:    the &struct list_head to use as a loop cursor.
   * @head:    the head for your list.
   *
   * This variant differs from list_for_each() in that it's the
   * simplest possible list iteration code, no prefetching is done.
   * Use this for code that knows the list to be very short (empty
   * or 1 entry) most of the time.
   * 此宏没有使用prefetch。
   */

#define __list_for_each(pos, head) \
        for (pos = (head)->next; pos != (head); pos = pos->next)

/**
   * list_for_each_prev    -    iterate over a list backwards
   * 反向迭代一个链表。
   * @pos:    the &struct list_head to use as a loop cursor.
   * @head:    the head for your list.
   */
#define list_for_each_prev(pos, head)
\
    for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
            pos = pos->prev)

/**
   * 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
   * 迭代一个链表,与list_for_each不同的地方在于此宏给出的是结构体指针。
   * @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))


/**
   * list_for_each_entry_reverse - iterate backwards 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_reverse(pos, head, member) \
    for (pos = list_entry((head)->prev, typeof(*pos), member); \
         prefetch(pos->member.prev), &pos->member != (head); \
         pos = list_entry(pos->member.prev, typeof(*pos), member))


/**
   * list_prepare_entry - prepare a pos entry for use in
   * list_for_each_entry_continue
   * 为list_for_each_entry_continue准备一个节点。
   * @pos:    the type * to use as a start point
   * @head:    the head of the list
   * @member:    the name of the list_struct within the struct.
   *
   * Prepares a pos entry for use as a start point in
   * list_for_each_entry_continue.
   */
#define list_prepare_entry(pos, head, member)
\
    ((pos) ? : list_entry(head, typeof(*pos), member))


/**
   * list_for_each_entry_continue - continue iteration over list of given type
   * 从pos的下一个节点处开始继续迭代一个链表。
   * @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.
   *
   * Continue to iterate over list of given type, continuing after
   * the current position.
   */

#define list_for_each_entry_continue(pos, head, member) \
    for (pos = list_entry(pos->member.next, typeof(*pos), member); \
         prefetch(pos->member.next), &pos->member != (head); \
         pos = list_entry(pos->member.next, typeof(*pos), member))


/**
   * list_for_each_entry_from - iterate over list of given type from the current
   * point
   * 从pos处开始继续迭代一个链表。
   * @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.
   *
   * Iterate over list of given type, continuing from current position.
   */

#define list_for_each_entry_from(pos, head, member) \
    for (; prefetch(pos->member.next), &pos->member != (head); \
         pos = list_entry(pos->member.next, typeof(*pos), member))


/**
   * list_for_each_entry_safe - iterate over list of given type safe against
   * removal of list entry
   * 安全地迭代一个链表。使用结构体指针。
   * @pos:    the type * to use as a loop cursor.
   * @n:        another type * to use as temporary storage
   * @head:    the head for your list.
   * @member:    the name of the list_struct within the struct.
   */

#define list_for_each_entry_safe(pos, n, head, member) \
    for (pos = list_entry((head)->next, typeof(*pos), member), \
        n = list_entry(pos->member.next, typeof(*pos), member); \
         &pos->member != (head); \
         pos = n, n = list_entry(n->member.next, typeof(*n), member))


/**
   * list_for_each_entry_safe_continue
   * 顾名思义。
   * @pos:    the type * to use as a loop cursor.
   * @n:        another type * to use as temporary storage
   * @head:    the head for your list.
   * @member:    the name of the list_struct within the struct.
   *
   * Iterate over list of given type, continuing after current point,
   * safe against removal of list entry.
   */

#define list_for_each_entry_safe_continue(pos, n, head, member) \
    for (pos = list_entry(pos->member.next, typeof(*pos), member), \
        n = list_entry(pos->member.next, typeof(*pos), member); \
         &pos->member != (head); \
         pos = n, n = list_entry(n->member.next, typeof(*n), member))


/**
   * list_for_each_entry_safe_from
   * 顾名思义。
   * @pos:    the type * to use as a loop cursor.
   * @n:        another type * to use as temporary storage
   * @head:    the head for your list.
   * @member:    the name of the list_struct within the struct.
   *
   * Iterate over list of given type from current point, safe against
   * removal of list entry.
   */

#define list_for_each_entry_safe_from(pos, n, head, member) \
    for (n = list_entry(pos->member.next, typeof(*pos), member); \
         &pos->member != (head); \
         pos = n, n = list_entry(n->member.next, typeof(*n), member))


/**
   * list_for_each_entry_safe_reverse
   * 顾名思义。
   * @pos:    the type * to use as a loop cursor.
   * @n:        another type * to use as temporary storage
   * @head:    the head for your list.
   * @member:    the name of the list_struct within the struct.
   *
   * Iterate backwards over list of given type, safe against removal
   * of list entry.
   */

#define list_for_each_entry_safe_reverse(pos, n, head, member) \
    for (pos = list_entry((head)->prev, typeof(*pos), member), \
        n = list_entry(pos->member.prev, typeof(*pos), member); \
         &pos->member != (head); \
         pos = n, n = list_entry(n->member.prev, typeof(*n), member))


/**
   * list_for_each_rcu    -    iterate over an rcu-protected list
   * 顾名思义。
   * @pos:    the &struct list_head to use as a loop cursor.
   * @head:    the head for your list.
   *
   * This list-traversal primitive may safely run concurrently with
   * the _rcu list-mutation primitives such as list_add_rcu()
   * as long as the traversal is guarded by rcu_read_lock().
   */

#define list_for_each_rcu(pos, head) \
    for (pos = (head)->next; \
        prefetch(rcu_dereference(pos)->next), pos != (head); \
            pos = pos->next)


#define __list_for_each_rcu(pos, head) \
    for (pos = (head)->next; \
        rcu_dereference(pos) != (head); \
            pos = pos->next)


/**
   * list_for_each_safe_rcu
   * 顾名思义。
   * @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.
   *
   * Iterate over an rcu-protected list, safe against removal of list entry.
   *
   * This list-traversal primitive may safely run concurrently with
   * the _rcu list-mutation primitives such as list_add_rcu()
   * as long as the traversal is guarded by rcu_read_lock().
   */

#define list_for_each_safe_rcu(pos, n, head) \
        for (pos = (head)->next; \
        n = rcu_dereference(pos)->next, pos != (head); \
        pos = n)


/**
   * list_for_each_entry_rcu    -    iterate over rcu 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.
   *
   * This list-traversal primitive may safely run concurrently with
   * the _rcu list-mutation primitives such as list_add_rcu()
   * as long as the traversal is guarded by rcu_read_lock().
   */

#define list_for_each_entry_rcu(pos, head, member) \
    for (pos = list_entry((head)->next, typeof(*pos), member); \
        prefetch(rcu_dereference(pos)->member.next), \
            &pos->member != (head); \
        pos = list_entry(pos->member.next, typeof(*pos), member))


/**
   * list_for_each_continue_rcu
   * 顾名思义。
   * @pos:    the &struct list_head to use as a loop cursor.
   * @head:    the head for your list.
   *
   * Iterate over an rcu-protected list, continuing after current point.
   *
   * This list-traversal primitive may safely run concurrently with
   * the _rcu list-mutation primitives such as list_add_rcu()
   * as long as the traversal is guarded by rcu_read_lock().
   */

#define list_for_each_continue_rcu(pos, head) \
    for ((pos) = (pos)->next; \
        prefetch(rcu_dereference((pos))->next), (pos) != (head); \
            (pos) = (pos)->next)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
linux 内核hash链表
RCU
linux内核中链表的实现
[保留] 在用户空间编程使用linux内核链表list,hlist宏定义和操作
linux 中list list_entry
Linux内核中list
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服