Linux Kernel linked list (3), Linux Kernel
Introduction to common Linux kernel two-way linked list APIs
The linux link list structure is as follows:
Position of the kernel bidirectional linked list in the linux kernel:/include/linux/list. h
The process of using a two-way linked list includes creating a struct (item) containing the struct link_head structure, creating a linked list header, and adding an item (custom data structure, two-way linked list data unit), delete linked list nodes, traverse chain tables, empty judgment, and so on.
1. Create a custom Linked List Data Structure
- Struct kool_list {
- Int;
- Struct list_head list; // contains the linked list header.
- Int from;
- }; // Customize the data amount structure to be linked and contain a two-way linked list Structure
2. Create a linked list Header
- Struct kool_list mylist;
- INIT_LIST_HEAD (& mylist. list); // initialize a table header.
Or
Static LIST_HEAD (adc_host_head); // initialize a linked list header, adc_host_head
The second method creates a chain table header. The first method is different from the first method. The second method is initialized during compilation. Another point is whether the linked list header is independent or in the Custom linked list data structure. It is like the difference between the linked list structure shown in Linux kernel linked list (2) and that shown in this article.
3. add an item to the linked list
List_add (struct list_head * new, struct list_head * head );
For example:
- Struct kool_list * tmp;
- Tmp = (struct kool_list *) malloc (sizeof (struct kool_list ));
- Printf ("enter to and from :");
- Scanf ("% d", & tmp-> to, & tmp-> from); // initialize the Data Structure
- /* Add the new item 'tmp 'to the list of items in mylist */
- List_add (& (tmp-> list), & (mylist. list); // Add a new element node to the linked list. list in tmp
List_add_tail (struct list_head * new, struct list_head * head );
Add an item at the end of the linked list. The difference between list_add and list_add is that list_add adds the new item to the end of the linked list header, and list_add_tail adds the item to the front of the linked list header.
4. traversal table
List_for_each_entry (type * cursor, struct list_head * list, member)
This is not a function. It is a for loop, listing the linked list to be traversed in turn. The meaning of the three elements: type * cursor represents the pointer of the item, struct list_head * list is the head of the linked list, and member is the list_head data item contained in the item. The three data items can be used to locate each data element in the linked list. The positioning principle is the structure offset.
For example:
- List_for_each_entry (tmp, & mylist. list, list)
- Printf ("to = % d from = % d \ n", tmp-> to, tmp-> from );
This macro can be divided into two steps. The first step is the traversal chain table. pos points to the struct list_head structure of each item in the linked list in sequence, and the second step is to obtain the item where the struct list_head pointed by pos is located. Here is tmp.
List_for_each (pos, & mylist. list) {// records the linked list elements in sequence.
Tmp = list_entry (pos, struct kool_list, list); // obtain the data structure pointer containing the pos Node
5. Delete
To delete a node in a linked list, use secure traversal before deleting it.
For example:
- List_for_each_safe (pos, q, & mylist. list ){
- Tmp = list_entry (pos, struct kool_list, list );
- Printf ("freeing item to = % d from = % d \ n", tmp-> to, tmp-> from );
- List_del (pos );
6. Empty linked list
Int list_empty (struct list_head * head );
Returns a nonzero value if the given list is empty.
Reference: http://www.makelinux.net/ldd3/chp-11-sect-5
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.