Using the list_head structure in Linux to implement two-way linked list

Source: Internet
Author: User

The data structure of a two-way linked list is usually implemented:

 
StructList_node1 {StructList_node1 * Next ,*Prev; type1 M1; Type 2 m2 ;};StructList_node2 {StructList_node2 * Next ,*Prev; type1 M1; Type 2 m2 ;};......

Each data structure defines its specific structure for implementing the linked list and the corresponding method (Add/del) for operating the linked list;

However, for systems with a large number of different data structures, you must use the linked list. If you define a specific structure and operation method for each data structure,

UndoubtedlyCodeAs it becomes repetitive and bloated, a common two-way linked list method is required, which is applicable to various data structures.

C language does not contain templates in C ++. How can this problem be achieved?

 

The Linux kernel uses the following data structure to implement a two-way linked list:

 

 
StructList_head {StructList_head * Next ,*Prev ;};

If you need a two-way queue with a certain data structure, put a list_head data structure member in this structure.

StructKobject {Const Char*Name;StructList_head entry;StructKobject *Parent;StructKset *Kset ;}

 

The following structure is formed:

How can I access the next member through the list_head member entry in the kobject linked list structure?

The system provides the macro list_entry:

# DefineList_entry (PTR, type, member )\(Type*)((Char*) (PTR)-(unsignedLong) (& (Type *)0)->Member )))
// Access the Linked List member kobject
* OBJ =Objlist; kobject* Nextobj = (kobject *) list_entry (obj-> entry-> next,StructKobject, entry );

What is the list_entry principle:

PTR: Indicates the data member entry in the current kobject structure object. (char *) (PTR): Entry Member address --Addra;

(Unsigned long) (& (type *) 0)-> member )): Convert address 0 to type (struct kobject) object, take the address of the member (entry) member --Addrb.

(Type *) (addra-addrb): Get the first address of the kobject structure object and convert it to the kobject object.

 

Here we need to pay attention to addrb:Convert address 0 to type (struct kobject) object and take the address of the member (entry) member.

It indicates that when the first address of the struct kobject object is 0, the address of the member Member (entry) is obtained, which is offset from the first address.

The first address of the struct kobject object can be calculated based on the member (entry) address in the struct kobject object and the offset relative to the first address.

Structure:

 

Fully utilizes the features of Direct Memory Address operations in C language.

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.