Linux Kernel Driver Learning (6) ---- kernel linked list, linux ----

Source: Internet
Author: User

Linux Kernel Driver Learning (6) ---- kernel linked list, linux ----
Review the concept of a linked list Linux kernel linked list: A two-way circular linked list. Kernel Linked List Design Philosophy: To achieve code reuse as much as possible, a large number of linked lists can be designed as a single linked list.
Structure of the kernel linked list(The code is in include/linux/list. h ):

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 bidirectional cyclic linked list, in this way, it can achieve the highest efficiency.
Note: 1> the linked list does not contain data fields and can be embedded in any structure. ---> in this way, the object structure containing the list_head pointer is not considered, in this case, you do not need to rewrite the basic operation function of the linked list. 2> A struct can contain multiple list fields. ---> in this way, you can mount a linked list to multiple different linked lists. For example: the Linux kernel simultaneously mounts the process data structure (task_struct) to the task linked list and priority linked list. Example:
struct score_list{    char name;    struct list_head *next,*prev;};


How to construct a kernel linked list?Define a list_head (containing only the pointer domain structure) in the structure of a specific object, and connect the specific object through this member to form a list, then, you can use the universal linked list function to insert, delete, and perform other operations ..
Function for operating the kernel linked list:1. INIT_LIST_HEAD: Create/initialize a linked list --- Search for its prototype in Linux kernel 2. list_add: Insert a node in the linked list header. 3. list_add_tail: insert nodes at the end of the linked list; 4. list_del: delete nodes; 5. list_entry: retrieve nodes; 6. list_for_each: traversal table
Find the following functions in/linux/list. h and analyze how to implement them? 1. INIT_LIST_HEAD: Create/initialize a linked list-search for its prototype in the Linux Kernel
static inline void INIT_LIST_HEAD(struct list_head *list){ list->next = list; list->prev = list;}

Let the list_head chain table point to its own front and back. Then initialize
2. list_add: Insert a node into the head of the linked list.
/*** List_add-add a new entry // add a new node * @ new: new entry to be added // parameter new: entry of the node to be added * @ head: list head to add it after // parameter head: add the node behind the header of the list_head table. * Insert a new entry after the specified head. * This is good for implementing stacks. --- "can be used to implement stack */static inline void list_add (struct list_head * new, struct list_head * head) {_ list_add (new, head, head-> next ); // Insert a new node between head and head> next}

Tracking_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; new->next = next; new->prev = prev; prev->next = new;}


3. list_add_tail: insert nodes at the end of the linked list
/*** List_add_tail-add a new entry // add a new node * @ new: new entry to be added // newly added node * @ head: list head to add it before // add a node before head ** Insert a new entry before the specified head. * This is useful for implementing queues. // It can be used to implement the queue */static inline void list_add_tail (struct list_head * new, struct list_head * head) {_ list_add (new, head-> prev, head ); // insert nodes between head and head-> prev, that is, insert nodes at the end of the chain table}


4. list_del: delete a node
static inline void list_del(struct list_head *entry){ __list_del(entry->prev, entry->next); entry->next = LIST_POISON1; entry->prev = LIST_POISON2;}

The list_del () function sets the deleted prev \ next pointers to list_python1 and list_python2 respectively to ensure that node items not in the linked list are inaccessible, access to the above two values will cause page faults
5, List_entry:Retrieves a node and returns a pointer to an external structure containing the pointer field.
/*** List_entry-get the struct for this entry ---> Returns a pointer to the structure of the node (including the external structure of head_list) * @ ptr: the & struct list_head pointer. ---> pos * @ type: the type of the struct this is embedded in. ---> structure type of the embedded object * @ member: the name of the list_struct within the struct. ---> name of the pointer field list_head structure in the structure of the embedded object */# define list_entry (ptr, type, member) \ container_of (ptr, type, member)


6. liast_for_each: traversal Link Table --- use source insight to find its usage
<Pre name = "code" class = "html">/*** list_for_each-iterate over a list * @ pos: the & struct list_head to use as a loop cursor. // the cursor in the traversal process points to each node * @ head: the head for your list. // point to the header head */# define list_for_each (pos, head) \ for (pos = (head)-> next; pos! = (Head); pos = pos-> next)

 

It can be seen as a for loop. The cursor starts from the header node pointed by the head until the pos reaches the end of the node pointer head. Reference usage.

The main method is to refer to the Linux kernel code and learn the usage of corresponding functions to implement the entire linked list.

Refer to blog: Workshop

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.