内核中的链表list_head
2013-11-27 11:06
239 查看
在linux内核中,链表的实现是通过把链表结点嵌入到数据结构中。
如:
struct cx18_buffer {
struct list_head list;
dma_addr_t dma_handle;
char *buf;
u32 bytesused;
u32 readpos;
};
链表结构体的定义在include\linux\types.h中:
struct list_head {
struct list_head *next, *prev;
};
操作在include\linux\list.h中实现。
1、链表的初始化
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name) /*定义一个list_head的变量,并分别对next和prev赋值,初始化为空链表*/
static inline void INIT_LIST_HEAD(struct list_head *list) /*初始化list为空链表,它是一个内联函数*/
{
list->next = list;
list->prev = list;
}
2、链表的遍历
/***
* pos是list_head的一个临时指针变量,用来指向当前项,head是头结点,访问的开始位置,
* prefetch用来预取下一条要访问的数据,提高访问速度。
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next)
/***
* pos仍然是临时变量,只不过它是你用的数据结构的结构体指针了。
* head是list_head类型的指针,你要访问的开始位置。
* member是pos中list_head结构的变量名。
* list_for_each_entry相对于list_for_each来说,每次能访问到的是数据结构指针,而不是链表指针了,
* 因为里面包含了list_entry这个宏,list_entry的定义如下:
* #define list_entry(ptr, type, member) container_of(ptr, type, member)
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
prefetch(pos->member.next), &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
如:
struct cx18_buffer {
struct list_head list;
dma_addr_t dma_handle;
char *buf;
u32 bytesused;
u32 readpos;
};
链表结构体的定义在include\linux\types.h中:
struct list_head {
struct list_head *next, *prev;
};
操作在include\linux\list.h中实现。
1、链表的初始化
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name) /*定义一个list_head的变量,并分别对next和prev赋值,初始化为空链表*/
static inline void INIT_LIST_HEAD(struct list_head *list) /*初始化list为空链表,它是一个内联函数*/
{
list->next = list;
list->prev = list;
}
2、链表的遍历
/***
* pos是list_head的一个临时指针变量,用来指向当前项,head是头结点,访问的开始位置,
* prefetch用来预取下一条要访问的数据,提高访问速度。
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next)
/***
* pos仍然是临时变量,只不过它是你用的数据结构的结构体指针了。
* head是list_head类型的指针,你要访问的开始位置。
* member是pos中list_head结构的变量名。
* list_for_each_entry相对于list_for_each来说,每次能访问到的是数据结构指针,而不是链表指针了,
* 因为里面包含了list_entry这个宏,list_entry的定义如下:
* #define list_entry(ptr, type, member) container_of(ptr, type, member)
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
prefetch(pos->member.next), &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
相关文章推荐
- 内核中常用的双向链表数据结构(list_head)详解
- linux 内核分析之list_head 双向链表结构
- linux 内核分析之链表解析(list_head)
- list_head内核链表的使用
- (Linux 内核)双向循环链表list_head
- 通用链表(内核双向循环链表list_head)
- 20150203 【 内核链表 kernel_list.h 】 list_head 使用
- 内核双向链表list.h中的list_entry
- list_head 内核
- 深入分析 Linux 内核链表 list_entry...
- 之二:内核中list_head的理解
- Linux内核 hlist_head/hlist_node结构解析
- linux内核源码“双向链表list_head”
- list.h内核链表是如何定义和操作的呢
- linux内核源码“双向链表list_head”续
- Linux 内核list_head 学习(一)
- Linux 内核list_head 学习(一)
- list_head 双向循环链表的结构
- 内核链表list.h文件剖析
- struct list_head 内核链表范例 增删改查