C语言函数大全
本篇介绍 C语言中 l 开头的 Linux 内核函数
1. list_add,list_add_tail
1.1 函数说明
函数声明 | 函数功能 |
---|---|
void list_add(struct list_head *new, struct list_head *head); |
它是 Linux 内核中双向链表操作的一个基本函数,用于将新节点添加到双向链表的头部 |
void list_add_tail(struct list_head *new, struct list_head *head); |
它是 Linux 内核中双向链表操作的一个基本函数,用于将新节点添加到链表尾部。 |
参数:
- new : 要添加的新节点的指针
- head : 链表头节点的指针。
list_add()
函数会将 new 节点插入到链表头之前,使其成为新的链表头节点。list_add_tail()
函数会根据 链表头节点找到链表尾节点,并将 new 节点添加到链表尾部。
1.2 演示示例
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
struct list_head {
struct list_head *next, *prev;
};
struct node {
int data;
struct list_head link;
};
void init_list_head(struct list_head *head)
{
head->next = head->prev = head;
}
void print_list(struct list_head *head)
{
struct node *p;
for (p = (struct node *)head->next; p != (struct node *)head; p = (struct node *)p->link.next) {
printf("%d ", p->data);
}
printf("\n");
}
int main()
{
struct list_head head = { NULL, NULL };
init_list_head(&head); // 用于初始化双向链表头部节点。
struct node *n1 = (struct node *)malloc(sizeof(struct node));
n1->data = 1;
list_add(&n1->link, &head); // 将节点添加到链表的头部
struct node *n2 = (struct node *)malloc(sizeof(struct node));
n2->data = 2;
list_add(&n2->link, &head);
struct node *n3 = (struct node *)malloc(sizeof(struct node));
n3->data = 3;
list_add_tail(&n3->link, &head); // 将节点添加到链表的尾部
printf("The original list is: ");
print_list(&head);
return 0;
}
在上述示例代码中,我们首先定义了 list_head
和 node
两个结构体,并通过调用 init_list_head()
函数初始化链表头部。然后,我们创建了三个 node
类型的节点,前两个节点分别通过 list_add()
函数将它们添加到链表的头部,最后一个节点通过 list_add_tail()
函数添加到链表的尾部 。最后,我们调用 print_list()
函数打印链表中的元素。
注意: 在使用 list_add()
和 list_add_tail()
函数之前,我们要为每个新节点分配内存空间。
2. list_cut_before,list_cut_position
2.1 函数说明
函数声明 | 函数功能 |
---|---|
void list_cut_before(struct list_head *new, struct list_head *head, struct list_head *entry); |
它是 Linux 内核中双向链表操作的一个基本函数,用于将一段节点从原始链表中移动到另一个链表中,并将其添加到新链表的头部。 |
void list_cut_position(struct list_head *new, struct list_head *head, struct list_head *entry); |
它是 Linux 内核中双向链表操作的一个基本函数,用于将一段节点从原始链表中移动到另一个链表中,并将其添加到新链表的头部。与list_cut_before不同的是,该函数需要指定要移动的节点的具体位置,而不是直接指定一个节点。 |
参数:
- new : 要添加的新链表头部;
- head : 原始链表的头部
- entry : 要移动的节点
list_cut_before()
函数会将 entry 节点及其前面的所有节点从原始链表中移动到 new 所指示的链表中,并将 entry 所在位置的前一个节点作为新链表的头节点。
list_cut_position()
函数会将 entry 节点及其后面的所有节点从原始链表中移动到 new 所指示的链表中,并将 entry 所在位置作为新链表的头节点。
2.2 演示示例
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
struct list_head {
struct list_head *next, *prev;
};
struct node {
int data;
struct list_head link;
};
void init_list_head(struct list_head *head)
{
head->next = head->prev = head;
}
void print_list(struct list_head *head)
{
struct node *p;
for (p = (struct node *)head->next; p != (struct node *)head; p = (struct node *)p->link.next) {
printf("%d ", p->data);
}
printf("\n");
}
int main()
{
struct list_head head1 = { NULL, NULL };
init_list_head(&head1);
struct node *n1 = (struct node *)malloc(sizeof(struct node));
n1->data = 1;
list_add_tail(&n1->link, &head1);
struct node *n2 = (struct node *)malloc(sizeof(struct node));
n2->data = 2;
list_add_tail(&n2->link, &head1);
struct node *n3 = (struct node *)malloc(sizeof(struct node));
n3->data = 3;
list_add_tail(&n3->link, &head1);
printf("The original list is: ");
print_list(&head1);
struct list_head head2 = { NULL, NULL };
init_list_head(&head2);
// 移动节点n1和n2到另一个链表中
list_cut_before(&head2, &head1, &n2->link);
printf("The first list after move is: ");
print_list(&head1);
printf("The second list after move is: ");
print_list(&head2);
// 再次移动节点n3到另一个链表中
list_cut_position(&head2, &head1, &n3->link);
printf("The first list after second move is: ");
print_list(&head1);
printf("The second list after second move is: ");
print_list(&head2);
return 0;
}
在上述示例代码中,我们首先定义了 list_head
和 node
两个结构体,并通过调用 init_list_head()
函数初始化两个链表的头部。然后,我们创建了三个 node
类型的节点并分别将它们添加到第一个链表的尾部。接着,我们利用list_cut_before()
函数和 list_cut_position()
函数将链表中的一段节点移动到第二个链表中。最后,我们调用 print_list
函数分别打印两个链表中的元素。
3. list_del,list_del_init,list_del_init_careful
3.1 函数说明
函数声明 | 函数功能 |
---|---|
void list_del(struct list_head *entry); |
用于从链表中删除一个节点,但不会修改该节点的指针信息。 |
void list_del_init(struct list_head *entry); |
用于从链表中删除一个节点,但会将被删除的节点的指针信息初始化为NULL。 |
void list_del_init_careful(struct list_head *entry, struct list_head *prev, struct list_head *next); |
用于从链表中删除一个节点,但需要指定该节点的前驱节点和后继节点,以确保链表结构正确。 |
参数:
- entry : 要删除的节点
- prev : 该节点的前驱节点
- next : 该节点的后继节点
3.2 演示示例
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
struct list_head {
struct list_head *next, *prev;
};
struct node {
int data;
struct list_head link;
};
void init_list_head(struct list_head *head)
{
head->next = head->prev = head;
}
void print_list(struct list_head *head)
{
struct node *p;
for (p = (struct node *)head->next; p != (struct node *)head; p = (struct node *)p->link.next) {
printf("%d ", p->data);
}
printf("\n");
}
int main()
{
struct list_head head = { NULL, NULL };
init_list_head(&head);
struct node *n1 = (struct node *)malloc(sizeof(struct node));
n1->data = 1;
list_add_tail(&n1->link, &head);
struct node *n2 = (struct node *)malloc(sizeof(struct node));
n2->data = 2;
list_add_tail(&n2->link, &head);
struct node *n3 = (struct node *)malloc(sizeof(struct node));
n3->data = 3;
list_add_tail(&n3->link, &head);
printf("The original list is: ");
print_list(&head);
// 删除节点n2,但不改变其指针信息
list_del(&n2->link);
printf("The list after delete n2 is: ");
print_list(&head);
// 删除节点n3,并初始化其指针信息为NULL
list_del_init(&n3->link);
printf("The list after delete and init n3 is: ");
print_list(&head);
// 删除节点n1,并指定其前驱和后继节点
list_del_init_careful(&n1->link, &head, head.next);
printf("The list after careful delete n1 is: ");
print_list(&head);
return 0;
}
在上述示例代码中,我们首先定义了 list_head
和 node
两个结构体,并通过调用 init_list_head()
函数初始化链表头部。然后,我们创建了三个 node
类型的节点并分别将它们添加到链表的尾部。接下来,我们利用 list_del()
、 list_del_init()
和 list_del_init_careful()
函数从链表中删除节点,并打印每次操作后的链表元素。
注意: 在使用这些函数之前,我们要确保被删除的节点在链表中。
4. list_empty,list_empty_careful
4.1 函数说明
函数声明 | 函数功能 |
---|---|
int list_empty(const struct list_head *head); |
用于判断链表是否为空,并返回非零值表示为空,返回0表示不为空 |
int list_empty_careful(const struct list_head *head); |
用于判断链表是否为空,但会先检查链表头部的指针是否为空,以避免对空指针进行解引用。 |
参数:
- head : 要判断的链表头部
4.2 演示示例
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
struct list_head {
struct list_head *next, *prev;
};
struct node {
int data;
struct list_head link;
};
void init_list_head(struct list_head *head)
{
head->next = head->prev = head;
}
void print_list(struct list_head *head)
{
struct node *p;
for (p = (struct node *)head->next; p != (struct node *)head; p = (struct node *)p->link.next) {
printf("%d ", p->data);
}
printf("\n");
}
int main()
{
struct list_head head = { NULL, NULL };
init_list_head(&head);
printf("Is the list empty? %d\n", list_empty(&head));
printf("Is the list empty carefully? %d\n", list_empty_careful(&head));
struct node *n1 = (struct node *)malloc(sizeof(struct node));
n1->data = 1;
list_add_tail(&n1->link, &head);
printf("The list after adding n1: ");
print_list(&head);
printf("Is the list empty? %d\n", list_empty(&head));
printf("Is the list empty carefully? %d\n", list_empty_careful(&head));
struct node *n2 = (struct node *)malloc(sizeof(struct node));
n2->data = 2;
list_add_tail(&n2->link, &head);
printf("The list after adding n2: ");
print_list(&head);
printf("Is the list empty? %d\n", list_empty(&head));
printf("Is the list empty carefully? %d\n", list_empty_careful(&head));
return 0;
}
在上述示例代码中,我们首先定义了 list_head
和 node
两个结构体,并通过调用init_list_head()函数初始化链表头部。然后,我们利用list_empty()和list_empty_careful()函数分别判断链表是否为空,并打印其返回值。接下来,我们创建了两个node类型的节点并分别将它们添加到链表的尾部。每次添加节点后,我们再次使用list_empty和list_empty_careful函数判断链表是否为空,并打印其返回值。
需要注意的是,在使用这些函数之前,我们要确保链表头部已经初始化。
5. Linux 内核中双向链表遍历相关
5.1 说明
宏定义 | 宏描述 |
---|---|
#define list_entry(ptr, type, member) ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) |
用于获取一个节点所在结构体的起始地址。 |
static inline int list_entry_is_head(const struct list_head *entry, const struct list_head *head) { return entry->prev == head; } |
用于判断给定节点是否为链表头。 |
#define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member) |
用于获取链表中第一个节点所在结构体的起始地址。 |
#define list_first_entry_or_null(ptr, type, member) ({ struct list_head *__head = (ptr); struct list_head *__pos = __head->next; __pos != __head ? list_entry(__pos, type, member) : NULL; }) |
用于获取链表中第一个节点所在结构体的起始地址,但会先检查链表是否为空,以避免对空指针进行解引用。 |
#define list_next_entry(pos, member) list_entry((pos)->member.next, typeof(*(pos)), member) |
用于获取链表中紧随给定节点之后的节点所在结构体的起始地址。 |
#define list_last_entry(ptr, type, member) list_entry((ptr)->prev, type, member) |
用于获取链表中最后一个节点所在结构体的起始地址。 |
#define list_prepare_entry(pos, ptr, member) ((pos) ? : list_entry(ptr, typeof(*pos), member)) |
用于准备一个节点的数据结构指针。如果该指针为NULL,则将其初始化为链表的头部。 |
#define list_prev_entry(pos, member) list_entry((pos)->member.prev, typeof(*(pos)), member) |
用于获取链表中紧靠给定节点之前的节点所在结构体的起始地址。 |
#define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next) |
遍历链表中的所有节点 |
#define list_for_each_continue(pos, head) for (pos = pos->next; pos != (head); pos = pos->next) |
从当前节点继续遍历链表中的剩余节点。 |
#define list_for_each_prev(pos, head) for (pos = (head)->prev; pos != (head); pos = pos->prev) |
从链表尾部开始遍历所有节点。 |
#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函数类似,但允许在遍历过程中删除或添加节点。其中,n参数表示要处理的下一个节点。 |
#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_safe函数类似,但遍历顺序是从链表尾部开始。 |
#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)) |
用于在遍历链表时,获取每个节点所在结构体的起始地址。其中,pos参数表示当前节点所在结构体的指针;head参数表示要遍历的链表头部指针;member参数表示每个节点在结构体中的成员名称。 |
#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_for_each_entry函数类似,但遍历顺序是从链表尾部开始。 |
#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)) |
用于从当前节点继续往后遍历链表,并获取每个节点所在结构体的起始地址。 |
#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_continue函数类似,但遍历顺序是从链表尾部开始。 |
#define list_for_each_entry_from(pos, head, member) for (; &pos->member != (head); pos = list_next_entry(pos, member)) |
用于从某个节点开始遍历链表,并获取每个节点所在结构体的起始地址。其中,pos参数表示当前要遍历的节点所在结构体的指针;head参数表示要遍历的链表头部指针;member参数表示每个节点在结构体中的成员名称。 |
#define list_for_each_entry_from_reverse(pos, head, member) for (; &pos->member != (head); pos = list_prev_entry(pos, member)) |
与list_for_each_entry_from函数类似,但遍历顺序是从链表尾部开始。 |
#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函数类似,但允许在遍历过程中删除或添加节点。其中,n参数表示要处理的下一个节点。 |
#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)) |
用于从当前节点继续往后遍历链表,并允许在遍历过程中删除或添加节点。 |
#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)) |
用于从某个节点开始遍历链表,并允许在遍历过程中删除或添加节点。 |
#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_for_each_entry_reverse函数类似,但允许在遍历过程中删除或添加节点。 |
参考
- [The Linux Kernel API]