目录
背景
如果我们有阅读Linux内核源码的经验,就会发现Linux内核中有红黑树、链表等数据结构的定义和操作,而且基本是可以直接拿来用的,但是因为这些数据结构及其API定义在内核源码文件中,我们在用户态开发程序是不能直接include的,所以要想使用它们,基本的方法就是复制一份对应文件出来,再对我们的数据结构做对应的修改,本文以链表为例说明这一过程。
内核链表介绍
Linux内核链表定义为list_head结构体,这是一个双向循环链表,也就是有前驱指针和后继指针。当使用链表时,我们需要自己的结构体(假设结构体名为task)有一个list_head字段(假设字段名为task_node),作为向全局链表插入的结点,因此,全局链表的结构如下图所示(绿色箭头代表前驱指针,紫色箭头代表后继指针):
其中每一个task_node都对应一个自定义结构体task,如下图所示
至于链表的头插、尾插、删除、反射(根据task_node字段得到对应的task变量),包括链表结构体的定义,基本都在内核源码/include/linux/list.h下,我们需要将其复制出来
内核文件复制与修改
我们把文件“内核源码/include/linux/list.h”复制出来,名字依旧是list.h,并做一些修改(否则编译报错,还需要复制更多文件,所以还是自己修改比较好),得到的结果如下:
//
// Created by root on 2021-06-15.
// list.h
#ifndef TEST_LIST_H
#define TEST_LIST_H
#define LIST_POISON1 ((void *) 0x100)
#define LIST_POISON2 ((void *) 0x200)
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;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
#ifndef CONFIG_DEBUG_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;
}
#else
extern void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next);
#endif
/**
* 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)
{
__list_add(new, head, head->next);
}
/**
* 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)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
*/
#ifndef CONFIG_DEBUG_LIST
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;
}
#else
extern void __list_del_entry(struct list_head *entry);
extern void list_del(struct list_head *entry);
#endif
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
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;
}
static inline void list_replace_init(struct list_head *old,
struct list_head *new)
{
list_replace(old, new);
INIT_LIST_HEAD(old);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del_entry(entry);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del_entry(list);
list_add(list, head);
}
/**
* 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_entry(list);
list_add_tail(list, head);
}
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
/**
* list_empty_careful - tests whether a list is empty and not being modified
* @head: the list to test
*
* Description:
* tests whether a list is empty _and_ checks that no other CPU might be
* in the process of modifying either member (next or prev)
*
* NOTE: using list_empty_careful() without synchronization
* can only be safe if the only activity that can happen
* to the list entry is list_del_init(). Eg. it cannot be used
* if another CPU could re-list_add() it.
*/
static inline int list_empty_careful(const struct list_head *head)
{
struct list_head *next = head->next;
return (next == head) && (next == head->prev);
}
/**
* list_rotate_left - rotate the list to the left
* @head: the head of the list
*/
static inline void list_rotate_left(struct list_head *head)
{
struct list_head *first;
if (!list_empty(head)) {
first = head->next;
list_move_tail(first, head);
}
}
/**
* list_is_singular - tests whether a list has just one entry.
* @head: the list to test.
*/
static inline int list_is_singular(const struct list_head *head)
{
return !list_empty(head) && (head->next == head->prev);
}
static inline void __list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
struct list_head *new_first = entry->next;
list->next = head->next;
list->next->prev = list;
list->prev = entry;
entry->next = list;
head->next = new_first;
new_first->prev = head;
}
/**
* list_cut_position - cut a list into two
* @list: a new list to add all removed entries
* @head: a list with entries
* @entry: an entry within head, could be the head itself
* and if so we won't cut the list
*
* This helper moves the initial part of @head, up to and
* including @entry, from @head to @list. You should
* pass on @entry an element you know is on @head. @list
* should be an empty list or a list you do not care about
* losing its data.
*
*/
static inline void list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
if (list_empty(head))
return;
if (list_is_singular(head) &&
(head->next != entry && head != entry))
return;
if (entry == head)
INIT_LIST_HEAD(list);
else
__list_cut_position(list, head, entry);
}
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
/**
* list_splice - join two lists, this is designed for stacks
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}
/**
* list_splice_tail - join two lists, each list being a queue
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head->prev, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head, head->next);
INIT_LIST_HEAD(list);
}
}
/**
* list_splice_tail_init - join two lists and reinitialise the emptied list
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* Each of the lists is a queue.
* The list at @list is reinitialised
*/
static inline void list_splice_tail_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head->prev, head);
INIT_LIST_HEAD(list);
}
}
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member) * __mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); })
/**
* 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_head within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_last_entry - get the last element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
/**
* list_first_entry_or_null - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note that if the list is empty, it returns 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; \
})
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
/**
* list_prev_entry - get the prev element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, typeof(*(pos)), 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_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
/**
* 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_prev_safe - iterate over a list backwards 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_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
pos != (head); \
pos = n, n = pos->prev)
/**
* 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_head within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_reverse - iterate backwards 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_head within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
* @pos: the type * to use as a start point
* @head: the head of the list
* @member: the name of the list_head within the struct.
*
* Prepares a pos entry for use as a start point in list_for_each_entry_continue().
*/
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
/**
* list_for_each_entry_continue - continue iteration 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_head within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_next_entry(pos, member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_continue_reverse - iterate backwards from the given point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Start to iterate over list of given type backwards, continuing after
* the current position.
*/
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_for_each_entry_from - iterate over list of given type from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing from current position.
*/
#define list_for_each_entry_from(pos, head, member) \
for (; &pos->member != (head); \
pos = list_next_entry(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_head within the struct.
*/
#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); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_continue - continue list iteration safe against removal
* @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_head within the struct.
*
* Iterate over list of given type, continuing after current point,
* safe against removal of list entry.
*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \
for (pos = list_next_entry(pos, member), \
n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_from - iterate over list from current point safe against removal
* @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_head within the struct.
*
* Iterate over list of given type from current point, safe against
* removal of list entry.
*/
#define list_for_each_entry_safe_from(pos, n, head, member) \
for (n = list_next_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_next_entry(n, member))
/**
* list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
* @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_head within the struct.
*
* Iterate backwards over list of given type, safe against removal
* of list entry.
*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member), \
n = list_prev_entry(pos, member); \
&pos->member != (head); \
pos = n, n = list_prev_entry(n, member))
/**
* list_safe_reset_next - reset a stale list_for_each_entry_safe loop
* @pos: the loop cursor used in the list_for_each_entry_safe loop
* @n: temporary storage used in list_for_each_entry_safe
* @member: the name of the list_head within the struct.
*
* list_safe_reset_next is not safe to use in general if the list may be
* modified concurrently (eg. the lock is dropped in the loop body). An
* exception to this is if the cursor element (pos) is pinned in the list,
* and list_safe_reset_next is called after re-taking the lock and before
* completing the current iteration of the loop body.
*/
#define list_safe_reset_next(pos, n, member) \
n = list_next_entry(pos, member)
/*
* Double linked lists with a single pointer list head.
* Mostly useful for hash tables where the two pointer list head is
* too wasteful.
* You lose the ability to access the tail in O(1).
*/
#endif //TEST_LIST_H
其中用到的主要有list_head结构体、INIT_LIST_HEAD()宏函数、list_add_tail()函数、list_add()函数、list_entry()函数,以及一些遍历操作等
使用
我们就模拟一下往全局任务链表里添加任务、子任务然后遍历的场景
头文件包含
#include "list.h"
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
自定义结构体
然后定义结构体:子任务链表、任务,以及父任务链表
struct task;
struct children_list {
struct list_head list; // children list
};
struct task {
struct list_head task_node; // 父任务链表结点
struct list_head child_node; // 父任务中子任务链表的结点
char name[128]; // 任务名
struct task* parent; // 父任务指针
struct children_list children; // 子任务链表
int* dummy;
};
struct list_head tasks; // 父任务链表
task结构体中的dummy字段用来判断是否成功根据task_node或child_node字段得到task变量,因为根据list.h中的container_of()函数源码可知,这种反射是根据偏移量算出来的。那么假设一个list_head指针不属于某个task结构体,我们根据此list_head通过container_of()进行task_node字段或者child_node字段进行task结构体反射时,也会得到一个非NULL的task指针,但此task指针中的指针字段均为默认0值。所以,通过在创建任务时为dummy分配内存,就可以在字段反射时通过判断结果中的dummy字段是否非空来判断得到的task指针是否真的指向list_head指针所对应的task结构体:如果dummy为NULL,task则不指向list_head所属task结构体,反射失败;反之则task指向的正是list_head所属task结构体,反射成功。
初始化结构体
定义函数用来初始化任务:
void initializeTask(struct task* new_task, char name[128], struct task* parent) {
INIT_LIST_HEAD(&new_task->children.list); // 初始化子任务链表头结点
strcpy(new_task->name, name);
new_task->parent = parent;
new_task->dummy = (int *) malloc(sizeof (int )); // 初始化dummy字段
if (parent != NULL) { // 如果有父任务,就尾插到父任务的子任务链表中
list_add_tail(&new_task->child_node, &new_task->parent->children.list);
} else { // 没有父任务则尾插到全局父任务链表中
list_add_tail(&new_task->task_node, &tasks);
}
}
这里分别使用了INIT_LIST_HEAD()函数和list_add_tail()进行链表头结点初始化和链表新结点尾插,其中list_add_tail()的形参列表分别是新结点指针和目标链表头结点指针
构造场景并遍历
测试场景为两个父任务task1和task2,分别有子任务task1_1、task1_2和task2_1,拓扑图如下图所示
我们可以写出类似于下面的main()函数:
int main() {
INIT_LIST_HEAD(&tasks); // 初始化全局父任务链表头结点
struct task task1;
struct task task2;
struct task task1_1;
struct task task1_2;
struct task task2_1;
initializeTask(&task1, "task1", NULL); // 初始化task1
initializeTask(&task2, "task2", NULL); // 初始化task2
initializeTask(&task1_1, "task1_1", &task1); // 初始化task1_1
initializeTask(&task1_2, "task1_2", &task1); // 初始化task1_2
initializeTask(&task2_1, "task2_1", &task2); // 初始化task2_1
struct list_head* node;
struct task* task;
list_for_each(node, &tasks) {
task = list_entry(node, struct task, task_node);
printf("task name: %s.\n", task->name);
if (!list_empty(&task->children.list)) {
struct list_head* child;
struct task* child_task;
int index = 0;
list_for_each(child, &task->children.list) {
child_task = list_entry(child, struct task, child_node);
printf("Child %d: %s.\n", ++index, child_task->name);
}
}
}
printf("end.\n");
return 0;
}
下面的list_for_each()宏函数相当于一个for循环,node为当前项指针,tasks为待遍历链表(参数需要是指针,所以取址),{}内完全可以有contine、break、return等迭代控制语句。
其中的task = list_entry(node, struct task, task_node);就是字段反射,含义就是根据字段名为task_node的字段node,得到其所属类型为struct task的结构体指针首地址(当然就是struct task结构体指针变量了)。熟悉java的话,翻译成java语句就是我有一个对象node,它是某个task类对象的task_node字段,给我计算出这个task类对象。
下面的child_task = list_entry(child, struct task, child_node);也是一样的道理,只不过child对应的字段类型变成了child_node。
其实list_for_each() + list_entry()可以合并成对list_for_each_entry()的调用,如下所示
list_for_each_entry(task, &tasks, task_node) {
printf("task name: %s.\n", task->name);
if (!list_empty(&task->children.list)) {
printf("task %s children list:\n", task->name);
struct task* child_task;
int index = 0;
list_for_each_entry(child_task, &task->children.list, child_node) {
printf("Child %d: %s.\n", ++index, child_task->name);
}
}
}
list_for_each_entry(task, &tasks, task_node)含义就是:遍历tasks链表中所有项的task_node字段,将此字段赋值给task指针变量。
CMake文件编写
我用的是CMake来编译项目,目录结构如下图所示(只关注list.h和test_list.c就好):
CMakeLists.txt文件如下所示:
project("test")
cmake_minimum_required(VERSION 3.17)
INCLUDE_DIRECTORIES(./include/)
add_executable(test_list test_list.c)
运行结果
运行的输出结果如下图所示:
字段反射测试
最后,我们测试一下对属于和不属于某个task结构体的list_head指针进行字段反射的结果,使用的源码如下:
struct task* tt1 = (struct task*) malloc(sizeof (struct task));
initializeTask(tt1, "tt1", NULL);
struct list_head* node1 = tasks.prev;
struct task* out1 = list_entry(node1, struct task, task_node);
struct list_head* node2;
INIT_LIST_HEAD(node2);
struct task* out2 = list_entry(node2, struct task, task_node);
可以看到,tt1被初始化并尾插到全局父任务链表中,因此node1就是属于tt1的;而node2是一个独立的链表头结点,不属于任何task结构体。我们对node1和node2均通过task_node字段进行task结构体反射,反射出来的task结构体指针分别为out1和out2,打断点后的截图如下所示:
非常明显,属于tt1的node1的反射结果out1中的dummy不为NULL;而独立结点node2的反射结果out2中的dummy为NULL,而node2中其他字段均为随机值,这也证实了dummy字段的作用。
但这种反射的缺点就是只能根据非指针字段去反射,而不能通过指针字段(直接编译报错,因为container_of()函数对字段的要求就是非指针)。
结语
使用内核提供好的双向循环链表及其API,可以节省我们很大的精力,让我们集中更多的精力放在业务场景上。内核中也有红黑树相关定义和API,但是涉及文件比较多,有兴趣的可以比葫芦画瓢去尝试使用一下。