Chinaunix首页 | 论坛 | 博客
  • 博客访问: 43881
  • 博文数量: 6
  • 博客积分: 131
  • 博客等级: 入伍新兵
  • 技术积分: 104
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-29 21:22
文章分类
文章存档

2013年(1)

2012年(5)

我的朋友

分类: LINUX

2012-10-29 09:39:20


处理链表:

内核基本都是双向链表,其基本结构:
  1. struct list_head {
  2.     struct list_head *next, *prev;
  3. };

xx/include/linux/list.h 文件记录了操作双向链表函数

初始化双向链表:
1.用INIT_LIST_HEAD函数初始化一个双向链表
  1. static inline void INIT_LIST_HEAD(struct list_head *list)
  2. {
  3.     list->next = list;
  4.     list->prev = list;
  5. }
2.使用宏创建并初始化一个双向链表
  1. #define LIST_HEAD_INIT(name) { &(name), &(name) }

  2. #define LIST_HEAD(name) \
  3.     struct list_head name = LIST_HEAD_INIT(name)

还有其他链表操作函数,认真分析还是很容易搞定,在此不多介绍;


下面介绍下内核中经常会用到的(跟链表相关的)宏:


1. list_entry: 与container_of功能相同;

点击(此处)折叠或打开

  1. /**
  2.  * list_entry - get the struct for this entry
  3.  * @ptr:    the &struct list_head pointer.
  4.  * @type:    the type of the struct this is embedded in.
  5.  * @member:    the name of the list_struct within the struct.
  6.  */
  7. #define list_entry(ptr, type, member) \
  8.     container_of(ptr, type, member)
2. list_for_each:向后(next)遍历head所指链表每个节点

点击(此处)折叠或打开

  1. /**
  2.  * list_for_each    -    iterate over a list
  3.  * @pos:    the &struct list_head to use as a loop cursor.
  4.  * @head:    the head for your list.
  5.  */
  6. #define list_for_each(pos, head) \
  7.     for (pos = (head)->next; pos != (head); pos = pos->next)
3. list_for_each_prev:向前(prev)遍历head所指链表每个节点

点击(此处)折叠或打开

  1. /**
  2.      * list_for_each_prev - iterate over a list backwards
  3.      * @pos: the &struct list_head to use as a loop cursor.
  4.      * @head: the head for your list.
  5.      */
  6.     #define list_for_each_prev(pos, head) \
  7.         for (pos = (head)->prev; pos != (head); pos = pos->prev)
4. list_for_each_entry:

点击(此处)折叠或打开

  1. /**
  2.  * list_for_each_entry    -    iterate over list of given type
  3.  * @pos:    the type * to use as a loop cursor.
  4.  * @head:    the head for your list.
  5.  * @member:    the name of the list_struct within the struct.
  6.  */
  7. #define list_for_each_entry(pos, head, member)                \
  8.     for (pos = list_entry((head)->next, typeof(*pos), member);    \
  9.      &pos->member != (head);     \
  10.      pos = list_entry(pos->member.next, typeof(*pos), member))
注:关于typeof可见:http://paullu.blog.51cto.com/828864/169145
举个例子:

点击(此处)折叠或打开

  1. struct kobject *k;

  2. list_for_each_entry(k, &kset->list, entry) {
  3.         if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
  4.             ret = kobject_get(k);
  5.             break;
  6.         }
  7. //这是在kset_find_obj_hinted函数中使用的遍历kset中的kobject链表,寻找与name同名的kobject;


#####################################################################
接下来看看Klist,内核中使用的非常频繁,它是对list_head的扩展

相关代码位于:xx/include/linux/klist.h,xx/lib/klist.c

点击(此处)折叠或打开

  1. struct klist {
  2.     spinlock_t        k_lock;
  3.     struct list_head    k_list;
  4.     void            (*get)(struct klist_node *);
  5.     void            (*put)(struct klist_node *);
  6. } __attribute__ ((aligned (sizeof(void *))));    /* 按指针大小对齐 */

点击(此处)折叠或打开

  1. struct klist_node {
  2.     void            *n_klist;    /* never access directly */
  3.     struct list_head    n_node;
  4.     struct kref        n_ref;
  5. };

点击(此处)折叠或打开

  1. struct klist_iter {
  2.     struct klist        *i_klist;
  3.     struct klist_node    *i_cur;
  4. };



待续。。。





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