Linux Kernel linuxList

Source: Internet
Author: User
A deep understanding of Linux kernel linuxList-general Linux technology-Linux programming and kernel information. The following is a detailed description. List_entry () Macro
# Define list_entry (ptr, type, member )\
(Type *) (char *) (ptr)-(unsigned long) (& (type *) 0)-> member )))
Ptr is a pointer to the list_head type linked list. type is a structure, while member is a field in the structure type and the type is list_head. This macro returns a pointer to the type structure. This macro is heavily referenced in the kernel code. Therefore, it is very important to understand the meaning and usage of this macro.

What is the role of this macro:
Simply put, its function is to use a struct (usually a host structure, which contains a struct list, but can also be others, through which all hosts are connected) to obtain the address of this struct. With this address, you can access other members of the structure.

The following struct definition is provided:
Typedef struct xxx
{
...... (Other fields in the struct, so that the total size is size1)
Type1 member;
...... (Other fields in the struct)
} Type;


Define variables:
Type;
Type * B;
Type1 * ptr;
Run:
Ptr = & (a. member );
B = list_entry (ptr, type, member );
You can point B to a to obtain the address of.

First view & (type *) 0)-> member:
If "0" is forcibly converted to the pointer type, the pointer must point to "0" (the base address of the Data Segment ). Because the pointer is of the "type *" type, the address of a type Variable member domain with the "0" base address can be obtained. This address is equal to the number of offset bytes from the member domain to the base address of the struct.
Let's take a look at (type *) (char *) (ptr)-(unsigned long) (& (type *) 0)-> member ))):
(Char *) (ptr) makes the increment and decrement step of the pointer A byte. Note that (char *) must never be less. If there is no such forced conversion, ptr is the offset of other types (such as int type) minus (for example, 12). The final result is ptr-(12*4) Because int type occupies 4 bytes, so (char *) cannot be less
, (Unsigned long) (& (type *) 0)-> member) is equal to the number of offset bytes from the member pointed by ptr to the base address of the structure where the member is located. The address of the struct is obtained by subtracting the two. Convert to (type *) type pointer,

Example:

For example, create a list of Windows
Typedef struct
{
Struct list head;
Char * wndname;
Int x;
Int y;
} SWnd;
If you know the list, you can use the macro list_entry to obtain the struct address of each window and obtain the x and y coordinates of the window.
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.