Chinaunix首页 | 论坛 | 博客
  • 博客访问: 558747
  • 博文数量: 105
  • 博客积分: 3274
  • 博客等级: 中校
  • 技术积分: 1161
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-21 12:14
文章分类

全部博文(105)

文章存档

2011年(1)

2010年(104)

分类: LINUX

2010-08-03 21:14:32

链表结构体定义在文件中,如下

/*

 * Simple doubly linked list implementation.

 *

 * Some of the internal functions ("__xxx") are useful when

 * manipulating whole lists rather than single entries, as

 * sometimes we already know the next/prev entries and we can

 * generate better code by using them directly rather than

 * using the generic single-entry routines.

 */

 

struct list_head {

       struct list_head *next, *prev;

};

注意list_head这个奇怪的名字,这个名字暗示了链表不存在头节点,但是由于可以从链表中的任何一个节点开始遍历,所以任何一个节点都可以起到头节点的效果,因此每个独立节点都可以被称作是链表头。

一个list_head结构体本身没有什么意义,通常需要把它嵌入到自己的结构体中。

我们先看下链表库所提供的链表操作接口,如下

Function蓝色部分为源代码

INIT_LIST_HEAD() Initializes the list head 初始化表头

static inline void INIT_LIST_HEAD(struct list_head *list)

{

      list->next = list;

      list->prev = list;

}

list_add() Adds an element after the list head 在表头后增加一个元素

/*

 * Insert a new entry between two known consecutive entries.

 *

 * This is only for internal list manipulation where we know

 * the prev/next entries already!

 */

#ifndef CONFIG_DEBUG_LIST

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;

}

#else

extern void __list_add(struct list_head *new,

                       struct list_head *prev,

                       struct list_head *next);

#endif

 

/**

 * list_add - add a new entry

 * @new: new entry to be added

 * @head: list head to add it after

 *

 * Insert a new entry after the specified head.

 * This is good for implementing stacks.

 */

static inline void list_add(struct list_head *new, struct list_head *head)

{

      __list_add(new, head, head->next);

}

list_add_tail() Adds an element to the tail of the list在链表尾部增加一个元素

/**

 * list_add_tail - add a new entry

 * @new: new entry to be added

 * @head: list head to add it before

 *

 * Insert a new entry before the specified head.

 * This is useful for implementing queues.

 */

static inline void list_add_tail(struct list_head *new, struct list_head *head)

{

      __list_add(new, head->prev, head);

}

list_del() Deletes an element from the list从链表删除一个元素

/*

 * Delete a list entry by making the prev/next entries

 * point to each other.

 *

 * This is only for internal list manipulation where we know

 * the prev/next entries already!

 */

static inline void __list_del(struct list_head * prev, struct list_head * next)

{

      next->prev = prev;

      prev->next = next;

}

 

/**

 * list_del - deletes entry from list.

 * @entry: the element to delete from the list.

 * Note: list_empty() on entry does not return true after this, the entry is

 * in an undefined state.

 */

#ifndef CONFIG_DEBUG_LIST

static inline void list_del(struct list_head *entry)

{

      __list_del(entry->prev, entry->next);

      entry->next = LIST_POISON1;

      entry->prev = LIST_POISON2;

}

#else

extern void list_del(struct list_head *entry);

#endif

list_replace() Replaces an element in the list with another用另一个元素代替链表中的某一个元素

/**

 * list_replace - replace old entry by new one

 * @old : the element to be replaced

 * @new : the new element to insert

 *

 * If @old was empty, it will be overwritten.

 */

static inline void list_replace(struct list_head *old,

                      struct list_head *new)

{

      new->next = old->next;

      new->next->prev = new;

      new->prev = old->prev;

      new->prev->next = new;

}

list_entry() Loops through all nodes in the 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)

这里调用的container_of是通过结构体成员的指针找到对应结构体的指针,这个技巧在linux内核编程使用常用。本例中container_of的第一个参数是结构体成员的指针,第2个参数为整个结构体的类型,第3个参数为传入的第一个参数即结构体成员的类型,container_of()返回的是整个结构体的指针。

看看container_of是如何让实现的,非常具有技巧性,如下

/**

 * container_of - cast a member of a structure out to the containing structure

 * @ptr:   the pointer to the member.

 * @type: the type of the container struct this is embedded in.

 * @member:   the name of the member within the struct.

 *

 */

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

  const typeof( ((type *)0)->member ) *__mptr = (ptr); \

  (type *)( (char *)__mptr - offsetof(type,member) );})

list_for_each_entry()list_for_each_entry_safe()

Simpler list iteration interfaces简化的链表递归接口

/**

 * 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))

list_empty() Checks whether there are any elements in the list检查链表是否为空

/**

 * 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_splice() Joins one list with another2个链表联合起来

static inline void __list_splice(const struct list_head *list,

                       struct list_head *prev,

                       struct list_head *next)

{

      struct list_head *first = list->next;

      struct list_head *last = list->prev;

 

      first->prev = prev;

      prev->next = first;

 

      last->next = next;

      next->prev = last;

}

 

/**

 * list_splice - join two lists, this is designed for stacks

 * @list: the new list to add.

 * @head: the place to add it in the first list.

 */

static inline void list_splice(const struct list_head *list,

                      struct list_head *head)

{

      if (!list_empty(list))

           __list_splice(list, head, head->next);

}

阅读(2652) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~