Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to fully understand the cyclic linked list of Linux kernel

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to fully understand the Linux kernel circular linked list, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

With the development of Linux, Linux is now more and more deviating from the previous theme, less and less in line with its original meaning, but it has not become the Linux kernel. The name of the Linux kernel is also Linux.

Linux kernel cyclic linked list structure

Note: the code quoted in this article is from LXR, and the kernel version analyzed is v2.6.31.

The linux kernel implements similar operations on different types of circular linked lists by defining list_head and a set of operations on list_head, which avoids defining repeated operation functions for circular linked lists of different data types, makes full use of the code, and is a very effective programming method.

Definition of list_head:

19struct list_head {20struct list_head * next, * prev; 21}

Then let's look at the circular linked list of any data structure (figure 1). Each node of the linked list adds a variable of type list_head, and the other variables of the node are arbitrary. (note: the location that each pointer points to is not the start of the node data, but the start address of the list_head type variable. )

In a linked list established in such an implementation, nodes are connected by variables of type list_head, so how do we get a pointer of a node type from a pointer of type list_head? Let's take a look at such an operation: list_entry (pforce tGram), where t is the node type of the linked list, m is the name of the variable of type list_head within the node, and p is the pointer to that variable, which is used to get a pointer to the linked list node from the list_head pointer.

334#define list_entry (ptr, type,member)\ 335container_of (ptr, type,member) 650#define container_of (ptr, type,member) ({\ 651const typeof (type *) 0)-> member) * _ mptr = (ptr);\ / * _ mptr is the same as ptr type and is a copy of ptr * / 652 (type *) ((char *) _ mptr-offsetof (type,member)) }) / * the address minus the offset (in bytes) can * / 24#define offsetof (TYPE, MEMBER) ((size_t) & (TYPE *) 0)-> MEMBER) / * calculate the offset of the variable in the structure (in bytes) * /

This is the Linux kernel circular linked list structure.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report