Universal bidirectional circular linked list in Linux kernel

Source: Internet
Author: User

The development of contact with Linux more and more, rest and relax, inevitably look over the mysterious Linux kernel. See the two-way linked list, I think it is very interesting, this article noted.

As a member of many basic data structures, the implementation of the two-way circular linked list in various "textbooks" is quite standard and consistent.

That's probably the way it looks:

1 struct node_tag{2     // T data; 3     struct node_tag *prev; 4     struct Node_tag *next; 5 }node;

When you need some kind of list, plug the data members into the nodes. such as menu chain list, inside can have Kung Pao chicken, hot and sour powder, ground delicacies, boiled fish, spicy chicken wings ...

Well, when you need another kind of list, then follow the same line, as long as it's deep, it's not a problem for hundreds of decades. Some people are good at solving these kinds of problems, they are called CP Programmers (Copy-paste),

Don't ask me why I know. C + + templates can achieve common features on this point, but are not listed in this section.

With the geek spirit of Linux, the kernel certainly won't do it like above. A large number of data structures in the kernel require the use of doubly linked lists, such as processes, modules, and files.

Do you want to maintain various types of doubly linked lists? And it's still a linked list that can't be reused. I don't think many people are willing to spend their time on this kind of thing. The maintenance of a generic is not good.

Linked list node, as a "connector", the most essential thing is to link some objects, as for the object stored inside the data, you can not know.

In the Include/linux/list.h file, there is a "connector" that is used:

struct list_head {    struct list_head *next, *prev;};

Compared with Node_tag, the data section is missing.

List_head acts as an independent variable and is the role of a linked header, or a "connector" if it is a member of a struct.

In this way, in order to get some kind of linked list, we simply declare a List_head member in the host structure, and can also be arbitrarily named;

The key is that the list operation only needs to be implemented with List_head as the object, and the only question is how to get the first address of the host structure when traversing the linked list?

After all, linked lists are used for content. Here's a little trick of the compiler to figure out the address offset

#define offsetof (s,m)   (size_t) & (((S *) 0)->m)

With the List_head relative to the host structure of the first address of the offset, and its own address to a plus and minus can get the host's first address, then how to operate the operation.

Personally, I think there is an object-oriented meaning in this. Extracting common "connector" list_head, linked list operation is also designed with List_head as the object,

In addition to specific access to the operation host structure, all are common things.

Universal bidirectional circular linked list in Linux 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.