Chinaunix首页 | 论坛 | 博客
  • 博客访问: 317334
  • 博文数量: 113
  • 博客积分: 446
  • 博客等级: 下士
  • 技术积分: 1229
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-09 16:01
个人简介

Let's go!!!!!

文章分类

全部博文(113)

文章存档

2019年(5)

2018年(4)

2017年(9)

2016年(5)

2015年(39)

2014年(6)

2013年(28)

2012年(17)

分类: C/C++

2018-11-14 15:15:19

inux内核链表-通用链表的实现
  最近编程总想着参考一些有名的开源代码是如何实现的,因为要写链表就看了下linux内核中对链表的实现。

  链表是一种非常常见的数据结构,特别是在动态创建相应数据结构的情况下更是如此,然而在操作系统内核中,动态创建相应的数据结构尤为频繁。由于不带数据域所以Linux中的这种链表是通用的,在如何情况下,只要需要链表的数据结构包含它就行了。

  链表只包含两个指针 

struct list_head {
   struct list_head *next, *prev;
};
  数据结构如果需要链表只需要包含它就行

复制代码
typedef struct s_DASTUC
{
 int ds_stus;
 int ds_type;
 struct list_head ds_list;
 int ds_count;          
}
复制代码
  通过链表的地址获取链表所在的数据结构地址

#define list_entry(ptr, type, number)    \
                   ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->number)))
网上看到过有人说为什么不把链表放到数据结构首部,这样链表所在的地址就是数据结构的地址,何必实现list_entry(ptr, type, number),不要去限制数据结构调用链表的写法。

  详细的网上都有博客了,这里博主只是为了记下知识点而已,下面是从Linux源码List.h拿出来的部分实现:
#ifndef _MYLIST_H
#define _MYLIST_H

#define LIST_POISON1    NULL
#define LIST_POISON2    NULL

/*
* 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;
};

#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;
}

/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
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;
}


/**
* 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 - 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);
}

/*
* 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.
*/
static inline void list_del(struct list_head *entry)
{
   __list_del(entry->prev, entry->next);
   entry->next = LIST_POISON1;
   entry->prev = LIST_POISON2;
}

/**
* 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;
}

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

/**
* 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);
}

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

#define list_first_entry(ptr, type, member) \
                   list_entry((ptr)->next, type, member)
#define list_entry(ptr, type, number)    \
                   ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->number)))

#endif



#include
#include
#include
#include
#include

MODULE_LICENSE("GPL");
MODULE_AUTHOR("David Xie");
MODULE_DESCRIPTION("List Module");
MODULE_ALIAS("List module");

struct student  //代表一个实际节点的结构
{
   char name[100];
   int num;
   struct list_head list;   //内核链表里的节点结构
};

struct student *pstudent;      
struct student *tmp_student;
struct list_head student_list;  
struct list_head *pos;

int mylist_init(void)
{
int i = 0;

//初始化一个链表,其实就是把student_list的prev和next指向自身
INIT_LIST_HEAD(&student_list);  

pstudent = kmalloc(sizeof(struct student)*5,GFP_KERNEL);//向内核申请5个student结构空间
memset(pstudent,0,sizeof(struct student)*5); //清空,这两个函数可以由kzalloc单独做到

for(i=0;i<5;i++)
{ //为结构体属性赋值
sprintf(pstudent[i].name,"Student%d",i+1);
pstudent[i].num = i+1;
//加入链表节点,list_add的话是在表头插入,list_add_tail是在表尾插入
list_add( &(pstudent[i].list), &student_list);//参数1是要插入的节点地址,参数2是链表头地址
}

list_for_each(pos,&student_list) //list_for_each用来遍历链表,这是个宏定义
                                //pos在上面有定义
{
//list_entry用来提取出内核链表节点对应的实际结构节点,即根据struct list_head来提取struct student
//第三个参数list就是student结构定义里的属性list
//list_entry的原理有点复杂,也是linux内核的一个经典实现,这个在上面那篇链接文章里也有讲解
tmp_student = list_entry(pos,struct student,list);
//打印一些信息,以备验证结果
printk("<0>student %d name: %s/n",tmp_student->num,tmp_student->name);
}

return 0;
}


void mylist_exit(void)
{
int i ;
/* 实验:将for换成list_for_each来遍历删除结点,观察要发生的现象,并考虑解决办法 */
for(i=0;i<5;i++)
{
//额,删除节点,只要传个内核链表节点就行了
list_del(&(pstudent[i].list));    
}
//释放空间
kfree(pstudent);
}

module_init(mylist_init);
module_exit(mylist_exit);


阅读(413) | 评论(0) | 转发(0) |
0

上一篇:boost bind

下一篇:自旋锁vs信号量

给主人留下些什么吧!~~