From: arlie@worldash.org (Arlie Stephens)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Design Patterns in Linux Kernel: Fancy Tricks With Linked Lists
Date: Tue, 19 Mar 2013 09:52:09 -0700 [thread overview]
Message-ID: <20130319165209.GB30406@worldash.org> (raw)
In-Reply-To: <CABi1daFhcsbb8oNmFhQj6fVjKnaET2p6GCs0c72ZE3AmwEagUA@mail.gmail.com>
Hi Dave,
Thank you very much.
On Mar 19 2013, Dave Hylands wrote:
>
> Hi Arlie,
>
[SNIP]
> So the real secret is that it's a circular list with a dummy node. This
> dummy node just has head/tail pointers and nothing else.
It's good to have confirmation of this.
>
> So when you're advancing through the list, instead of testing list->next
> for NULL you check to see if list->next is equal to the list head.
>
> So if you want your cursor object to be self-contained, then it should
> include both a list head and a list current.
>
> If you want your cursor to be generic, then it should probably look
> something like:
>
> struct list_cursor {
> struct list_head *list;
> struct list_head *curr;
> };
>
> I think you'll find that the cursor operations make a lot more sense if you
> keep the cursor generic and try not to include the type information about
> the list node.
And this feels right, in a way that what I was doing did not.
>
> To initialize the cursor:
>
> cursor->list = somelist
> cursor->curr = somelist
>
> To advance to the first node (remember the list has a dummy head node)
>
> cursor->curr = cursor->curr->next
>
> If the result is == cursor->head then there aren't any nodes except for the
> dummy node (i.e. list is empty)
>
> To get at the actual entry, use list_entry as per usual.
>
> I see RPDay has posted the link to his little tutorial, and I was going to
> do the same, but I didn't have it saved anywhere. I highly recommend
> reading through that.
Good introduction. And a great link at the end.
Interestingly, part of the debate yesterday probably resulted from one
engineer having Love's 2nd edition, and me having his 3rd
edition. Apparently RPDay pointed out some problems to Love which
resulted in him changing his linked list discussion in his 3rd
edition ;-)
--
Arlie
(Arlie Stephens arlie at worldash.org)
next prev parent reply other threads:[~2013-03-19 16:52 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-03-19 15:34 Design Patterns in Linux Kernel: Fancy Tricks With Linked Lists Arlie Stephens
2013-03-19 15:39 ` Robert P. J. Day
2013-03-19 15:53 ` Dave Hylands
2013-03-19 16:26 ` Anuz Pratap Singh Tomar
2013-03-19 16:52 ` Arlie Stephens [this message]
2013-03-19 17:44 ` Robert P. J. Day
2013-03-19 17:54 ` Robert P. J. Day
2013-03-19 18:45 ` Arlie Stephens
2013-03-19 19:50 ` Robert P. J. Day
2013-03-20 12:37 ` Greg Freemyer
2013-03-20 13:30 ` Robert P. J. Day
[not found] ` <CAKop=Li=fz8aZ5O2VXewr5UAByy3x551EJEPoo7=7ys9DWmSmw@mail.gmail.com>
2013-03-19 18:56 ` Victor Buciuc
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130319165209.GB30406@worldash.org \
--to=arlie@worldash.org \
--cc=kernelnewbies@lists.kernelnewbies.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).