Skip to contentSkip to conversation

About head of kernel linked list structure

DISCUSSION Huaicheng Li 5 participants 9 messages May 6, 2015 – May 11, 2015

lore
Share & tools
lore
#0 lore raw

In my understanding, the head initialised using LIST_HEAD_INIT or defined by LIST_HEAD corresponds to no *real* data field. But it *does* have its own _next_ and _prev_ pointers. The _next_ pointer points to the first real node in the doubly linked list, and the _prev_ pointer points to the last real node.

I'm wondering if the picture shown at the very bottom of http://kernelnewbies.org/FAQ/LinkedLists is wrong about this. Because I believe head's _prev_ should point to the last node, which is at the very right. And the last node's _next_ should point to the head. Just as shown like the red line.

Am I right?

--
Best Regards
Huaicheng Li

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150506/5cd631c4/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: linked_list_example copy.png
Type: image/png
Size: 37281 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150506/5cd631c4/attachment-0001.png

#1 lore raw
Quoted 2 lines · Huaicheng Li

In my understanding, the head initialised using LIST_HEAD_INIT or defined by LIST_HEAD corresponds to no *real* data field.

correct. a better way to describe it would be that it corresponds to
no real enclosing payload, so it should never be dereferenced to get to said payload.

Quoted 3 lines

But it *does* have its own _next_ and _prev_ pointers. The _next_ pointer points to the first real node in the doubly linked list, and the _prev_ pointer points to the last real node.

so far, so good -- by "real", you mean list heads that actually
correspond to an enclosing payload.

Quoted 9 lines

I'm wondering if the picture shown at the very bottom of
http://kernelnewbies.org/FAQ/LinkedLists is wrong about this. Because

I believe head's _prev_ should point to the last node, which is at the very right. And the last node's _next_ should point to the head. Just as shown like the red line.

Am I right?

it looks fine to me, although my standard is to show next pointers
going to the right, and prev to the left, but this way looks fine. maybe i'm misunderstanding your objection.

i once wrote a tutorial on LLs:

http://www.crashcourse.ca/introduction-linux-kernel-programming/intermission-lets-talk-about-linked-lists-and-containerof-free

rday

Signature · 7 lines
========================================================================
Robert P. J. Day                                 Ottawa, Ontario, CANADA
                        http://crashcourse.ca

Twitter:                                       http://twitter.com/rpjday
LinkedIn:                               http://ca.linkedin.com/in/rpjday
========================================================================
#2 lore raw

Hi Robert,

You got my point and I?m sorry for not stating it in a clear way. You are right about the "prev-to-left" and "next-to-right" when drawing the line. At that time, I just wanted to show which node they were pointing at. Anyway, you solved my puzzle. Also thanks for your excellent article.

--
Best Regards
Huaicheng Li

Quoted 47 lines · Robert P. J. Day
Quoted 2 lines · Huaicheng Li

In my understanding, the head initialised using LIST_HEAD_INIT or defined by LIST_HEAD corresponds to no *real* data field.

correct. a better way to describe it would be that it corresponds to
no real enclosing payload, so it should never be dereferenced to get to said payload.

Quoted 3 lines

But it *does* have its own _next_ and _prev_ pointers. The _next_ pointer points to the first real node in the doubly linked list, and the _prev_ pointer points to the last real node.

so far, so good -- by "real", you mean list heads that actually
correspond to an enclosing payload.

Quoted 9 lines

I'm wondering if the picture shown at the very bottom of
http://kernelnewbies.org/FAQ/LinkedLists is wrong about this. Because

I believe head's _prev_ should point to the last node, which is at the very right. And the last node's _next_ should point to the head. Just as shown like the red line.

Am I right?

it looks fine to me, although my standard is to show next pointers
going to the right, and prev to the left, but this way looks fine. maybe i'm misunderstanding your objection.

i once wrote a tutorial on LLs:

http://www.crashcourse.ca/introduction-linux-kernel-programming/intermission-lets-talk-about-linked-lists-and-containerof-free

rday

Signature · 7 lines
========================================================================
Robert P. J. Day                                 Ottawa, Ontario, CANADA
                       http://crashcourse.ca

Twitter:                                       http://twitter.com/rpjday
LinkedIn:                               http://ca.linkedin.com/in/rpjday
========================================================================
#4 lore raw
In reply to Huaicheng Li
Quoted 6 lines · Huaicheng Li

In my understanding, the head initialised using LIST_HEAD_INIT or defined by LIST_HEAD corresponds to no *real* data field.
But it *does* have its own _next_ and _prev_ pointers. The _next_ pointer points to the first real node in the doubly linked list,
and the _prev_ pointer points to the last real node.

Quoted 11 lines

I'm wondering if the picture shown at the very bottom of *http://kernelnewbies.org/FAQ/LinkedLists
<http://kernelnewbies.org/FAQ/LinkedLists>* is wrong about this. Because
I believe head's _prev_ should point to the last node, which is at the
very right. And the last node's _next_ should point to the head.
Just as shown like the red line.

Am I right?

Hi Huachieng

AFAIK, if the linked list is not circular one, the last node's next should point to NULL, so does the head prev's. This is done so you know when you hit head i.e

if !(head.prev)

{
      // we're at head
}
Quoted 16 lines

--
Best Regards
Huaicheng Li

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Signature · 17 lines
regards,

Mulyadi Santosa
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150507/b43227d9/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: linked_list_example copy.png
Type: image/png
Size: 37281 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150507/b43227d9/attachment-0001.png 
#5 lore raw
Quoted 6 lines · Huaicheng Li

