Linked List of small secrets in C Language (3)

Source: Internet
Author: User

 

Before I started writing the Linux kernel two-way circular linked list, I had been wondering if I would like to use a long article to describe the Linux kernel two-way circular linked list? After careful consideration, I refused to describe the Linux kernel bidirectional circular linked list in boring words, because for programming languages, I believe that most readers should not like to face boring texts, prefer to see code, and that is what readers want to achieve after reading the text, so I decided to use the code with appropriate text descriptions to explain it here, which makes it impossible for me to explain it in one article, so I will write two articles to explain this knowledge point. It is hoped that readers will continue to read and learn how to write two-way circular linked lists in their applications without having to write the troublesome operation functions themselves, make full use of the operation functions provided in the Linux kernel.

Note: I will list the header files I used when writing code in the article. h. upload files to my space without point downloads. If you need them, you can download them yourself. Of course, you can also download them online or from your own linux system. : Http://download.csdn.net/user/bigloomy

 

After understanding the implementation of the two-way circular linked list in the Linux kernel, we have to wonder that its implementation is so clever. In order for readers to smoothly complete this Linux kernel two-way circular linked list tour with me, before that, I wrote an article titled the byte alignment of the little secrets in C language, you can go back and check the byte alignment of the little secrets in C language and continue reading the full text.

 

Because we have a large amount of data structures in the Linux kernel, we need to use a two-way circular linked list. If we use the traditional two-way circular linked list implementation method, we have to maintain their respective linked lists for these data structures and design operation functions such as insert, search, and delete for each linked list. This is because the next and prev pointers used to maintain the linked list in the conventional linked list point to the corresponding type of objects. Therefore, the linked list operation function of a data structure cannot be used to operate the linked list of other data structures. To solve this problem, a type-independent bidirectional circular linked list implementation method is adopted in the Linux kernel, its implementation eliminates the need to design operation functions such as insert, search, and delete for each linked list. The implementation method is to extract the pointers prev and next in the struct from the specific data structure to form a general two-way circular linked list data structure list_head. To construct a specific linked list of a type of objects, you only need to define a member of The list_head type in its struct, and connect these objects through the list_head type member defined in this definition, form the required bidirectional cyclic linked list, and then operate it through the universal linked list function. Obviously, we only need to write a universal linked list function to construct and operate the linked list of different objects, instead of writing a special function for each created bidirectional cyclic linked list, which greatly achieves code reuse.

 

Next we will start our Linux kernel two-way circular linked list tour. You can download a list of two-way circular linked lists of Linux kernel from the Internet. the header file of h is worth noting that there are some differences in the header files that may be downloaded due to different kernel versions, but this does not affect our explanation of it. Readers can read the complete text first and then start it later. We use the list. h header file to implement our two-way circular linked list. For ease of explanation, we will explain the code in the list. h header file in sequence.

 

Add: (Note: if the reader does not understand the following code, he can continue to read it without affecting the next study. The following sections will also explain it, this part of the code is added after I finish writing the full text, because at first I used # define list_entry (ptr, type, member) (type *) (char *) (ptr)-(unsigned long) (& (type *) 0)-> member) instead of # define list_entry (ptr, type, member) container_of (ptr, type, member ))

 

# Define container_of (ptr, type, member )({\

Const typeof (type *) 0)-> member) * _ mptr = (ptr );\

(Type *) (char *) _ mptr-offsetof (type, member ));})

Use typeof (type *) 0)-> member) to obtain the type of the member. Assign the pointer ptr pointing to the member to _ mptr, __mptr pointer type is the type of member data member. Use (char *) _ mptr to forcibly convert _ mptr to a char pointer, And then subtract offsetof (type, member) to obtain the pointer of the host struct. If you are not familiar with offsetof (type, member), refer to my previous article titled byte alignment of small secrets in C language.

 

First, let's look at the implementation of the list_head structure.

 

