linux数据结构——链表

linux内核数据结构——链表


源码分析

重要宏定义

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#ifndef 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)); })
#endif

结构体

struct list_head {
    struct list_head *next, *prev;
};
 
struct hlist_head {
    struct hlist_node *first;
};
 
struct hlist_node {
    struct hlist_node *next, **pprev;
};

链表操作

初始化
#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;
}
插入
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;
}
/* head insert */
static inline void list_add(struct list_head *new, struct list_head *head)
{
	__list_add(new, head, head->next);
}
/* tail insert */
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_entry(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
}

static inline void list_del(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	entry->next = LIST_POISON1;
	entry->prev = LIST_POISON2;
}
遍历
#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)

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

/* list_for_each_safe - iterate over a list safe against removal of list entry */
#define list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); \
		pos = n, n = pos->next)

/* list_for_each_entry	-	iterate over list of given type */
#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))

移植

/* 1、注释掉如下头文件 */
//#include <linux/types.h>
//#include <linux/stddef.h>
//#include <linux/poison.h>
//#include <linux/const.h>

/* 2、添加如下宏定义 */
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#ifndef 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)); })
#endif
 
#define prefetch(x) ((void)x)
 
#define LIST_POISON1  (NULL)
#define LIST_POISON2  (NULL)

/* 3、添加如下结构体定义 */
struct list_head {
    struct list_head *next, *prev;
};
 
/* 4、屏蔽哈希表相关操作接口 */

仿写的单向循环链表

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#ifndef 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)); })
#endif

struct list_head {
    struct list_head *next;
};

 
#define LIST_HEAD_INIT(name) { &(name)}
    
#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)

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

#define list_first_entry(ptr, type, member) \
    list_entry((ptr)->next, type, member)


/**
 * list_for_end - serch for end entry
 * @pos:    the &struct list_head pointer.
 * @head:   list head
 */
#define list_for_end(pos, head) \
    for (pos = (head)->next; pos->next != (head); pos = pos->next)

/**
 * list_for_end_safe - serch for end entry against removal of list entry
 * @pos:    the &struct list_head pointer.
 * @n:      the &struct list_head pointer.
 * @head:   list head
 */
#define list_for_end_safe(pos, n, head) \
    for (pos = (head)->next, n = pos->next; n != (head); \
        pos = n, n = pos->next)

/**
 * list_for_end_entry - serch for end entry of given type list. 
 * @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_end_entry(pos, head, member) \
    for (pos = list_entry((head)->next, typeof(*pos), member);  \
         pos->member.next != (head);    \
         pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * list_for_end_entry_safe - serch for end entry of given type list against removal of list entry
 * @pos:    the type * to use as a loop cursor.
 * @n:      another type * to use as temporary storage
 * @head:   the head for your list.
 * @member: the name of the list_struct within the struct.
 */         
#define list_for_end_entry_safe(pos, n, head, member) \
    for (pos = list_entry((head)->next, typeof(*pos), member),  \
        n = list_entry(pos->member.next, typeof(*pos), member); \
         &n->member != (head);                    \
         pos = n, n = list_entry(n->member.next, typeof(*n), member))

/**
 * list_for_each    -   iterate over a list
 * @pos:    the &struct list_head to use as a loop cursor.
 * @head:   the head for your list.
 */
#define list_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)

/**
 * list_for_each_safe - iterate over a list safe against removal of list entry
 * @pos:    the &struct list_head to use as a loop cursor.
 * @n:      another &struct list_head to use as temporary storage
 * @head:   the head for your list.
 */
#define list_for_each_safe(pos, n, head) \
    for (pos = (head)->next, n = pos->next; pos != (head); \
        pos = n, n = pos->next)

/**
 * 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);  \
         &pos->member != (head);    \
         pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:    the type * to use as a loop cursor.
 * @n:      another type * to use as temporary storage
 * @head:   the head for your list.
 * @member: the name of the list_struct within the struct.
 */
#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))


#define list_for_item(pos, head, item, pre) \
    for (pre = head, pos = (head)->next; pos != (item); pre = pos, pos = pos->next)

