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 understand the cyclic linked list structure in Linux kernel

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article to share with you is about how to understand the Linux kernel in the circular list structure, Xiaobian feel quite practical, so share to everyone to learn, I hope you can read this article after harvest, not much to say, follow Xiaobian to see it.

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

Linux kernel through the definition of list_head and for a group of operations on the list_head to achieve the same type of operation on different types of circular list, this approach avoids defining repeated operation functions for different data types of circular list, so that the code has been fully used, is a very effective programming method.

Definition of list_head:

19struct list_head {

20struct list_head *next, *prev;

21};

Next, let's look at a circular linked list of any data structure (see Figure 1). Each node of the linked list has a variable of type list_head added to it, and the other variables of the node are arbitrary. (Note that each pointer points not to the start of the node data, but to the start of a variable of type list_head.)

In such an implementation, the nodes are connected by variables of type list_head. How do we get a pointer to a node type in the middle from the pointer of type list_head? Let's look at an operation like this: list_entry(p,t,m), where t is the node type of the list, m is the name of a variable of type list_head within the node, and p is a pointer to the variable, which is used to get a pointer to the 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 value, is a copy of ptr */

652(type *)( (char *)__mptr - offsetof(type,member) );})/* Address minus offset (in bytes)*/

24#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) /* Computes the offset of the variable in bytes within the structure */

The above is how to understand the circular list structure in the Linux kernel, Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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