Discussion on the realization of circulating chain list in Linux Kernel

Source: Internet
Author: User

Before the array in the cache, we need to make QEMU for disk image file write requests into a linked list, and finally the list of write requests inside the mirror to the image file, then we need a robust, reliable link list interface, so we modeled the Linux 2.4.0 kernel, To build the wheels of such a list. Take the time to record today.

Linked list, estimated to have learned the data structure of the course of the people are very impressed, because teachers like to put it in the comparative front of the place, many people think that the linked list is a very easy data structure. Because the logic of the list is very simple, each node is divided into pointers and data, a tail by the pointer of each node string up, there is no tree (binary tree, B-tree, red-black tree, Motoki, etc.) of the data structure as complex precursor and successor structure, as well as complex structure stretching change operation. And the complexity of the various operations of the list is also very easy to estimate.

However, to achieve a very reliable, universal, robust linked list data structure is very difficult.

In general, we will define the following for a node of a circular list:

struct Node {     int Val;     struct node *prev;     struct node *next; };

The code is simple, and after a linked list is constructed, it has the following effects:

This is just the implementation of the ordinary list, when we need to create another data structure of the linked list, if you still follow the above method to create a linked list, then we have to redefine a list of nodes

To store this data structure, if there are 10,000 kinds of data structures in an application, do we have to define the data structures of 10,000 kinds of linked lists, and their query, delete, and modify application interfaces?

We can try to separate the list out and decouple it so that we can use it as a common application interface.

First, define the access portals for the linked list nodes:

struct cir_list_head {    struct cir_list_head *prev, *next;} Gc_list;

Now we define a list of nodes for a specific stored data:

struct test_list{    gc_list list;     int Val;} Test_list;

Note that we are here to package the access interface of the linked list node into the list node, and carefully understand that the result we have built is as follows:

Let's take a look at how to access the data inside these nodes through the gc_list * pointer:

We want to be able to get the data Val through the list (gc_list) inside the struct test_list, that is, in the same struct, through one of the struct members to get the information of another struct member,

At this time, we need to use some artifice:

#define OFFSETOF (type, member)        (size_t) & (((type *) 0)->member)#define container_of (PTR, type, member) ({                              consttypeof(((type *)0)->member) *__mptr = (PTR);             *) ((char *) __mptr-offsetof (Type,member));})

Misappropriation of a picture to describe the above lines of code meaning, not too much text explanation ~ ~, through the above interface we can freely manipulate the circular chain of data inside the list, this packaging method

The better part is that you no longer have to deliberately rewrite a set of APIs for each of the data in the list, and note that this code is related to the compiler platform (mostly on the GCC platform) and does not have 100% portability.

RELATED links:

Wheel of a circulating chain: https://github.com/jusonalien/VM-DB/blob/master/list.h

Test usage Code: HTTPS://GITHUB.COM/JUSONALIEN/VM-DB/BLOB/MASTER/TESTLIST.C

A detailed explanation of the CONTAINER_OF macro: http://radek.io/2012/11/10/magical-container_of-macro/

Discussion on the realization of circulating chain 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.