Deep analysis of the Linux kernel chain list

Source: Internet
Author: User
Tags prev

1. Ordinary single-linked list

2. Kernel link List

I was stolen from other blogs, almost fooled past.

I use keynote to draw it myself (alas!!) Drawing really is to let the human heart is very tired ah!! )。

The difference is not very obvious ah?!

Read the fucking Source Code

1. Initialization

/*Include/linux/types.h*/structList_head {structList_head *next, *prev;};

/* include/linux/list.h */
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
//I. How to initialize a linked list, what is the list of the initialized lists?
3 ways to initialize a list:
//1.
#define LIST_HEAD_INIT (name) {& (name), & (name)}
//Use example: struct List_head test_list = List_head_init (test_list);

//2.
#define LIST_HEAD (name) \
struct List_head name = List_head_init (name)
//Use Example: List_head (module_bug_list);

//3.
static inline void Init_list_head (struct list_head *list)
{
List->next = list;
List->prev = list;
}
//Use example: struct List_head test_list;
Init_list_head (&test_list);
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/

Even a list of the initialization of all want so thoughtful!! What a real dick!!

After initialization, the list is the bird-like:

2. Insert

/** Insert A new entry between, known consecutive entries. * * This is only for internal list manipulation where we Know * The Prev/next entries already! */#ifndef config_debug_listStaticInlinevoid__list_add (structList_head *New,                  structList_head *Prev,structList_head *next) {Next->prev =New; New->next =Next; New->prev =prev; Prev->next =New;}#elseextern void__list_add (structList_head *New,                  structList_head *Prev,structList_head *next);#endif/** * List_add-add A new entry * @new: new entry to being added * @head: List head to add it after * * Insert a new entry After the specified head. * This is good for implementing stacks. */StaticInlinevoidList_add (structList_head *New,structList_head *head) {__list_add (New, head, head->next);}/** * List_add_tail-add A new entry * @new: new entry to being added * @head: List head to add it before * * Insert a new Entry before the specified head. * This was useful for implementing queues. */StaticInlinevoidList_add_tail (structList_head *New,structList_head *head) {__list_add (New, head->prev, head);}
/**
* The difference between List_add and List_add_tail is:
* List_add is always inserted in the first position after the chain header: For example, linked list: head-to-data 1--Data 2--data 3, after inserting new elements: head-to-new--> ; Data 1--Data 2 --Data 3
* List_add_tail always inserts new elements at the end of the list: for example, linked list: head-to-data 1--Data 2--data 3, after inserting new elements: head-to-data 1-- Data 2--Data 3 --New
*/
/**
* Careful analysis of the above functions, you can find its function abstract ingenious.
* __list_add receives three parameters: New, Prev, next. The double-linked list insertion operation anywhere, with just these 3 parameters. Then the new element must be inserted between Prev and next.
* So it's obvious: List_add is inserted between head and Head->next, which is the first element of the list.
* List_add_tail is actually inserted between Head->prev and head, that is the last element of the list.
*/

3. Delete

/** Delete A list entry by making the Prev/next entries * point to each of the other. * * This was only for internal list Manip Ulation where we know * the Prev/next entries already! */StaticInlinevoid__list_del (structList_head * prev,structList_head *next) {Next->prev =prev; Prev->next =next;}/** * list_del-deletes entry from list. * @entry: The element-to-delete from the list. * Note:list_empty () on entry D OES not return True after this, the entry was * in a undefined state. */#ifndef config_debug_listStaticInlinevoid__list_del_entry (structList_head *entry) {__list_del (Entry->prev, entry->next);}StaticInlinevoidList_del (structList_head *entry) {__list_del (Entry->prev, entry->next); Entry->next =List_poison1; Entry->prev =List_poison2;}#elseextern void__list_del_entry (structList_head *entry);extern voidList_del (structList_head *entry);#endif

/**
* There are two macros in the code above, as defined in Include/linux/poison.h:
*/
/*
* These is non-null pointers that would result in page faults
* Under normal circumstances, used to verify that nobody uses
* non-initialized List entries.
* Non-null pointers, under normal circumstances, cause page faults to be used to verify that no one is using an uninitialized linked list item.
*/
#define List_poison1 (void *) 0x00100100 + Poison_pointer_delta)
#define List_poison2 (void *) 0x00200200 + Poison_pointer_delta)

/*
* The __list_del_entry and List_del are not obvious.
*/

At this point, the kernel list has an essential understanding, so the analysis of the other list operations is very easy.

Deep analysis of the Linux kernel chain 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.