Struct list_head {

Struct list_head * next, * prev;

};

In the Linux kernel bidirectional cyclic linked list, we use the list_head Type above to define a variable and embed it as a member into the host structure. What is a host struct? It is the structure of the two-way circular linked list we have created. The linked list structure can be placed anywhere in the host structure, or any name can be used for the linked list structure, in this way, we can use the members in list_head and corresponding processing functions to traverse the linked list. If we want to get a pointer to the host structure, we can use list_entry to calculate it, don't worry about what the list_entry is. We will explain it below and continue to look at it.

 

After the list_head is defined in the host struct, initialization of the defined header node is required. The initialization can be implemented in either of the following ways.

 

# Define LIST_HEAD_INIT (name) {& (name), & (name )}

 

# Define LIST_HEAD (name )\

Struct list_head name = LIST_HEAD_INIT (name)

 

# Define INIT_LIST_HEAD (ptr) do {\

(Ptr)-> next = (ptr); (ptr)-> prev = (ptr );\

} While (0)

The code above shows that after we define a header node using list_head in the code, we need to initialize the defined header node. We can use the INIT_LIST_HEAD (ptr) macro to initialize it, alternatively, you can use the LIST_HEAD (name) macro to complete the definition and initialization without having to define it yourself. After the initialization of the header node is complete, the next step is to add the node.

 

Static inline void _ list_add (struct list_head * new,

Struct list_head * prev,

Struct list_head * next)

{

Next-> prev = new;

New-> next = next;

New-> prev = prev;

Prev-> next = new;

}

_ List_add () is used to insert a node into two non-empty nodes. It is worth noting that new, prev, And next cannot both be null values. Of course, prev can be equal to next, indicating that a new node is inserted into the linked list containing only the first node. Now that we know the implementation of the _ list_add () function, let's take a look at the implementation of list_add () and list_add_tail.

 

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 );

}

After reading the above implementation methods, we know that they are implemented by calling the underlying _ list_add. Let's see how to add nodes by passing different parameters in the _ list_add () function. The double dashes before the _ list_add () function usually indicate that this is an underlying function for other modules to call.

 

The first parameter passed by list_add () is implemented by inserting the node pointed to by new between the nodes pointed to by the head and head-> next pointers. That is, insert a new point to the end of the head pointer. Head is not necessarily a header node. If our linked list contains only one header node, the above _ list_add (new, head, head-> next) will still be true.

 

The second list_add_tail () function is to insert the node pointed by new before the node pointed by the node pointer head. If the head points to a head node, it is equivalent to adding a new node to the end node. In this function, it is worth noting that head-> prev cannot be empty. If head is a header node, head-> prev must point to a value, which generally points to the end node, create a circular linked list.

 

Speaking of this, some readers may have been eager to look at the code, so we can use list. h In the Linux kernel to write our code at the application layer.

 

# Include <stdio. h>

# Include <stdlib. h>

# Include "list. h"

 

Typedef struct _ stu

{

Char name [20];

Int num;

Struct list_head list;

} Stu;

 

Int main ()

{

Stu * pstu;

Stu * tmp_stu;

Struct list_head stu_list;

Struct list_head * pos;

Int I = 0;

INIT_LIST_HEAD (& stu_list );

Pstu = malloc (sizeof (stu) * 5 );

For (I = 0; I <5; I ++)

{

Sprintf (pstu [I]. name, "Stu % d", I + 1 );

Pstu [I]. num = I + 1;

List_add (& (pstu [I]. list), & stu_list );

}

List_for_each (pos, & stu_list)

{

Tmp_stu = list_entry (pos, stu, list );

Printf ("student num: % d \ tstudent name: % s \ n", tmp_stu-> num, tmp_stu-> name );

}

Free (pstu );

Return 0;

}

The running result is:

 

Root @ ubuntu:/home/paixu/dlist_node #./

Student num: 5 student name: Stu5

