[Principle Analysis]linux The chain list principle practice in the kernel

Source: Internet
Author: User

Summary:

Based on the list definition of the Linux kernel, this paper attempts to complete the related interface design, and constructs the test case to realize the usage of the list interface.

Body:

Let's start by looking at the list interfaces provided by the Linux kernel as follows:

struct head_node{struct head_node* pre;struct head_node* next;};
That is, there are only two pointers in the node: one pointing to the previous element and one pointing to the latter element;Let's say we're going to use this interface now, together with an int value to form a data node, there are two possible ways:

CASE1:

typedef struct DATA_NODE{HEAD_NODE h;int value;} Data_node;
CASE2:

typedef struct DATA{HEAD_NODE* head;//should I use pointers here? int value;} Data_node;
The difference between the two structural designs is mainly: Should the Head_node be in the form of pointers? Because my system capacity is limited, can not start from the height of the review, so only empirical way to see.

The following main tasks are: 1. Create multiple Data_node data nodes, 2. Thread the nodes through the Head_node interface, 3. Traverse these data_node through the Head_node interface, and output the value I first design the correlation function with the structure of CASE1,

void Node_add (head_node* head, head_node* a) {Head->next->prev = A;a->next = Head->next;head->next = a;a-& Gt;prev = head;} void Node_del (head_node* a) {A->prev->next = A->next;a->next->prev = A->prev;}
void List_value (data_node* d) {   data_node* dn = D;   do{     printf ("%d", dn->value);     DN = (data_node*) dn->h.next;   } while (Dn!=d);}
The above is the main interface design, involves mainly the operation of pointers, the following is the code of the caller:

int _tmain (int argc, _tchar* argv[]) {    data_node* D1 = (data_node*) malloc (sizeof (Data_node));    D1->h.next = & (d1->h);    D1->h.prev = & (d1->h);    D1->value = 2;    data_node* D2 = (data_node*) malloc (sizeof (Data_node));    D2->h.next = & (d2->h);    D2->h.prev = & (d2->h);    D2->value = 3;    data_node* d3 = (data_node*) malloc (sizeof (Data_node));    D3->h.next = & (d3->h);    H.prev = & (d3->h);    D3->value = 4;    Node_add (& (D1->h), & (D2->h));    Node_add (& (D1->h), & (D3->h));    List_value (d1);    Node_del (& (D2->h));    List_value (d1);    GetChar ();    return 0;}
The basic function of the design to complete the predetermined. So let's take a look at the Case2-based design, where the Node_add (), Node_del () interface is the same as CASE1, but the List_value () interface cannot be implemented here because the head_node-> When the next interface reaches the next head_node, its value data is also in the previous layer structure and cannot be found by the address offset, as shown in the following example:


In the upper part of the CASE2 design, wherein the "irreversible" indicates that it is difficult to return from the Head_node to Data_node, so as to obtain value, and the lower part is the CASE1 design, value can be directly obtained by the address offset. As a result, we can understand why we do not use the pointer form of a linked list node during the design of the data node.

Conclusion:

This article starts with the link list interface in the Linux kernel and tries to build and find out the correct usage of the linked list interface. Finally quoted the next famous saying: "I hear and I forget." I see and I remember. I do and I understand. "


[Principle Analysis]linux The chain list principle practice in the kernel

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.