Chinaunix首页 | 论坛 | 博客
  • 博客访问: 630409
  • 博文数量: 51
  • 博客积分: 773
  • 博客等级: 军士长
  • 技术积分: 2392
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-07 21:32
文章分类
文章存档

2018年(1)

2013年(16)

2012年(34)

分类: LINUX

2012-06-18 22:12:55

   list-head结构在linux内核中用得非常多,这是一个双向链表结构,可以灵活嵌入其他数据结构中,对于建立链表非常方便。在Linux内核中,由于很多数据结构都需要连成一个链表进行管理,所以有了list-head将链表的管理独立出来,所以在内核编程不需要自己实现list-head的一些管理函数,可以直接调用。
  在open-iscsi用户态编程时,将这个文件从内核中拷贝出来了:
#ifndef __LIST_H__
#define __LIST_H__

#include
/* taken from linux kernel */

#undef offsetof
#ifdef __compiler_offsetof //这是gcc编译器中定义的,与下面用户自己定义的性质一样
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
//这里这个&符号取地址非常巧妙,而且->运算符比取地址符&的优先级高
这样就返回了member在type结构中的便宜量
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

//根据type数据结构中成员member的地址ptr,返回包含member的数据结构的地址
//因为member是type结构中的一个成员名,所以这里为了取得member的类型,需要通过(type *)0->member,费了半天事
#define list_container_of(ptr, type, member) ({ \
        const typeof( ((type *)0)->member ) *__mptr = (ptr); \
        (type *)( (char *)__mptr - offsetof(type,member) );})

/*list_head 结构原型*/
struct list_head {
struct list_head *next, *prev;
};

/*初始化一个list_head*/
#define LIST_HEAD_INIT(name) { &(name), &(name) }

/*定义一个list_head并初始化*/
#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 int list_empty(const struct list_head *head)
{
return head->next == head;
}

/*返回包含member的结构的地址*/
#define list_entry(ptr, type, member) \
list_container_of(ptr, type, member)

/*遍历链表,头节点不存放数据*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)

/*这个一般用于,pos是一个结构体指针,head是头节点,member是list_head结构在类型pos结构体中的成员名,通过list_head对结构体链表进行遍历*/
#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))

/*在pre和next中间插入new*/
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;
}
/*在链表头插入节点*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}

/*在链表尾插入节点*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*将节点摘连*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/*将一个节点摘连*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = entry->prev = NULL;
}

/*将以个节点摘连,然后初始化为头节点*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}

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

#endif

通过这个list.h文件,发现将双链表的管理操作独立出来后,对编程中经常用到双链表管理是非常方便的,在分析中发现typeof关键字起着至关重要的作用,后面的参考资料对typeof关键字进行了详细分析,而typeof((c)+1)则更有意思。

参考资料:
3.
阅读(4091) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~