The first statement and initialization are not described in detail here. Please refer to my previous blog post the corresponding code here:
# Ifndef _ linux_list_h # DEFINE _ linux_list_h # define offsetof1 (type, member) (size_t) & (type *) 0)-> member) # define container_of (PTR, type, member) ({\ const typeof (type *) 0)-> member) * _ mptr = (PTR); \ (type *) (char *) _ mptr-offsetof1 (type, member) ;}) static inline void prefetch (const void * X) {;} static inline void prefetchw (const void * X ){;} // # define list_poison1 (void *) 0x00100100) // # define list_policon2 (void *) 0x00200200) # define list_poison1 0 # define list_poison2 0 struct list_head {struct list_head * Next, * Prev; // bidirectional linked list}; # define list_head_init (name) {& (name ), & (name)} // initialize next and Prev with the same object. # define list_head (name) \ struct list_head name = list_head_init (name) # define init_list_head (PTR) do {\ // initialization means to point the pointer to itself (PTR) -> next = (PTR); (PTR)-> Prev = (PTR); \} while (0)
The following is a detailed analysis of the insert function. In fact, the insert function is very simple and can be understood as long as you have learned the C language. Here is a simple explanation:
/** Insert a new entry between two known consecutive entries. ** this is only for internal list manipulation where we know * The Prev/next entries already! */Static inline void _ list_add (struct list_head * new1, // Insert a new entry, insert it in the middle of Prev and next struct list_head * Prev, // struct list_head * Next) {next-> Prev = new1; new1-> next = next; new1-> Prev = Prev; Prev-> next = new1 ;} /*** list_add-Add a new entry * @ New: New entry to be added * @ head: List head to add it after ** Insert a new entry after the specified head. * This is good for implementing stacks. */static inline void list_add (struct list_head * new1, struct list_head * head) // the header insertion method. Call _ list_add () to implement {_ list_add (new1, Head, head-> next);}/*** list_add_tail-Add a new entry * @ New: New entry to be added * @ head: list head to add it before ** Insert a new entry before the specified head. * This is useful for implementing queues. */static inline void list_add_tail (struct list_head * new1, struct list_head * head) // end interpolation {_ list_add (new1, head-> Prev, head );}