1#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
4#include <linux/stddef.h>
5#include <linux/poison.h>
6#include <linux/prefetch.h>
7#include <asm/system.h>
链表的初始化
19struct list_head {
20 struct list_head *next, *prev;
21};
22
23#define LIST_HEAD_INIT(name) { &(name), &(name) }
24
25#define LIST_HEAD(name) /
26 struct list_head name = LIST_HEAD_INIT(name)
27
28static inline void INIT_LIST_HEAD(struct list_head *list)
29{
30 list->next = list;
31 list->prev = list;
32}
19-21行定义了一个list_head结构,只有两个指向list_head结构的指针,一个next,一个prev,作用显而易见。
23行的宏LIST_HEAD_INIT(name)与25行的宏LIST_HEAD(name)组合进行链表的初始化,即next和prev都指向自身。
25行的静态内联函数INIT_LIST_HEAD(struct list_head *list)同样是用来初始化链表,效果同上述一点。GNU下的C语言对C进行了扩充,不再是ANSI C,它里面增添了很多C++的特性,所以对内核进行编译只能选用相应的GCC。
INIT_LIST_HEAD在有的文献中是以宏的形式出现:
#define INIT_LIST_HEAD(ptr) do { /
(ptr)->next = (ptr); (ptr)->prev = (ptr); /
} while (0)
链表的插入
34/*
35 * Insert a new entry between two known consecutive entries.
36 *
37 * This is only for internal list manipulation where we know
38 * the prev/next entries already!
39 */
40#ifndef CONFIG_DEBUG_LIST
41static inline void __list_add(struct list_head *new,
42 struct list_head *prev,
43 struct list_head *next)
44{
45 next->prev = new;
46 new->next = next;
47 new->prev = prev;
48 prev->next = new;
49}
50#else
51extern void __list_add(struct list_head *new,
52 struct list_head *prev,
53 struct list_head *next);
54#endif
这段程序在两个已知的节点中间插入一个新节点。这里选择的是条件编译,如果没有对CONFIG_DEBUG_LIST进行宏定义,则定义了__list_add这个静态内联函数,便于以下两个函数使用。
56/**
57 * list_add - add a new entry
58 * @new: new entry to be added
59 * @head: list head to add it after
60 *
61 * Insert a new entry after the specified head.
62 * This is good for implementing stacks.
63 */
64static inline void list_add(struct list_head *new, struct list_head *head)
65{
66 __list_add(new, head, head->next);
67}
该函数在指定的head节点后面插入一个新节点new。
70/**
71 * list_add_tail - add a new entry
72 * @new: new entry to be added
73 * @head: list head to add it before
74 *
75 * Insert a new entry before the specified head.
76 * This is useful for implementing queues.
77 */
78static inline void list_add_tail(struct list_head *new, struct list_head *head)
79{
80 __list_add(new, head->prev, head);
81}
该函数将一个节点new插在指定的节点head之前。
本文详细介绍了Linux内核中的链表实现方式,包括链表结构的定义、初始化方法及节点的插入操作。通过具体的宏定义和内联函数,展示了如何在内核中高效地管理和操作链表。
5819

被折叠的 条评论
为什么被折叠?



