Chinaunix首页 | 论坛 | 博客
  • 博客访问: 519624
  • 博文数量: 96
  • 博客积分: 2102
  • 博客等级: 上尉
  • 技术积分: 1695
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:12
文章分类

全部博文(96)

文章存档

2014年(2)

2012年(94)

分类: C/C++

2012-04-21 11:10:38

        include/linux/list.h 文件利用C 语言实现了封装较好的,好用的双链表函数集合。他实现了高效性和可移植性。对于高效性或许大家可以理解,但是对于可移植性来讲,确实让人十分纳闷,怎么可能呢?指针类型怎么可能全部适用,对于一个OS内部 ,数据结构类型很多,如何实现的呢?
        刚开始,我也觉得不可信。但是当看到linux list.h 实例中的宏
       #define list_entry(ptr,type,member)\
                   ((type *)((char*)(ptr)-(unsiagned long)(((type*)0)->member)))
      便会觉得刚才的疑问迎刃而解了,该宏的作用时,已知结构体中某个变量的地址,求得该节点的地址。其中:将ptr强制转换为char*的原因是使的ptr以一个单位来变动。(type*)0->member获取的便是memeber成员对应的偏移量。//虽然一个指针没有被实例化,但是依旧拥有对应的成员。

#ifndef __LIST_H
#define __LIST_H
#if defined(WIN32)
#define INLINE __inline
#else
#define INLINE inline
#endif

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

不含有数据项的结构,注定了它的通用性和未来使用的灵活性,将该类型的变量作为需要链表的链接的结构体的成员。将相应的节点链接。 #define LIST_HEAD_INIT(name) { &(name), &(name)} 实现对struct list_head 变量的初始化,形成循环链表。 #define LIST_HEAD(name) \ struct list_head name = LIST_HEAD_INIT(name) #define INIT_LIST_HEAD(ptr) do { \ (ptr)->next = (ptr); (ptr)->prev = (ptr); \ } while (0)
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;
}将new节点插入next前面和prev后面。

static INLINEvoid list_add(struct list_head*new,struct list_head*head)
{

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

插入head的后面,应用于栈。因为head为 带头节点。重头入,重头出,满足条件。
static INLINEvoid list_add_tail(struct list_head*new,struct list_head*head)
{

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

插入head(为链的头部,他的前一个节点为链的最后一个节点)的前面,应用于队列的入到队尾。
static INLINEvoid __list_del(struct list_head*prev,struct list_head*next)
{

next->prev= prev;
prev->next= next; }
删除prev和next之间的所有节点。
static INLINEvoid list_del(struct list_head*entry)
{

__list_del(entry->prev, entry->next);
entry->next= (void*)0;
entry->prev= (void*)0;
}

删除entry节点。
static INLINE void list_del_init(struct list_head*entry)
{

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

删除节点entry,并且将该节点初始化。
static INLINEvoid list_move(struct list_head*list,struct list_head*head)
{
__list_del(list->prev, list->next);
 list_add(list, head);
}

删除list节点,将list插入head(带头节点)节点后面。入栈!
static INLINEvoid list_move_tail(struct list_head*list,struct list_head*head)
{

 __list_del(list->prev, list->next);
list_add_tail(list, head); }
删除list节点,将list插入head节点前面。追加在该链的末尾。如队列!
static INLINE int list_empty(struct list_head*head)
{
return head->next== head;
}

判断该链是否为空,在删除节点时要及时进行判断。
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节点和at节点交换
static INLINEvoid list_splice(struct list_head*list,struct list_head*head)
{

if
(!list_empty(list)) __list_splice(list, head);
}

如果节点不为空,将list和head后面的节点互换。
static INLINEvoid list_splice_init(struct list_head*list, struct list_head*head)
{

if
(!list_empty(list)) { __list_splice(list, head);
INIT_LIST_HEAD(list); } }
#define list_entry(ptr, type, member) \
   ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
获取该结构体节点的首地址。
#define list_for_each(pos, head) \
   for (pos = (head)->next; pos != (head); \
      pos = pos->next)
向后遍历 #define list_for_each_prev(pos, head) \ for (pos = (head)->prev; pos != (head); \ pos = pos->prev)
向前遍历 #define list_for_each_safe(pos, n, head) \ for (pos = (head)->next, n = pos->next; pos != (head); \ pos = n, n = pos->next) #define list_for_each_entry(pos, head, member) \ for (pos = list_entry((head)->next, typeof(*pos), member); \ &pos->member != (head); \ pos = list_entry(pos->member.next, typeof(*pos), member))
#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)) #endif 与#ifndef __LIST_H搭配

static inline ::其中static 标识为静态函数,对此函数作用域限制在本文件中,因此具有隐藏信息
的作用。inline标识了这个函数 对于编译程序是可见的,也就是编译程序在调用这个函数时就展开,关
键字inline与函数定义体放在一起才使函数成为内联函数,则一般放在头文件内部。

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