Linux kernel-linked list

Source: Internet
Author: User

definition of the Linux kernel list (defines a doubly linked list, without data fields)

defined in the/linux-source-3.13.0/include/linux/types.h header file.

1 struct List_head {2         struct list_head *next, *prev; 3 };

We can use this data structure to define linked lists that contain data fields, such as:

 1  struct   My_list  2  3    void  * mydata; // void * can point to any type of data   4  struct  list_head list; //  hide the list pointer  5 }      
Declaration and initialization macros for linked lists (list.h)

defined in the/linux-source-3.13.0/include/linux/list.h.

1 #define List_head_init (name) {& (name), & (name)}

Initializes the head node of a doubly linked list named name. (Name.pre = &name, Name.next = &name)

1 #define 2         struct list_head name = List_head_init (name)

By calling List_head, you declare and initialize one of your own linked headers, generating an empty list. such as: List_head (Mylist_head)

Adding a node to a linked list

Defined in the/linux-source-3.13.0/include/linux/list.h

Specific implementation:

1 /*2 * Insert A new entry between, known consecutive entries.3  *4 * This was only for internal list manipulation where we know5 * The Prev/next entries already!6  */7 #ifndef config_debug_list8 StaticInlinevoid__list_add (structList_head *New,9                               structList_head *Prev,Ten                               structList_head *next) One { ANext->prev =New; -         New->next =Next; -         New->prev =prev; thePrev->next =New; - } - #else - extern void__list_add (structList_head *New, +                               structList_head *Prev, -                               structList_head *next); + #endif A  at /** - * 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. in * This is good for implementing stacks. -  */ to StaticInlinevoidList_add (structList_head *New,structList_head *head) //Insert new node after head node, loop list does not end, head can be any node  + { -__list_add (New, head, head->next); the } *  $ /**Panax Notoginseng * List_add_tail-add a new entry - * @new: New entry to be added the * @head: List head to add it before +  * A * Insert A new entry before the specified head. the * This was useful for implementing queues. +  */ - StaticInlinevoidList_add_tail (structList_head *New,structList_head *head) //Insert new node before head node  $ { $__list_add (New, head->prev, head); -}
traversing a linked list

Defined in the/linux-source-3.13.0/include/linux/list.h

1 /* * 2 * List_for_each        -       iterate over a list3  * @pos: The &struct list_head to use as        a lo OP cursor. 4 * @head: The       head for your list. 5  */ 6 #define 7          for (pos = (head)->next; pos! = (head); pos = pos->next)

It is actually a for loop that uses the incoming POS as the loop variable to move the POS from top to back (next direction) from the table header until it returns to head.

1 /* * 2 * List_entry-get The struct for this entry 3 * @ptr: The        &struct list_head pointer. 4 * @type: The       type of the struct this was embedded in. 5 * @member: The name of the     list_struct within the struct. 6  */ 7 #define 8         Container_of (PTR, type, member)

LIST_ENTRY macro: A member variable (menber) pointer (PRT) from the struct body (type) to find the first pointer to the struct (type).

Linux kernel-linked list

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.