In my understanding, the head initialised using LIST_HEAD_INIT or defined by LIST_HEAD corresponds to no *real* data field.
But it *does* have its own _next_ and _prev_ pointers. The _next_ pointer points to the first real node in the doubly linked list,
and the _prev_ pointer points to the last real node.

Quoted 11 lines

I'm wondering if the picture shown at the very bottom of *http://kernelnewbies.org/FAQ/LinkedLists
<http://kernelnewbies.org/FAQ/LinkedLists>* is wrong about this. Because
I believe head's _prev_ should point to the last node, which is at the
very right. And the last node's _next_ should point to the head.
Just as shown like the red line.

Am I right?

Hi Huachieng

AFAIK, if the linked list is not circular one, the last node's next should point to NULL, so does the head prev's. This is done so you know when you hit head i.e

if !(head.prev)

{
      // we're at head
}
Signature · 10 lines
regards,

Mulyadi Santosa
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150507/a7d3605c/attachment-0001.html 
#3 lore raw
In reply to Huaicheng Li
Quoted 7 lines · Huaicheng Li

Hi Robert,

You got my point and I?m sorry for not stating it in a clear way. You are right about the "prev-to-left" and "next-to-right" when drawing the line. At that time, I just wanted to show which node they were pointing at. Anyway, you solved my puzzle. Also thanks for your excellent article.

it's a moderately common mistake to not realize that the "head" of a
list is represented by just another "struct list_head" like all the other elements in the list, but it's a special list_head in the sense that it does ***not*** represent a valid enclosing data payload, and should never be dereferenced.

the way i normally demonstrate this is to show the following macro
from include/linux/list.h:

/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/

#define list_for_each(pos, head) \
        for (pos = (head)->next; pos != (head); pos = pos->next)

note well how iteration over a list starts from "(head)->next" and
continues until returning back to "(head)" while *not* including that last address to be dereferenced.

this also has the (obvious) consequence that you can never forget
which element in a list is the head; otherwise, you'll never know which element you're not supposed to dereference.

rday

Signature · 7 lines
========================================================================
Robert P. J. Day                                 Ottawa, Ontario, CANADA
                        http://crashcourse.ca

Twitter:                                       http://twitter.com/rpjday
LinkedIn:                               http://ca.linkedin.com/in/rpjday
========================================================================
#6 lore raw
In reply to Mulyadi Santosa

Hi all!

Quoted 6 lines · Mulyadi Santosa
Quoted 5 lines · Huaicheng Li

In my understanding, the head initialised using LIST_HEAD_INIT or defined by LIST_HEAD corresponds to no *real* data field.
But it *does* have its own _next_ and _prev_ pointers. The _next_ pointer points to the first real node in the doubly linked list,
and the _prev_ pointer points to the last real node.

[...]

Quoted 5 lines

AFAIK, if the linked list is not circular one, the last node's next should point to NULL, so does the head prev's. This is done so you know when you hit head i.e

if !(head.prev)

What programming language?

Bernd

Signature · 4 lines
"I dislike type abstraction if it has no real reason. And saving
on typing is not a good reason - if your typing speed is the main
issue when you're coding, you're doing something seriously wrong."
    - Linus Torvalds
#7 lore raw
In reply to Mulyadi Santosa

From: [email protected] [mailto:kernelnewbies-bounces at kernelnewbies.org] On Behalf Of Mulyadi Santosa
Sent: Thursday, May 07, 2015 3:53 AM
To: Huaicheng Li
Cc: kernelnewbies
Subject: Re: About head of kernel linked list structure

On Wed, May 6, 2015 at 5:39 PM, Huaicheng Li <lhcwhu at gmail.com<mailto:[email protected]>> wrote:
In my understanding, the head initialised using LIST_HEAD_INIT or defined by LIST_HEAD corresponds to no *real* data field. But it *does* have its own _next_ and _prev_ pointers. The _next_ pointer points to the first real node in the doubly linked list, and the _prev_ pointer points to the last real node.

I'm wondering if the picture shown at the very bottom of http://kernelnewbies.org/FAQ/LinkedLists is wrong about this. Because I believe head's _prev_ should point to the last node, which is at the very right. And the last node's _next_ should point to the head. Just as shown like the red line.

Am I right?

[cid:image002.png at 01D089A8.8896F550]

Hi Huachieng
AFAIK, if the linked list is not circular one, the last node's next should point to NULL, so does the head prev's. This is done so you know when you hit head i.e if !(head.prev)

{
      // we're at head
}

Just pointing out that these lists are circular. Back in the old days we used to call them "rings".

Quoted 1 line

From list.h:

183 /**

184  * list_empty - tests whether a list is empty
185  * @head: the list to test.
186  */

187 static inline int list_empty(const struct list_head *head) 188 {
189 return head->next == head;
190 }

Jeff Haran

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150508/1377f263/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 11417 bytes
Desc: image002.png
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150508/1377f263/attachment.png

#8 lore raw
Quoted 22 lines · Jeff Haran

Just pointing out that these lists are circular. Back in the old days we used to call them "rings".

From list.h:

183 /**

184 * list_empty - tests whether a list is empty

185 * @head: the list to test.

186 */

187 static inline int list_empty(const struct list_head *head)

188 {

Quoted 10 lines

189 return head->next == head;

190 }

Jeff Haran

After re-reading the page about linked list in kernelnewbies, I agree it's circular list. I just didn't realize that even after reading carefully the first post

Signature · 10 lines
regards,

Mulyadi Santosa
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150511/e8abfbe4/attachment.html