I am trying to understand Linux Kernel implementation of linked list and hash table. A link to the implementation is here. I understood the linked list implementation. But i am little confused of why double pointers is being used in hlist (**pprev). Link for hlist is here. I understand that hlist is used in implementation of hash table since head of the list requires only one pointer and it saves space. Why cant it be done using single pointer (just *prev like the linked list)? Please help me.
I am trying to understand Linux Kernel implementation of linked list and hash table.
Share
The reason can be found in one of the comments:
If you had *prev instead of **pprev, and because we are trying to save memory, we don’t include *prev in the head, then our hlist implementation looks like this:
Notice that the
prevpointer cannot point to the head, orhead->first(unlike**pprev). This complicates the hlist implementation, as you’ll see when we implementhlist_add_before():Notice that
prevhas nothing to point to, in the above imeplementation ofhlist_add_head(). So, now when you implementhlist_add_before()it looks like this:Notice that now we need to pass in
headas well tohlist_add_before(), which requires an extrapushinstruction for pushingheadon the stack. Besides, there’s an extra conditional check in the implementation, which further slows down things.Now, try implementing other hlist operations, with
*previnstead of**pprev, and you’ll find out that your implementation is going to be slower than what you saw in the linux kernel.