请自己用c语言实现,Linux内核链表,并按照Linux内核代码风格
时间: 2024-02-12 08:04:52 浏览: 37
好的,下面是我自己用 C 语言实现的 Linux 内核链表,按照 Linux 内核代码风格:
```c
#include <stdio.h>
#include <stdlib.h>
struct list_head {
struct list_head *prev, *next;
};
#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) {
new->prev = prev;
new->next = next;
prev->next = new;
next->prev = new;
}
static inline void list_add(struct list_head *new, struct list_head *head) {
__list_add(new, head, head->next);
}
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) {
prev->next = next;
next->prev = prev;
}
static inline void list_del(struct list_head *entry) {
__list_del(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
}
static inline int list_empty(struct list_head *head) {
return head->next == head;
}
#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)); })
struct my_node {
int value;
struct list_head list;
};
LIST_HEAD(my_list);
int main(void) {
struct my_node *node;
// 添加节点
node = malloc(sizeof(*node));
node->value = 1;
INIT_LIST_HEAD(&node->list);
list_add(&node->list, &my_list);
// 遍历链表
struct list_head *pos;
list_for_each(pos, &my_list) {
node = container_of(pos, struct my_node, list);
printf("node value: %d\n", node->value);
}
// 删除节点
list_for_each(pos, &my_list) {
node = container_of(pos, struct my_node, list);
list_del(&node->list);
free(node);
}
return 0;
}
```
和之前的代码相比,这份代码增加了一些宏定义和内联函数,以及`offsetof`和`container_of`两个宏。`offsetof`用于计算结构体成员相对于结构体起始地址的偏移量,`container_of`用于获取一个结构体变量的指针,给定结构体变量中一个成员的指针。
代码中的链表操作和之前的代码相同,只是增加了一些类型定义和结构体操作的宏定义。需要注意的是,Linux 内核代码中的宏和函数名通常都是以双下划线开头的,这也是代码风格的一部分。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)