[<prev] [next>] [day] [month] [year] [list]
Message-ID: <CAMP44s2=OAmLmoNN1VG7j3783ac9L7mk-4sTpNWA2gtUPHfpAA@mail.gmail.com>
Date: Tue, 2 Apr 2024 11:08:54 -0600
From: Felipe Contreras <felipe.contreras@...il.com>
To: Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
Linus Torvalds <torvalds@...ux-foundation.org>
Subject: C skill issues and good taste
Hi,
Recently it has been more apparent to me that there is a lot of desire
to learn proper C, and the chasm between beginners and experts is huge.
Linus Torvalds provided an example of what he considered "good taste" in
code for his TED interview in 2016 [1]. I wasn't the only one that found
this example interesting, since Marc Kirchner created an entire project
devoted to explaining this code [2] and why it's good.
I wasn't content with that, and I created yet another project that
ultimately explains intrusive linked lists [3] using code from Linux.
While I'm OK with the end result, I wonder if Linux kernel developers
would agree this is "good taste" code.
I understand that deleting a node in the middle of a singly linked list
is not something that Linux developers would want to do, but this is the
example Torvalds gave, and I would like to show code that does this in a
proper way.
So the following is my version of what llist_del would look like, and in
my opinion the only tricky part is what to do in the case that the entry
to remove is the first node. For that case we need to update the head's
first pointer, but if we consider it a llist_node, it's the same as the
next pointer.
struct llist_node *llist_del(struct llist_head *list, struct llist_node *entry)
{
struct llist_node *p;
llist_for_each(p, (struct llist_node *)list) {
if (p->next != entry) continue;
p->next = entry->next;
return entry;
}
return NULL;
}
Does this make sense for educational purposes? I'm not expecting this
code to be merged, as I understand deleting a node in the middle of a
single linked list doesn't offer good performance.
But does this explain the point Linus Torvalds was trying to make in the
Ted interview?
Cheers.
[1] https://www.youtube.com/watch?v=o8NPllzkFhE&t=858s
[2] https://github.com/mkirchner/linked-list-good-taste
[3] https://felipec.github.io/good-taste/parts/3.html
--
Felipe Contreras
Powered by blists - more mailing lists