Linux kernel link list:
A linked list typically consists of two fields: a data field and a pointer field.
struct list_head{
struct List_head *next,*prev;
};
A wonderful set of linked list data structures is implemented in include/linux/list.h.
The traditional linked list pointer points to the head of the next node. The Linux linked list pointer points to the next pointer list_head structure (*NEXT), bidirectional Loop. Does not change as the external data changes, making it universal.
-------------------------------------------------------------------
The Linux kernel provides a list of key operations:
1. Initialize the list header:
Init_list_head (List_head *head): Points *head,*prev to the LIST itself.
2. Inserting nodes
List_add (struct list_head *new,struct list_head *head)
List_add_tail (struct list_head *new,struct list_head *head)
3. Delete a node
List_del (struct list_head *entry: A pointer structure body)
4. Extracting Data structures
List_entry (PTR (pointer to list_head), type (type of external structure), member (member name of struct list_head))
The node pointer in the known data structure PTR, find the data structure, example
List_entry (Aup,struct,autofs,list)
5. Traverse
List_for_each (struct list_head *pos,struct list_head *head (the linked list header of the linked list you need to traverse))
Such as:
struct List_head *entry;
struct List_head cs46xx_devs;//list header
List_for_each (Entry,&cs46xx_devs) {
Card = List_entry (Entry,struct cs_card,list);
if (Card->dev_midi = = minor)
Break
}
Look at the kernel code to see how List_entry is implemented???
----------------------------------------------------------
A concrete implementation example of a kernel list:
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>//kfree Kmalloc
Module_license ("GPL");//(General public LICENSE)
struct student{
Char name[100];
int num;
struct List_head list;
}
struct student *pstudent;
struct student *tmp_student;
struct List_head student_list;
struct List_head *pos;
int Mylist_init () {
int i=0;
Init_list_head (&student_list);
Pstudent=kmalloc (sizeof (struct student) *5,gfp_kernel);//Allocate space.
memset (pstudent,0,sizeof (struct student);//initialization
for (i=0;i<5;i++) {
sprintf (Pstudent[i].name, "student%d", i+1);
pstudent[i].num=i+1;
List_add (& (Pstudent[i].list), &student_list);
}//Loop Insert Student Information
List_for_each (Pos,&student_list)
{
Tmp_student = List_entry (pos,struct student,list);
PRINTK ("<0>student%d name:%s\n", tmp_student->num,tmp_student->num,tmp_student->name);
}
return 0;
}//Traverse Student Information
void Mylist_exit ()
{
int i;
for (i=0;i<5;i++) {
List_del (& (Pstudent[i].list));
}
Kfree (pstudent);
}
Module_init (Mylist_init);
Module_exit (Mylist_exit);
Use of the Linux kernel list