Linux Kernel 8th

Source: Internet
Author: User

A large number of linked lists are used in the Linux kernel to organize data structures. Most of these linked lists use a set of wonderful linked list data structures implemented in [INCLUDE/Linux/list. H.

Prerequisites:

# Define offsetof (type, member) (size_t) & (type *) 0)-> Member)

Where (type *) 0 indicates to forcibly convert constant 0 to the address pointed to by type * type pointer, & (type *) 0)-> Member indicates to take (type *) the address of the Member member with the 0 pointer. Because the first address of the type structure is 0, the address of the Member member is the offset relative to the first address. The size_t here is unsigned.
Int type, which is mainly used for porting between different systems.

Therefore, the macro is used to determine the offset of a Member member from the first address of the type structure.

# Define container_of (PTR, type, member )({\
Const typeof (type *) 0)-> member) * _ mptr = (PTR );\
(Type *) (char *) _ mptr-offsetof (type, member ));})

PTR is the pointer to member, type is a structure type, member is a member of the structure, typeof usage, typeof (int *) P <=> int * P; here typeof (type *) 0)-> member) * _ mptr is equivalent to a member type pointer, but this pointer has been specially processed, is to set the first address of the type to 0, const
Typeof (type *) 0)-> member) * _ mptr = (PTR); here we actually assign values to the member pointer. (Char *) _ mptr-offsetof (type, member), first of all, why convert the address of _ mptr to the address of member * type, it facilitates byte computing. The first address of the structure is obtained after the address of the member minus its offset in the structure.

# Include <stdio. h> # define find (type, member) (size_t) & (type *) 0)-> member) # define offsetof (type, member) (size_t) & (type *) 0)-> member) # define container_of (PTR, type, member) ({\ const typeof (type *) 0)-> Member) * _ mptr = (PTR); \ (type *) (char *) _ mptr-offsetof (type, member ));}) struct list_head {struct list_head * Next, * Prev;}; struct student {int A; char B [19]; double C; struct list_head List ;}; int main (void) {struct student s = {1, {0}, 0.0, {null, null }}; struct list_head * PTR = & S. list; printf ("% 08x \ n", S); printf ("ADDR of S: % 08x \ n", & S); printf ("ADDR of S. a: % 08x \ n ", & S. a); printf ("ADDR of S. b: % 08x \ n ", & S. b); printf ("ADDR of S. c: % 08x \ n ", & S. c); printf ("ADDR of PTR: % 08x \ n", PTR); printf ("offset of list: % 08x \ n", find (struct student, list); struct student * Stu = container_of (PTR, S Truct student, list); // This sentence has a problem. Printf ("ADDR of Stu: % 08x \ n", Stu); Return 0 ;}

Result:

hubimaso@ubuntu:~/Linux$ ./container_of 00000001addr of s : bfe5d424addr of s.a : bfe5d424addr of s.b : bfe5d428addr of s.c : bfe5d43caddr of ptr : bfe5d444offset of list: 00000020addr of stu : bfe5d424

# Define list_poison1 (void *) 0x00100100)
# Define list_poison2 (void *) 0x00200200)

The first sentence is to forcibly convert the 0x00100100 hexadecimal number into a void * pointer. The address is 0x00100100.

The second sentence is the same. (It seems that these two addresses are not used) is used to assign values to unused pointers.


# Define init_list_head (PTR) do {\
(PTR)-> next = (PTR); (PTR)-> Prev = (PTR );\
} While (0)

Returns the structure pointed to by PTR to the original state. The second figure below.


# Define list_head_init (name) {& (name), & (name )}

# Define list_head (name )\
Struct list_head name = list_head_init (name)

The features are the same as above. Initialize the structure.


1. Definition of the Structure of linked list data:

Struct list_head

{

Struct list_head * Next, * Prev;

}

The list_head structure contains two pointers to the list_head structure, Prev and next. It can be seen that the linked list of the kernel has the function of double-stranded tables. In fact, it is usually organized into a two-way circular linked list. (Note that this struct does not have any data, and this definition reflects its uniqueness ).


The first figure is not to mention. The second figure shows that the pointer field of the Linux kernel linked list is no longer just a pointer to the next struct (this struct contains the data field), as shown in the first figure ), in Linux, the pointer field is a struct of the list_head type, and the struct has two pointers pointing to the struct of the previous list_head and the struct of the next list_head respectively.

Why is this defined?

The traditional type of pointer includes the structure type of the data field. If the structure changes, the pointer type also changes. If the following method is used, such pointers are of the list_head type and will not change as the struct type changes.

How can we get the data?


2. Linked List Operation (* key)

The linked list operations provided in the Linux Kernel mainly include:

1) initialize the linked list Header

Init_list_head (list_head * head)

Original function:

Static inline void init_list_head (struct list_head * List)
{
List-> next = List;
List-> Prev = List;
}


2) Insert a node

List_add (struct list_head * New, struct list_head * head)

Original function:

Static inline void list_add (struct list_head * New, struct list_head * head)
{
_ List_add (new, head, head-> next );
}

Static inline void _ list_add (struct list_head * New,
Struct list_head * Prev,
Struct list_head * Next)
{
Next-> Prev = new; // head-> next-> Prev, step 1 in the figure
New-> next = next; // Step 1 in the figure
New-> Prev = Prev; // Step 1 in the figure
Prev-> next = new; // head-> next, // Step 1 in the figure, including the section black X
}


(A solid line pointer is drawn less, that is, the prev of the next node of the head points to the head, and the prev pointer of the head points to the end node. Some areas are hard to draw and need to be understood)

List_add_tail (struct list_head * New, struct list_head * head)
Prototype:
Static inline void list_add_tail (struct list_head * New, struct list_head * head)
{
_ List_add (new, head-> Prev, head );
}

3) delete a node

List_del (struct list_head * entry)

Prototype: static inline void list_del (struct list_head * entry)
{
_ List_del (Entry-> Prev, entry-> next );
Entry-> next = list_python1;
Entry-> Prev = list_python2;
}

And:

Static inline void _ list_del (struct list_head * Prev, struct list_head * Next) // This function is classic and looks like it connects two nodes.
{
Next-> Prev = Prev;
Prev-> next = next;
}

4) Static inline void list_del_init (struct list_head * entry)
{
_ List_del (Entry-> Prev, entry-> next );
Init_list_head (entry); // The only difference above is that the deleted node is initialized.
}

5) # define list_entry (PTR, type, member )\
Container_of (PTR, type, member)

Needless to say.

6) # define list_for_each_entry (Pos, Head, member)
\
For (Pos = list_entry (head)-> next, typeof (* POS), member );
\
& Pos-> member! = (Head );
\
Pos = list_entry (POS-> member. Next, typeof (* POS), member ))

A Time Series table. POS is the structure pointer of the packaging, head is the original structure pointer, and member is the name of the original structure. The packaging structure mentioned here is equivalent to the student struct in the above example. The original structure is equivalent to list, and the member is equivalent to the name list of the original struct.

The key is & Pos-> member! = (Head); in this statement, when the values are equal, the description and traversal are complete. In this case, the member is the last node, and the next of member is the head;

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.