1, the definition of the kernel linked list in include/linux/list.h
structstruct list_head *next, *prev;};
It is easy to see that the Linux kernel list is a doubly linked list.
2, the Linux link list and the ordinary list difference
We typically define a list of elements that are embedded in a linked list node, such as
structint studentid; /* */struct MyList*struct MyList *next;}
Linux, for portability and versatility, embeds linked list nodes in the element structure
intstruct list_head head; /* */
3. The operation list function provided in the Linux kernel list
(1) initialization
Static void Init_list_head (struct list_head *list) {list->next = list; /* * / list->prev = list; /* */}
(2) Adding a linked list node
List_add (struct list_head *new, struct list_head *head)
The node of new is inserted behind the head (head of any linked header)
/** * List_add-add A new entry * @new: new entry to being added * @head: List head to add it after * Insert a new entry a fter the specified head. * This is good for implementing stacks. */StaticInlinevoidList_add (structList_head *New,structList_head *head) {__list_add (New, head, Head->next);/*node inserted between head and Head->next*/and the __list_add function is as followsStaticInlinevoid__list_add (structList_head *New, structList_head *prev,structList_head *next) {Next->prev =New; New->next =Next;New->prev =prev; prev->next =New;}
List_add_tail (struct list_head *new, struct list_head *head)
New node inserted in front of head
/**/staticvoid list_add_tail (struct list_head *new struct list_head *head) { __list_add (new, head->prev, head);}
(3) Deleting a node
Method One:
/** * list_del-deletes entry from list. * @entry: The element-to-delete from the list. * Note:list_empty () on entry D OES not return True after this, the entry was * in a undefined state. */StaticInlinevoidList_del (structList_head *entry) {__list_del (Entry->prev, entry->next); Entry->next = (void*)0xDEADBEEF;/*point Pointer to 2 inaccessible locations*/entry->prev = (void*)0xBEEFDEAD;} The __list_del function where the call is as follows,StaticInlinevoid__list_del (structList_head *prev,structList_head *next) {Next->prev = prev;/* */prev->next =next;}
Note that the last two statements in the List_del function are similar to the function of free ().
A page break occurs when the user intends to access address 0xDEADBEEF or 0xBEEFDEAD.
Method Two:
For more secure deletion of nodes, use List_del_init
/**/staticvoid list_del_init (struct list_head * entry) {__ List_del (Entry->prev, entry->
(4) Extracting data from structures
It is easy to get data information in the usual way, but it is difficult to access the data using the Linux kernel list, the key is how to obtain the offset of the address and data address of the linked list node.
Note the parameters passed by List_entry! Type refers to the kind that is passed, not a variable.
List_entry (PTR, type, member) //list_head pointer, external structure data type (struct Statu), member name
Returns a pointer to data
/** * List_entry-get The struct for this entry * @ptr: the &struct list_head pointer. * @type: The type of the STR UCT this was embedded in. * @member: The name of the list_struct within the struct. */#defineList_entry (PTR, type, member) \container_of (PTR, type, member) container_of defined in the Include/linux/in Kernel.h,/** * Container_of-cast A member of a structure out to the containing structure * @ptr: The pointer to the member. * Type:the type of the container struct this is embedded in. * @member: The name of the member within the struct. * */#defineContainer_of (PTR, type, member) ({Const typeof((Type *)0)->member) * __mptr =(PTR); (Type*)((Char*) __mptr-offsetof (type, member)); })
(5) Traversal of a linked list
/* * * List_for_each - iterate over a list * @pos: The &struct list_head to use as a loop cursor. * @head : */#define List_for_each (POS, head) for (pos = (head)->next; pos! = (head) ; pos = pos->next)
Lift a chestnut:
struct list_head *entry; struct list_head dev; // Linked list Header struct list_head, card); if (Card->dev_midi = = minor) Break ;}
linux--Kernel chain List