#define list_for_item_safe(pos, n, head, item, pre) \
    for (pre = head, pos = (head)->next, n = pos->next; \
        pos != (item); pre = pos, pos = n, n = pos->next)

#define list_for_item_entry(pos, head, item, pre, member) \
    for (pre = list_entry((head), typeof(*pos), member),\
        pos = list_entry((head)->next, typeof(*pos), member);  \
        pos != (item); \
        pre = pos, pos = list_entry(pos->member.next, typeof(*pos), member))


#define list_for_item_entry_safe(pos, n, head, item, pre, member) \
    for (pre = list_entry((head), typeof(*pos), member),\
         pos = list_entry((head)->next, typeof(*pos), member),\
         n = list_entry((pos)->member.next, typeof(*pos), member);  \
         pos != (item); \
         pre = pos, pos= n, n = list_entry(pos->member.next, typeof(*pos), member))

/**
 * list_add_tail_entry - add a new entry to list of given type.
 * @pos: the type *
 * @new: the type *
 * @head: list head
 * @member: the name of the list_struct within the struct.
 */
#define list_add_tail_entry(pos, new, head, member) \
    do{\
        list_for_end_entry(pos, head, member);\
        pos->member.next = new;\
        new->member.next = head;\
    }while(0)

/**
 * list_add_tail_entry_safe - add a new entry to list of given type against removal of list entry.
 * @pos: the type *
 * @n: the type *
 * @new: the type *
 * @head: list head
 * @member: the name of the list_struct within the struct.
 */
#define list_add_tail_entry_safe(pos, n, new, head, member) \
    do{\
        list_for_end_entry_safe(pos, n, head, member);\
        pos->member.next = new;\
        new->member.next = head;\
    }while(0)

/**
 * list_del_entry - delet a entry from list of given type.
 * @pos: the type *
 * @pre: the type *
 * @item: the type *
 * @head: list head
 * @member: the name of the list_struct within the struct.
 */
#define list_del_entry(pos, pre, item, head, member) \
    do{\
        list_for_item_entry(pos, head, item, pre, member);\
        pre->member.next = pos->member.next;\
        pos->member.next = NULL;\
    }while(0)

/**
 * list_del_entry_safe - delet a entry from list of given type against removal of list entry.
 * @pos: the type *
 * @pre: the type *
 * @n: the type *
 * @item: the type *
 * @head: list head
 * @member: the name of the list_struct within the struct.
 */
#define list_del_entry_safe(pos, pre, n, item, head, member) \
    do{\
        list_for_item_entry_safe(pos, n, head, item, pre, member);\
        pre->member.next = pos->member.next;\
        pos->member.next = NULL;\
    }while(0)

   
static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
}

static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}

/**
 * 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)
{
    new->next = head->next;
    head->next = new;
}

/**
 * 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)
{
    struct list_head *pos = NULL;
    list_for_end(pos, head);
    pos->next = new;
    new->next = head;
}

/**
 * list_add_tail - add a new entry against removal of list 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_safe(struct list_head *new, struct list_head *head)
{
    struct list_head *pos = NULL, *n = NULL;
    list_for_end_safe(pos, n, head);
    pos->next = new;
    new->next = head;
}

/**
 * list_del - deletes entry from list.
 * @item: the element to delete from the list.
 * @head: list head
 * 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 *item, struct list_head *head)
{
    struct list_head *pos = NULL,*pre = NULL;
    list_for_item(pos, head, item, pre);
    pre->next = pos->next;
    pos->next = NULL;
}

/**
 * list_del_safe - deletes entry from list against removal of list entry.
 * @item: the element to delete from the list.
 * @head: list head
 * Note: list_empty() on entry does not return true after this, the entry is
 * in an undefined state.
 */
static inline void list_del_safe(struct list_head *item, struct list_head *head)
{
    struct list_head *pos = NULL,*pre = NULL,*n = NULL;
    list_for_item_safe(pos, n, head, item, pre);
    pre->next = pos->next;
    pos->next = NULL;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值