Linux Kernel hlist_head/hlist_node structure Parsing

Source: Internet
Author: User

Definition in the kernel:

Struct hlist_head {
Structhlist_node * first;
};

Struct hlist_node {
Structhlist_node * Next, ** pprev;
};
This data structure differs from the general hash-List data structure definition in the following ways:

1) First, the hash header node stores only one pointer, that is, the first pointer, pointing to the list header node. No tail pointer is the pointer to the list end node, this is to save space-especially when hashbucket is large, it can save up to half the pointer space.

2) The list node has two pointers, but note that pprev is the pointer, which points to the next pointer of the previous node; the pprev of the first element points to the fist field of the linked list header. The next of the last element is null. (SEE ).

Now I have a question: why is pprev a pointer instead of a prev? In this way, even for first, it can point the prev pointer to the End Node of the list.

It is mainly based on the following considerations:
1) there are not many list elements in the hash-List (if there are too many elements, it is usually a problem with the design), even if the traversal does not require too much cost, at the same time, there are not many requirements for getting the end node.

2) If the prev points to the previous pointer for a general node, and the first element of hash points to the End Node of list, when deleting an element, you also need to determine whether the node is the first node for processing. however, the hlist_head parameter is not included in the API provided by hlist to delete nodes. Therefore, this judgment is difficult.

3) the above two points explain why Prev is not used. Now, we can explain why pprev is needed, that is, a pointer pointing to the pointer to save the next pointer of the previous node-because in this way, even if the deleted node is a first node, you can use * pprev = next; directly modify the pointer. let's look at the two APIs for deleting a node and modifying a list header node:

Static inline void hlist_add_head (struct hlist_node * n, struct hlist_head * H)
{
Struct hlist_node * First = H-> first;
N-> next = first;
If (first)
First-> pprev = & N-> next;
H-> first = N;
N-> pprev = & H-> first; // at this time, n is the first pointer of hash, so its pprev points to the address of the first pointer of hash.
}

Static inline void _ hlist_del (struct hlist_node * n)
{
Struct hlist_node * Next = N-> next;
Struct hlist_node ** pprev = N-> pprev;
* Pprev = next; // pprev points to the next pointer of the previous node, and points to itself when the node is the first node, therefore, in both cases, you can delete the nodes to be deleted, whether the node is a common node or a header node.
If (next)
Next-> pprev = pprev;
}

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.