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

What is the circular linked list structure in the Linux kernel

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

Share

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

What is the circular linked list structure in the Linux kernel? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.

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 {

20 struct 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.)

Figure 1

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)\

335 container_of (ptr, type, member)

650#define container_of (ptr, type, member)

651 const typeof (type *) 0)-> member) * _ mptr = (ptr);\ / * _ mptr has the same value as ptr and 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) / * calculate the offset of the variable in the structure (in bytes) * /

This is the answer to the question about what is the circular linked list structure in the Linux kernel. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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