Linked list of Linux kernel data structures

Source: Internet
Author: User
Tags prev

comparison with classic doubly linked list

?? Classic doubly linked list. There is a pre pointer and a next pointer, and the data is within the node of the linked list.

?? A list of kernel links. There is only one pre pointer and one next pointer in each linked list node, and the entire list node is embedded in a structure that needs to use the list.

Introduction to the kernel chain list

?? The kernel linked list node structure defines the body. Where the next pointer points to the next linked list node, and the Prev pointer points to the previous linked list node.

?? As mentioned earlier, the kernel linked list node is embedded in the data node, then there is a problem, how to access the structure of the linked list pointer?

?? A pointer to the structure of a linked list, such as a. List_entry macro, is visited in the kernel chain list. There are 3 parameters ptr, type, member, according to the note, PTR is a pointer to the list node members of the variable, type is the linked list node embedded structure, that is, the structure containing the data members, member is the type structure defined in the list node members used by the name.

?? The List_entry macro also contains 2 macros, the offsetof used in container_of and container_of, respectively, as shown in the two figure below.

?? In GNU C, a conforming statement surrounded by parentheses can generate a return value, and in Container_of, __mptr is defined to prevent side effects such as ptr++.

?? The Offsetof macro is the offset of the member member in the type struct relative to the 0 address, and finally, by subtracting the offset from the __mptr, it is possible to get a pointer to the structure of the linked list node.

Common Functions

?? Init_list_head: Initializes a node of the linked header.

?? List_add_tail: Adds a member to the end of the list.

?? List_del: Deletes an element.

?? For example, when you delete an element, next and Prev are not pointing to null, but instead point to the List_poison1 and list_poison2 two specified addresses. This is to prevent some nodes to request memory error is also null, so with two specific addresses, List_poison1 and list_poison2 are low-level addresses, in the kernel space to request memory will not occur.

?? List_empty: Checks if the linked list is empty.

?? List_for_each_entry: Iterates through the list and gets the pointer to the outer struct by List_entry.

use of the kernel chain list

?? First define the structure, the data for CH and grade,ch save the student's name, Grade Save the student's performance.

?? Then define one, two, three three students, assign their names and scores. Defines a list header.

?? Call Init_list_head to initialize, and then the one, two, and three three nodes List_head inserted into the linked list.

?? Finally, the List_for_each macro is called to traverse the output list, and the structure of the linked list node is obtained through List_entry in List_for_each.

#include <linux/kernel.h> #include <linux/module.h> #include <linux/list.h> #include <linux/    Slab.h>struct k_list {struct List_head test_list;    Char ch; int grade;};    static __init int List_op_init (void) {struct k_list *one, *two, *three, *entry;    struct List_head test_head;    struct List_head *ptr;    one = kmalloc (sizeof (struct k_list *), Gfp_kernel);    both = kmalloc (sizeof (struct k_list *), Gfp_kernel);    three = Kmalloc (sizeof (struct k_list *), Gfp_kernel);    One->ch = ' A ';    Two->ch = ' B ';    Three->ch = ' C ';    One->grade = 90;    Two->grade = 85;    Three->grade = 88;    Init_list_head (&test_head);    List_add (&one->test_list, &test_head);    List_add (&two->test_list, &test_head);    List_add (&three->test_list, &test_head);        List_for_each (PTR, &test_head) {entry=list_entry (ptr, struct k_list, test_list); PRINTK (kern_info "\ n Hello%c,%d \ n", Entry->ch, Entry->grade);    } printk (Kern_info "\ Deleting first entry \ n");    List_del (&one->test_list);    Kfree (void *) one);    one = NULL;        List_for_each (ptr,&test_head) {entry=list_entry (ptr, struct k_list, test_list);    PRINTK (kern_info "\ n Hello%c,%d \ n", Entry->ch, Entry->grade);    } printk (kern_info "\ n Deleting second entry \ n");    List_del (&two->test_list);    Kfree (void *);    both = NULL;        List_for_each (PTR, &test_head) {entry=list_entry (ptr, struct k_list, test_list);    PRINTK (kern_info "\ n Hello%c,%d \ n", Entry->ch, Entry->grade);    } printk (kern_info "\ n Deleting third entry \ n");    List_del (&three->test_list);        Kfree ((void *) three);        List_for_each (PTR, &test_head) {entry=list_entry (ptr, struct k_list, test_list);    PRINTK (kern_info "\ n Hello%c,%d \ n", Entry->ch, Entry->grade); } return 0;} static __exit void List_op_exit (void) {PRINTK (kern_info "K_list modulE exit successfully! ... \ n ");} Module_init (List_op_init); Module_exit (List_op_exit);

The contents of the makefile file are as follows:

obj-m += k_list.oall:    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean:    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

?? The above code is written in the Linux kernel, first make, then load the kernel module through Insmod, and then through the DMESG can view the output results, and finally unload the kernel module through Rmmod.

?? The output results are as follows:

Linked list of Linux kernel data structures

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.