内核链表在很多嵌入式的代码中都有用到,因为这个链表很好用,并且代码的统一性会增强代码的可读性,因此这里简单记录一下内核链表的使用,首先是库文件,这里也就是从内核中获取的,下面的代码做了一点注释。
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
// include/linux/types.h
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;
}
// 往链表插入节点内部方法 prv <=> new <=> next
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 节点插入 new 节点,head <=> new <=> head->next(利于堆栈实现)
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
// 在head 节点插入 new 节点,head->prev <=> new <=> head (利于队列实现)
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;
}
// 删除entry节点,并时prev = NULL
static inline void __list_del_clearprev(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->prev = NULL;
}
// 删除entry节点
static inline void __list_del_entry(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}
// 删除entry节点, 使entry->next,prev 为null (在Linux内部为非法值,访问产生缺页中断)
static inline void list_del(struct list_head *entry)
{
__list_del_entry(entry);
entry->next = NULL;
entry->prev = NULL;
}
// new 节点 替换 old 节点
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;
}
// new 节点 替换 old 节点,并初始化 old
static inline void list_replace_init(struct list_head *old, struct list_head *new)
{
list_replace(old, new);
INIT_LIST_HEAD(old);
}
// 交换两个节点位置
static inline void list_swap(struct list_head *entry1, struct list_head *entry2)
{
struct list_head *pos = entry2->prev;
list_del(entry2);
list_replace(entry1, entry2);
if (pos == entry1)
pos = entry2;
list_add(entry1, pos);
}
// 删除entry节点,并初始化entry
static inline void list_del_init(struct list_head *entry)
{
__list_del_entry(entry);
INIT_LIST_HEAD(entry);
}
// 删除list 节点,并将其添加到head节点后
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del_entry(list);
list_add(list, head);
}
// 删除list 节点,并将其添加到head节点前
static inline void list_move_tail(struct list_head *list, struct list_head *head)
{
__list_del_entry(list);
list_add_tail(list, head);
}
// 确定MEMBER 成员在TYPE 结构体的偏移量
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER)
// 知道type 结构体的 member 成员指针 ptr, 获取该结构体的指针
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) ); })
#define list_entry(ptr, type, member) container_of(ptr, type, member)
// 从链表ptr头 获取第一个元素
#define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member)
// 从链表ptr头 获取最后一个元素
#define list_last_entry(ptr, type, member) list_entry((ptr)->prev, type, member)
// 从链表ptr头 获取第一个元素,如果链表如空返回 NULL
#define list_first_entry_or_null(ptr, type, member) ({ \
struct list_head *head__ = (ptr); \
struct list_head *pos__ = READ_ONCE(head__->next); \
pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
})
// 从结构体pos 获取下一个元素
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
// 从结构体pos 获取前一个元素
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, typeof(*(pos)), member)
// 链表节点遍历,从表头head开始,逐项向后(next方向)移动pos,直至又回到head,基于列表不变的基础上
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
// 在当前pos 节点继续遍历
#define list_for_each_continue(pos, head) \
for (pos = pos->next; pos != (head); pos = pos->next)
// 在当前pos 节点往前遍历
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
// 安全遍历一个链表,通过传入两个参数pos,n 可以在链表中删除pos
#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_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
pos != (head); \
pos = n, n = pos->prev)
#define list_entry_is_head(pos, head, member) \
(&pos->member == (head))
// 遍历链表,但是获得是结构体
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
!list_entry_is_head(pos, head, member); \
pos = list_next_entry(pos, member))
// 反向遍历链表
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member); \
!list_entry_is_head(pos, head, member); \
pos = list_prev_entry(pos, member))
// 以安全的方式遍历链表
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member), \
n = list_next_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = n, n = list_next_entry(n, member))
#endif
下面是一个简单的例子说明内核链表的使用
#include <stdio.h>
#include <stdlib.h>
#include "kernel_list.h"
struct student
{
int num;
struct list_head mylist; /*定义的时候要记上这一段*/
};
int main()
{
struct student *head = malloc(sizeof(struct student));/*创建头结点*/
INIT_LIST_HEAD(&(head->mylist));/*初始化链表*/
struct student *mynewnode = malloc(sizeof(struct student));
mynewnode->num = 2;
struct student *mynewnode1 = malloc(sizeof(struct student));
mynewnode1->num = 3;
list_add(&(mynewnode->mylist), &(head->mylist)); /*添加节点*/
list_add(&(mynewnode1->mylist), &(head->mylist));
/*拿到第二个节点*/
struct student *node = list_entry(head->mylist.next->next, struct student, mylist);
printf("第二个节点 %d\n", node->num);
struct student *pos;
list_for_each_entry(pos, &(head->mylist), mylist) /*遍历节点*/
{
printf("num %d\n", pos->num);
}
exit(0);
}
运行结果如下