Student num: 4 student name: Stu4

Student num: 3 student name: Stu3

Student num: 2 student name: Stu2

Student num: 1 student name: Stu1

Look at the code above. What are the basic work we have done?

 

1. A host struct stu is defined, and a list variable of the struct list_head type is defined in the host struct;

 

2. Define a header node and initialize it;

 

3. Apply for memory space for a defined host struct variable;

 

4. initialize and add the requested host struct variable to the linked list with stu_list as the header node.

 

It is worth noting that the list_for_each () and list_entry () are described in the following sections, here, the reader only needs to know that the two of them are combined to print each data in the stu of the host structure. The use of sprintf () is not explained here. It is very simple. I believe that readers can guess its functions. If you have any questions or questions about the text description at the beginning, you should understand the code implementation. The use of list_add_tail () is similar to that of list_add, you can modify the code. If you do not understand list_add () at the beginning, you can refer to the running result and the text description above for your current understanding of list_add.

 

Let's look down.

 

Static inline void _ list_del (struct list_head * prev, struct list_head * next)

{

Next-> prev = prev;

Prev-> next = next;

}

Between the nodes pointed by the prev and next pointers, the two refer to each other. In fact, prev is the first node of the node to be deleted, and next is the next node of the node to be deleted.

 

Static inline void list_del (struct list_head * entry)

{

_ List_del (entry-> prev, entry-> next );

Entry-> next = list_python1;

Entry-> prev = list_python2;

}

Delete the node indicated by the entry, and block the node pointer field pointed to by the entry. It is worth noting that LIST_POISON1 and LIST_POISON2. Their Macros in list. h are defined as follows:

 

# Define LIST_POISON1 (void *) 0x00100100)

 

# Define LIST_POISON2 (void *) 0x00200200)

 

For the descriptions of LIST_POISON1 and LIST_POISON2, the Linux kernel has the following sentence: These are non-NULL pointers that will result in page faults under normal circumstances, used to verify that nobody uses non-initialized list entries. That is to say, they are not null pointers, but access to such pointers will normally cause errors. In fact, according to our general idea, the entry-> next and entry-> prev values are NULL, so that they cannot be accessed through this node. However, a special method is used here. Note: In the linux environment, the preceding macro values do not need to be modified and will not cause errors. However, errors will occur in the vc environment. The two values are not allowed, so we need to change them to NULL.

 

Static inline void list_del_init (struct list_head * entry)

{

_ List_del (entry-> prev, entry-> next );

INIT_LIST_HEAD (entry );

}

The functions of the above functions are to delete the node pointed to by the entry, and call LIST_INIT_HEAD () to create a new empty double loop linked list as the linked list header.

 

# Include <stdio. h>

# Include <stdlib. h>

# Include "list. h"

 

Typedef struct _ stu

{

Char name [20];

Int num;

Struct list_head list;

} Stu;

 

Int main ()

{

Stu * pstu;

Stu * tmp_stu;

Struct list_head stu_list;

Struct list_head * pos;

Int I = 0;

INIT_LIST_HEAD (& stu_list );

Pstu = malloc (sizeof (stu) * 5 );

For (I = 0; I <5; I ++)

{

Sprintf (pstu [I]. name, "Stu % d", I + 1 );

Pstu [I]. num = I + 1;

List_add (& (pstu [I]. list), & stu_list );

}

 

List_del (& (pstu [3]. list ));

Printf ("use list_del () to delete pstu [3] \ n ");

List_for_each (pos, & stu_list)

{

Tmp_stu = list_entry (pos, stu, list );

Printf ("student num: % d \ tstudent name: % s \ n", tmp_stu-> num, tmp_stu-> name );

}

List_del_init (& (pstu [2]. list ));

Printf ("use list_del_init () to delete pstu [2] \ n ");

List_for_each (pos, & stu_list)

{

Tmp_stu = list_entry (pos, stu, list );

Printf ("student num: % d \ tstudent name: % s \ n", tmp_stu-> num, tmp_stu-> name );

}

Free (pstu );

Return 0;

}

 

