General List of C languages

Source: Internet
Author: User

In operating system programming, often use C language, but C is very painful to use, unlike C + + has a convenient STL template library to use.

The Linux kernel has a very magical universal list structure that is easy to use and manage various types of data, and we'll look at the C data structures in the kernel today.

This article references: "Deep analysis of the Linux kernel chain list"

First of all, our goal is to build a circular doubly linked list structure, why the double-linked list, but also to cycle, of course, from the ease of consideration, the double-linked list can easily know their previous element, in the kernel to manage data more convenient.

The general structure is probably this:

Implementation mechanism

First, a LIST_NODE structure is defined to hold the linked list node information:

    /**     * @brief 链表节点结构     */    typedefstruct _list_node    {        struct _list_node   *next;        struct _list_node   *prev;    } list_node;

Then we use a structure that holds the data, for example, we want to use the list of the int type, as defined below:

    /**     * @brief 数据存放结构     */    typedefstruct _Int_List    {        list_node   node;        int         data;    } Int_List;

This is not the same as our traditional linked list implementation, our classic C-linked list is of course the data stored in the linked list:

    /**     * @brief 链表节点结构     */    typedef ElementType     int;    typedefstruct _list_node    {        struct _list_node   *next;        struct _list_node   *prev;        ElementType         data;    } list_node;

But this kind of realization inevitably causes the chain table structure to be repeatedly defined, is difficult to realize the generic type. Another way of thinking might be to implement ElementType as a void* pointer and then force the type conversion when it is used, but this inevitably leads to repeated type conversions, and its use is relatively inconvenient.

This is going to be a problem, if the data in the outer, linked list in the inner layer, then how to find data from the linked list?

Offsetof Macros and CONTAINER_OF macros

In order to realize the function of finding outer elements from inner elements, we need to use these two system macros, which look very complicated at one glance, but it is not difficult to understand.

/** * @brief 确定当前成员变量,在结构体中偏移量的宏 */#ifndef offsetof#define offsetof(type, member) ((size_t) &((type*)0)->member)#endif
/** * @brief 根据成员变量,找包含他的结构体指针 */#ifndef container_of#define container_of(ptr, type, member) ({  \    const typeof( ((type *)0)->member ) *__mptr = (ptr);     (type *)( (char *)__mptr - offsetof(type,member) );})#endif

The two macros are more complex, the first is the Offsetof macro, the macro with the No. 0 pointer to find member variables, we think, how is a normal structure, C compiler how to find a member of it?
For example:

    /**     * @brief 数据存放结构     */    typedefstruct _Int_List    {        list_node   node;        int         data;    } Int_List;

Suppose we Int_List A instantiate a struct with a statement, and then take the address of a 0x00001000
So node's starting address is not 0x00001000.
Is the start address of data a 0x00001000+data offset?

So how can I ask for the offset of data, not the following code:

    (&A)->data - (&A);

If the address of a is 0 o'clock, then it does not have to be reduced, which is the usage in our macro definition.

The CONTAINER_OF macro is also a similar idea, since the current List_node member's offset is found, then minus the offset, is the outer enclosing structure of the start address.

Well, so that we can achieve this list, but actually do not bother, there is a simple way, that is to let our list_node member structure, in our data storage structure of the first position, then its address is to surround its structure of the address , Direct type conversions can be done.

This idea is actually used to implement the inheritance mechanism of C language.

GitHub Project

To this, we have explained the general list of implementation ideas, we can try to achieve a unique universal chain list, the specific content does not repeat, I will complete the project has been posted on github above, welcome to the fork test.
"CList Project Address"

I Caishuxueqian, if there is an omission, also look correct.

General List of C languages

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.