RT-Thread kernel study notes-in-depth understanding of the structure of the kernel object linked list

RT-Thread Kernel Study Notes-Kernel Object rt_object

RT-Thread Kernel Study Notes-Kernel Object Management

RT-Thread Kernel Study Notes-Kernel Object Operation API

RT-Thread kernel study notes-kernel object initialization linked list organization

RT-Thread kernel study notes-in-depth understanding of the structure of the kernel object linked list

RT-Thread kernel study notes-understanding of the device model rt_device

RT-Thread Kernel Study Notes-Understanding defunct zombie threads

 

background

  • To learn the kernel object, you need to understand the basic data structure.
  • Most of the more complex data structures are kernel object data structures, which are based on linked list structures.
  • Need a deep understanding of how the kernel objects are connected.

 

Linked list link method one

The next pointer of the linked list points to the first address of the next object structure.

2021-02-15_093523.png

 

Linked list link method two

The next pointer of the linked list points to the next member of the linked list of the next object structure.

2021-02-15_093610.png

 

The entry memory address of the kernel object (first address)

  • RT-Thread kernel object, using linked list link method two.
  • Therefore, the address of the object structure member list can be obtained by traversing the linked list (the address of the first next member of the list is the first address of the list itself)
  • Knowing the address of the structure member, how to find the first address of the structure itself?

2021-02-15_093626.png

  • The structure of the structure itself will not change, the size, and the size will not change. Therefore, the position of each structure member is fixed.
  • The address of the structure member, relative to the first address, the offset size is fixed.

 

RT-Thread 使用:rt_list_entry

 

/**
 * @brief get the struct for this entry
 * @param node the entry point
 * @param type the type of structure
 * @param member the name of list in structure
 */
#define rt_list_entry(node, type, member) \
    rt_container_of(node, type, member)
/**
 * rt_container_of - return the member address of ptr, if the type of ptr is the
 * struct type.
 */
#define rt_container_of(ptr, type, member) \
    ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  • If the first address of the structure is 0, the offset of the member is the address of the structure.
  • Assuming that the first address of the structure is x, the offset of the member is: x+offset.
type a; /* 临时变量,主要为了求成员的偏移 */

/* 成员m 的地址偏移 */
offset = &a.member - &a;

/* 假如已知结构体的某个成员m的地址ptr */

p = ptr - offset; /* ptr - (&a.m - &a)  */
  • Find the member's offset (offset), you can get the address of the structure itself

 

to sum up

  • Need to deeply understand the connection method and operation method of the linked list structure.
  • The RT-Thread core provides a richer linked list operation API, which can be learned and used.
  • Master the operation of the data structure to lay the foundation for the subsequent study of the kernel.

Guess you like

Origin blog.csdn.net/tcjy1000/article/details/113813922