The running result is:

 

Root @ ubuntu:/home/paixu/dlist_node #./

Use list_del () to delete pstu [3]

Student num: 5 student name: Stu5

Student num: 3 student name: Stu3

Student num: 2 student name: Stu2

Student num: 1 student name: Stu1

Use list_del_init () to delete pstu [2]

Student num: 5 student name: Stu5

Student num: 2 student name: Stu2

Student num: 1 student name: Stu1

 

After reading the code, it is much easier to understand the previous explanation. Readers can analyze the code based on the corresponding text description to deepen their impressions. Next, let's take a look at the last two functions of this blog.

 

Static inline void list_move (struct list_head * list, struct list_head * head)

{

_ List_del (list-> prev, list-> next );

List_add (list, head );

}

 

Static inline void list_move_tail (struct list_head * list,

Struct list_head * head)

{

_ List_del (list-> prev, list-> next );

List_add_tail (list, head );

}

 

Let's take a look at the above two functions list_move () and list_move_tail (). The first list_move () function is to move the list between the nodes pointed by the head and head-> next pointers. The function of the second list_move_tail () function is to move the list to the node pointed to by the head and head-> prev pointers. If the reader thinks this is not very specific, let's take a look at the following code.

 

# Include <stdio. h>

# Include <stdlib. h>

# Include "list. h"

 

Typedef struct _ stu

{

Char name [20];

Int num;

Struct list_head list;

} Stu;

 

Int main ()

{

Stu * pstu;

Stu * tmp_stu;

Struct list_head stu_list;

Struct list_head * pos;

Int I = 0;

INIT_LIST_HEAD (& stu_list );

Pstu = malloc (sizeof (stu) * 5 );

For (I = 0; I <5; I ++)

{

Sprintf (pstu [I]. name, "Stu % d", I + 1 );

Pstu [I]. num = I + 1;

List_add (& (pstu [I]. list), & stu_list );

}

 

List_move (& (pstu [3]. list), & stu_list );

Printf ("Move pstu [3] to head and head-> next \ n ");

List_for_each (pos, & stu_list)

{

Tmp_stu = list_entry (pos, stu, list );

Printf ("student num: % d \ tstudent name: % s \ n", tmp_stu-> num, tmp_stu-> name );

}

List_move_tail (& (pstu [2]. list), & stu_list );

Printf ("Move pstu [2] to head and head-> between the nodes pointed by the prev pointer \ n ");

List_for_each (pos, & stu_list)

{

Tmp_stu = list_entry (pos, stu, list );

Printf ("student num: % d \ tstudent name: % s \ n", tmp_stu-> num, tmp_stu-> name );

}

Free (pstu );

Return 0;

}

 

The running result is:

 

Root @ ubuntu:/home/paixu/dlist_node #./

Move pstu [3] to the node pointing to head and head-> next

Student num: 4 student name: Stu4

Student num: 5 student name: Stu5

Student num: 3 student name: Stu3

Student num: 2 student name: Stu2

Student num: 1 student name: Stu1

Move pstu [2] to the node pointed to by the head and head-> prev pointers.

Student num: 4 student name: Stu4

Student num: 5 student name: Stu5

Student num: 2 student name: Stu2

Student num: 1 student name: Stu1

Student num: 3 student name: Stu3

 

Note this before, so that some readers think that the results are incorrect. The subscript in pstu [] starts from 0, so pstu [3] corresponds to stu4.

 

Here, we will continue with the rest in the next article "linked list of small secrets of C language (4. Due to my limited level, improper or incorrect content in my blog is inevitable, and I hope readers will criticize and correct me. Readers are also welcome to discuss relevant content. If you are willing to share your comments, please leave your valuable comments.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.