LWN: Comments on "Toward a better list iterator for the kernel" https://lwn.net/Articles/887097/ This is a special feed containing comments posted to the individual LWN article titled "Toward a better list iterator for the kernel". en-us Tue, 23 Sep 2025 10:52:08 +0000 Tue, 23 Sep 2025 10:52:08 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Toward a better list iterator for the kernel https://lwn.net/Articles/889320/ https://lwn.net/Articles/889320/ mcortese Am I the only one who thinks that depending on the names of the variables is extremely fragile? There are so many corner cases where this hack will fail that I wonder if developers can really be convinced to begin using it. Sat, 26 Mar 2022 07:09:49 +0000 Inventors and heroes https://lwn.net/Articles/888596/ https://lwn.net/Articles/888596/ ghane <div class="FormattedComment"> <font class="QuotedText">&gt; I wouldn&#x27;t say &quot;obvious&quot; but Isaac Newton (one of the very few who was _really_ way ahead of his time) put it best: &quot;If I have seen further it is by standing on the shoulders of Giants.&quot;</font><br> <p> The way I understand it, it was a dig at his arch-rival (in the 1670s), Hooke, who was short (and may have had a twisted back). <br> <p> <p> </div> Sun, 20 Mar 2022 10:24:17 +0000 Independent invention https://lwn.net/Articles/888555/ https://lwn.net/Articles/888555/ giraffedata <blockquote> Alexander Graham Bell is credited with the invention of the telephone, but he just happened to beat his competitor to the patent office. I think the same is true of the Wright Brothers (the first successful aircraft was built in 1896 in England - it just didn't actually fly til about 10 years later ... :-) </blockquote> <p> Being the first to fly, first to file and the first to publicize count for a lot. I think it's vanishingly rare for someone to be so smart he comes up with an idea that no one else has; turning such an idea into an invention is more worthy of credit than having the idea. <p> I've read that many things were invented in China before they were invented in Europe (in days before the two were in communication), but it's because of the European inventor that I have them today, so I tend to celebrate the European inventor. Sat, 19 Mar 2022 20:54:42 +0000 I find myself rejoicing... https://lwn.net/Articles/888364/ https://lwn.net/Articles/888364/ smitty_one_each <div class="FormattedComment"> ...that even the Really Smart Folks have to bang their heads as much as I do trying to get things working.<br> </div> Fri, 18 Mar 2022 01:43:13 +0000 Inventors and heroes https://lwn.net/Articles/888218/ https://lwn.net/Articles/888218/ excors <div class="FormattedComment"> That was Hooke, not Leibniz. (Newton seems to have been rather antagonistic and had several bitter rivalries. And as Master of the Mint he also had dozens of counterfeiters prosecuted and hanged, so those people probably weren&#x27;t too happy with him either. But (to bring this marginally back on-topic) as Linus Torvalds said when asked about his heroes, &quot;even though [Newton] was apparently not a very nice person, he was certainly one of those people who changed the world in many different ways&quot; (<a href="https://archive.computerhistory.org/resources/access/text/2012/10/102658325-05-01-acc.pdf">https://archive.computerhistory.org/resources/access/text...</a>))<br> </div> Thu, 17 Mar 2022 13:05:27 +0000 Inventors and heroes https://lwn.net/Articles/888196/ https://lwn.net/Articles/888196/ mathstuf <div class="FormattedComment"> The &quot;shoulders of giants&quot; quote was also meant as a slight to Leibnitz who was not tall.<br> </div> Thu, 17 Mar 2022 08:56:07 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887835/ https://lwn.net/Articles/887835/ jem <div class="FormattedComment"> The difference between C and Rust is that Rust allows you reuse a variable name in the same block, which is an error i C.<br> <p> The scope of a Rust variable variable ends either at the end of the block, or when a new variable with the same name is declared. You can use the old variable when computing the initial value of the new variable, i.e. you can initialize a variable with the value of the variable &quot;itself&quot;.<br> <p> This can be used to change a variable from being mutable to immutable when there is no need to change the value after some point in the program. (Technically there are two separate variables with the same name.)<br> </div> Mon, 14 Mar 2022 19:51:41 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887827/ https://lwn.net/Articles/887827/ laarmen <div class="FormattedComment"> In Rust, variable name re-use is actually rather idiomatic. I used to be skeptical about it, as my experience also was of confusing bugs, or at least code. However, I&#x27;ve embraced this idiom in Rust for purely pragmatic reasons:<br> <p> In C, your &#x27;char* str&#x27; variable will be the same type before and after your null check, but in Rust those are two separate types. A given &quot;content&quot; can undergo multiple type changes in as many lines as the assumptions are checked, it&#x27;d be really tedious to have to come up with new names for essentially the same content.<br> <p> You can find an example of shadowing in the Rust Book in the second chapter! <a href="https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html#comparing-the-guess-to-the-secret-number">https://doc.rust-lang.org/book/ch02-00-guessing-game-tuto...</a> (the &#x27;guess&#x27; name is reused as we go from the raw input string to the actual integer)<br> </div> Mon, 14 Mar 2022 16:43:38 +0000 Inventors and heroes https://lwn.net/Articles/887776/ https://lwn.net/Articles/887776/ ballombe <div class="FormattedComment"> Being French, I will argue that Fermat invented calculus before them!<br> Really, ideas only exist when they are shared, and for that you need to be at least two.<br> <p> While Hilbert gave his theorems numbers (90, 92 are the most well known)<br> Poincarré gave them the names of people whose work inspired him.<br> <p> <p> </div> Mon, 14 Mar 2022 15:53:19 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887760/ https://lwn.net/Articles/887760/ PengZheng <div class="FormattedComment"> Unfortunately, code size will be a concern, even in user space. Nowadays, devices with 64MB flash storage does a lot of things, doubling code-size as mentioned in ianmcc&#x27;s comment is terrifying.<br> </div> Mon, 14 Mar 2022 02:26:04 +0000 Inventors and heroes https://lwn.net/Articles/887757/ https://lwn.net/Articles/887757/ marcH <div class="FormattedComment"> <font class="QuotedText">&gt; &gt; Isaac Newton (one of the very few who was _really_ way ahead of his time) </font><br> <p> <font class="QuotedText">&gt; Certainly it&#x27;s disputed whether he was the &quot;inventor&quot; of Calculus, and the same is probably true of most of his other advances.</font><br> <p> OK, but:<br> <p> <font class="QuotedText">&gt; &gt; heroes make much better stories and movies</font><br> <p> So here it is: <a href="https://youtu.be/gMlf1ELvRzc">https://youtu.be/gMlf1ELvRzc</a> &quot;Then Newton came along and changed the game&quot;<br> <p> ;-)<br> <p> There is a genuine mathematician in awe of Newton in this video, so I think he was pretty far ahead. I have no idea whether he was ahead _alone_.<br> <p> <font class="QuotedText">&gt; or was it we just have no real record of their predecessors? (Because before the Royal Society there *were* no records?)</font><br> <p> I&#x27;m not an expert but I&#x27;m pretty sure we have plenty of scientific records much, much older than Newton. Newton lived more than 200 years after Gutenberg so I really doubt that was still a problem at a time.<br> <p> <p> </div> Mon, 14 Mar 2022 00:13:05 +0000 Inventors and heroes https://lwn.net/Articles/887751/ https://lwn.net/Articles/887751/ roc <div class="FormattedComment"> Shakespeare wasn&#x27;t just first, he actually was incredibly good.<br> </div> Sun, 13 Mar 2022 22:33:46 +0000 Inventors and heroes https://lwn.net/Articles/887749/ https://lwn.net/Articles/887749/ NYKevin <div class="FormattedComment"> The current consensus is that Newton and Leibniz invented calculus at more or less the same time, independently of each other, but (in the English-speaking world at least) Newton had better publicity.<br> <p> Interestingly, they also had very different ideas of what calculus was good for. Newton wanted to use it to model the physical world, while Leibniz seemed to think it had more metaphysical/philosophical significance. Arguably, they were both right, because the notion of Taylor series and analytic continuation have greatly improved our intuition and understanding of What Functions Are... and that has in turn been used to bring complex analysis into the world of physics and engineering (e.g. in the form of modern Fourier analysis). It&#x27;s all connected in the end.<br> </div> Sun, 13 Mar 2022 22:16:47 +0000 Inventors and heroes https://lwn.net/Articles/887745/ https://lwn.net/Articles/887745/ Wol <div class="FormattedComment"> <font class="QuotedText">&gt; I wouldn&#x27;t say &quot;obvious&quot; but Isaac Newton (one of the very few who was _really_ way ahead of his time) put it best: &quot;If I have seen further it is by standing on the shoulders of Giants.&quot; This sentence was apparently inspired by older, similar sentences in an interesting, &quot;recursive&quot; twist.</font><br> <p> Was he? I think he was probably one of the founding members of the Royal Society, and many of them were equally &quot;ahead of their time&quot; - or was it we just have no real record of their predecessors? (Because before the Royal Society there *were* no records? Just like Shakespeare is &quot;the greatest English playwright&quot;, not because he was necessarily any good, but also because he was the *first* major English playwright, writing at the time &quot;modern English&quot; was born.)<br> <p> And while Newton wrote &quot;In Principia Mathematica&quot;, I think it was primarily written to beat his contemporaries into the history books. Certainly it&#x27;s disputed whether he was the &quot;inventor&quot; of Calculus, and the same is probably true of most of his other advances.<br> <p> From what I can make out, the rivalry was intense, and Newton just won the publicity war.<br> <p> Cheers,<br> Wol<br> </div> Sun, 13 Mar 2022 20:32:56 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887744/ https://lwn.net/Articles/887744/ marcH <div class="FormattedComment"> <a href="https://www.ted.com/talks/linus_torvalds_the_mind_behind_linux/transcript?language=en">https://www.ted.com/talks/linus_torvalds_the_mind_behind_...</a><br> <p> <font class="QuotedText">&gt; Well, so this is kind of cliché in technology, the whole Tesla versus Edison, where Tesla is seen as the visionary scientist and crazy idea man. And people love Tesla. I mean, there are people who name their companies after him.</font><br> <p> <font class="QuotedText">&gt; The other person there is Edison, who is actually often vilified for being kind of pedestrian and is -- I mean, his most famous quote is, &quot;Genius is one percent inspiration and 99 percent perspiration.&quot; And I&#x27;m in the Edison camp, even if people don&#x27;t always like him. Because if you actually compare the two, Tesla has kind of this mind grab these days, but who actually changed the world? Edison may not have been a nice person, he did a lot of things -- he was maybe not so intellectual, not so visionary. But I think I&#x27;m more of an Edison than a Tesla.</font><br> <p> PS: funny enough Elon Musk was there early but not at the very beginning of Tesla. There&#x27;s a great interview of the Tesla founders where they (among others) acknowledge everything that came before Tesla.<br> </div> Sun, 13 Mar 2022 20:23:59 +0000 Inventors and heroes https://lwn.net/Articles/887743/ https://lwn.net/Articles/887743/ marcH <div class="FormattedComment"> <font class="QuotedText">&gt; I think the same is true of the Wright Brothers (the first successful aircraft was built in 1896 in England - it just didn&#x27;t actually fly til about 10 years later ... :-)</font><br> <p> If you&#x27;re French you know that the &quot;first&quot; flight was performed by either Clément Ader or Louis Blériot, if you&#x27;re Brazilian then you know that it was Alberto Santos-Dumont, if you&#x27;re German then maybe it&#x27;s Gustav Weißkopf / Whitehead? Etc.<br> <p> Everyone is correct as long as you tweak the definition of &quot;first flight&quot;.<br> <p> <font class="QuotedText">&gt; These things are probably obvious to any competent mind that decides it wants to tackle the problem.</font><br> <p> I wouldn&#x27;t say &quot;obvious&quot; but Isaac Newton (one of the very few who was _really_ way ahead of his time) put it best: &quot;If I have seen further it is by standing on the shoulders of Giants.&quot; This sentence was apparently inspired by older, similar sentences in an interesting, &quot;recursive&quot; twist.<br> <p> Humanity is a very social species who can&#x27;t achieve anything alone yet we love celebrating heroes. Inventors&#x27; contribution to progress is nothing compared to the Excel spreadsheets of the Department of Education but heroes make much better stories and movies and are a little bit more... inspirational! Heroes are especially important in American culture where the gaps are the widest; dispensing hope is so much cheaper. Panem, circenses and... &quot;spes&quot;?<br> <p> <font class="QuotedText">&gt; but he just happened to beat his competitor to the patent office.</font><br> <p> Exactly: whatever you do, make sure you get some lawyers and businessmen on your side. History shows you can&#x27;t make a difference without their help. <br> <p> </div> Sun, 13 Mar 2022 20:13:54 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887739/ https://lwn.net/Articles/887739/ Wol <div class="FormattedComment"> <font class="QuotedText">&gt; &gt;One might argue that all of this is self-inflicted pain caused by the continued use of C in the kernel. That may be true, but better alternatives are in somewhat short supply.</font><br> <p> <font class="QuotedText">&gt; I disagree; alternatives are not in short supply, but they have tradeoffs that some people may be unwilling to go with. The Linux kernel linked list implementation has two properties:</font><br> <p> Please note you are disagreeing with a straw man. Jon didn&#x27;t write what you imply he did - you&#x27;ve elided the very important word *better*.<br> <p> Cheers,<br> Wol<br> </div> Sun, 13 Mar 2022 16:39:05 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887737/ https://lwn.net/Articles/887737/ ianmcc <div class="FormattedComment"> It depends on how exceptions are implemented by the compiler. Exceptions require RTTI, because you need to be able to match the type of the thrown object with a corresponding catch (...) clause. When you have a try {...} block, the compiler needs to know how to unwind the stack. Most modern compilers use a &#x27;zero overhead&#x27; model, meaning that the runtime cost (in CPU time) is basically zero for the no-exception case. ie, there is no penalty for having a try {...} block if no exception is actually thrown. The way they do this is with a lookup table from the instruction pointer to the exception handler for the current stack frame. This basically doubles the code size. Also the cost of throwing an exception is quite high, so you normally only want to do it for really exceptional events, not as an alternative to a return statement. But in some cases exceptions can be faster than return codes, i.e. since you need to check the return code every time, in a tight loop using exceptions might be faster.<br> <p> More info: <a href="https://wg21.link/p1947">https://wg21.link/p1947</a><br> </div> Sun, 13 Mar 2022 15:43:49 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887732/ https://lwn.net/Articles/887732/ Wol <div class="FormattedComment"> Basically, anywhere you are hardware-constrained (and an OS is a perfect example) you need to be very careful about knowing exactly what abstractions the compiler is using. C++ makes this rather tricky!<br> <p> Once you&#x27;re in user space, especially if you&#x27;re in an online state (ie the computer is a lot faster than the person behind the keyboard), you no longer need to worry and can use the full power of C++. Just make sure you don&#x27;t use the bits you don&#x27;t understand :-)<br> <p> Cheers,<br> Wol<br> </div> Sun, 13 Mar 2022 11:39:53 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887729/ https://lwn.net/Articles/887729/ PengZheng <div class="FormattedComment"> Thanks for pointing me to SerenityOS. It seems that memory allocation failure will lead to crash:<br> <a href="https://github.com/SerenityOS/serenity/blob/master/AK/Vector.h#L87-L92">https://github.com/SerenityOS/serenity/blob/master/AK/Vec...</a><br> <p> I assume (it might be false) that C++ exception is not suitable for kernel development and some constrained embedded environment. <br> Are there any good guidelines for such C++ usages?<br> </div> Sun, 13 Mar 2022 10:08:17 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887707/ https://lwn.net/Articles/887707/ Wol <div class="FormattedComment"> The same way all these things happen.<br> <p> Alexander Graham Bell is credited with the invention of the telephone, but he just happened to beat his competitor to the patent office. I think the same is true of the Wright Brothers (the first successful aircraft was built in 1896 in England - it just didn&#x27;t actually fly til about 10 years later ... :-)<br> <p> These things are probably obvious to any competent mind that decides it wants to tackle the problem.<br> <p> Cheers,<br> Wol<br> </div> Sat, 12 Mar 2022 19:28:24 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887704/ https://lwn.net/Articles/887704/ moorray <div class="FormattedComment"> I suggested exactly what Linus &quot;came up with&quot; a week later - solution #2 here:<br> <a href="https://lore.kernel.org/all/20220228223228.24cf3fd4@kicinski-fedora-PC1C0HJN.hsd1.ca.comcast.net/">https://lore.kernel.org/all/20220228223228.24cf3fd4@kicin...</a><br> <p> I wonder how these things happen...<br> </div> Sat, 12 Mar 2022 17:48:23 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887703/ https://lwn.net/Articles/887703/ alonz Actually in C11 it <b>is</b> possible to create a shadowing variable. However, this usually triggers a compiler warning - since such shadowing is rather confusing to the readers, and is usually considered bad practice (and a harbinger of confusing bugs). Sat, 12 Mar 2022 17:04:42 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887688/ https://lwn.net/Articles/887688/ droundy <div class="FormattedComment"> If C had the ability rust has to create a new variable shadowing the name of a former one, the macro could just create a new variable of a the same name but a different type to make any further use cause a compile error.<br> <p> But of course if this weren&#x27;t C we wouldn&#x27;t have this problem.<br> </div> Sat, 12 Mar 2022 02:59:03 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887674/ https://lwn.net/Articles/887674/ timon <div class="FormattedComment"> As I read this, you misunderstood the &quot;cpp&quot; part; the OP is talking about the C preprocessor and you are talking about C++.<br> </div> Fri, 11 Mar 2022 18:55:35 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887580/ https://lwn.net/Articles/887580/ geert <div class="FormattedComment"> Mindlessly adding preinitializations to uninitialized variables instead of fixing the real problems prevents the compiler from flagging newly introduced uninitialized users of the same variable later.<br> <p> BTW, I used to fix all newly introduced non-false positives reported by gcc-4.1.2, which was rather strict (compared to later gcc versions), and caused lots of false positives. But every kernel release, I did catch a handful of real bugs. Later, gcc improved, Arnd started to build the kernel with bleeding edge gcc versions catching more bugs before I found them, and support for gcc-4.1.2 was dropped. But I believe there still are areas for improvements...<br> </div> Fri, 11 Mar 2022 11:20:03 +0000 Initialization https://lwn.net/Articles/887568/ https://lwn.net/Articles/887568/ bartoc <div class="FormattedComment"> Note, I&#x27;m talking more about /RTCs rather than &quot;InitAll&quot;, which might work better.<br> </div> Fri, 11 Mar 2022 01:29:10 +0000 Initialization https://lwn.net/Articles/887567/ https://lwn.net/Articles/887567/ bartoc <div class="FormattedComment"> MSVC&#x27;s behavior is a bit of a problem because there&#x27;s no great opt-out, and it doesn&#x27;t really do the optimizations in debug mode, meaning if you ever use stack arrays it&#x27;s really easy to have awful debug mode performance. It&#x27;s one of the bigger &quot;MSVC debug perf sucks&quot; things around (along with like, not having a way to write inline functions that are always inlined, even in debug).<br> <p> If there was an opt-out, and if MSVC had a saner &quot;debug mode&quot; (really these optimizations should be turned on in debug mode imo) then it would be a great behavior.<br> </div> Fri, 11 Mar 2022 01:25:24 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887566/ https://lwn.net/Articles/887566/ roc <div class="FormattedComment"> My point is that a lot of those &quot;dead stores&quot; are already being performed by kernel hardening. <a href="https://github.com/torvalds/linux/blob/186d32bbf034417b40e2b4e773eeb8ef106c16c1/security/Kconfig.hardening#L31">https://github.com/torvalds/linux/blob/186d32bbf034417b40...</a><br> <p> So on one hand we&#x27;ve got developers saying &quot;adding dead stores hurts performance, don&#x27;t do it and keep that essential warning disabled&quot;, and on the other hand developers are adding dead stores all over the place for the sake of security.<br> </div> Fri, 11 Mar 2022 01:24:34 +0000 Initialization https://lwn.net/Articles/887562/ https://lwn.net/Articles/887562/ tialaramex <div class="FormattedComment"> There was a CPP Con talk some years back about exactly this work by Microsoft. Basically let us teach the compiler to zero initialize everything, then run tests to see how awful the results are, teach the compiler to optimize away these dead stores and/or perform zero initialization that it more easily recognises as dead stores, iterate. IIRC The first few loops are very, very bad, nothing you could possibly ship, but as they learn what they&#x27;re doing by the end they can more or less do this over the bulk of the system and get the same performance as before but with more confidence there aren&#x27;t scary initialisation bugs. Obviously they were in C++ and their own C++ compiler, where Linux would want C and GCC, but it&#x27;s not so different otherwise. If people can&#x27;t find it themselves I can search Youtube maybe.<br> </div> Fri, 11 Mar 2022 00:29:09 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887561/ https://lwn.net/Articles/887561/ NYKevin <div class="FormattedComment"> If the compiler wrongly thinks that a variable is used before initialization, then it will probably fail to optimize away the dead store resulting from eager initialization. Whether that dead store is worth worrying about is another matter, of course, and probably depends on how hot a given codepath is.<br> </div> Thu, 10 Mar 2022 23:53:23 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887556/ https://lwn.net/Articles/887556/ roc <div class="FormattedComment"> <font class="QuotedText">&gt; the &quot;use of uninitialized variable&quot; warning is disabled in the kernel due to excessive false positives.</font><br> <p> Wow, this is terrible. I had no idea.<br> <p> Given there are hardening patches to force zeroing of local variables, why not just initialize them in the source as necessary to eliminate the existing warnings and turn that warning on?<br> </div> Thu, 10 Mar 2022 22:50:30 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887555/ https://lwn.net/Articles/887555/ roc <div class="FormattedComment"> The main point of Rust in the kernel is to get much-improved safety properties. Zig doesn&#x27;t provide that.<br> </div> Thu, 10 Mar 2022 22:47:44 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887553/ https://lwn.net/Articles/887553/ ppisa <div class="FormattedComment"> I have need for poor STL relative in C year ago and uLUt library was a result. Because it has been intended even for use in kernel at the times of RTLinux I have decide to be compatible/reuse kernel list but provide type-safe interfaces for each relation<br> <p> <a href="https://sourceforge.net/p/ulan/ulut/ci/master/tree/ulut/ul_list.h">https://sourceforge.net/p/ulan/ulut/ci/master/tree/ulut/u...</a><br> <p> This way you never use container_of directly and risk to use incorrect offset between list node location and start of containing structure is suppressed. For AVL, hashes and sorted arrays I have used other structures than kernel. The library provides even iterators which works over all of these basic types.<br> <p> But macros which expands to the static inline functions are really huge. But reasonable compilers generate code better for these variants with CUSTom prefix than when generic functions with void pointers are used.<br> <p> The library is base in many project, our RS485 multimaster project <a href="http://ulan.sourceforge.net/">http://ulan.sourceforge.net/</a> , CAN and CANopen <a href="http://ortcan.sourceforge.net/">http://ortcan.sourceforge.net/</a> more instruments using inhouse our SmallToolKit graphic library <a href="https://gitlab.com/pikron/sw-base/suitk/-/wikis/home">https://gitlab.com/pikron/sw-base/suitk/-/wikis/home</a> which has been used even is safety sensitive medical devices etc.<br> </div> Thu, 10 Mar 2022 22:13:51 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887551/ https://lwn.net/Articles/887551/ bartoc <div class="FormattedComment"> Note that what the list iterator does is pretty far from how C++ iterators work. This style of iterator is &quot;I write the loop, and I&#x27;ll insert your body with the right item all set up&quot;, where C++ is &quot;you write the loop, and ask me for each item&quot;<br> <p> There&#x27;s nothing really preventing you from doing the C++ approach in C, although you need to be careful about inlining, you just write an &quot;iterator init from structure&quot; &quot;iterator next&quot; and &quot;is iterator done&quot; function and call them in the appropriate places in a for loop. However I think the more intrusive way of doing things is quite a lot clearer when reading the iterator implementation, esp for more complicated iterators.<br> </div> Thu, 10 Mar 2022 21:55:02 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887549/ https://lwn.net/Articles/887549/ bartoc <div class="FormattedComment"> I&#x27;m concerned about how long zig tends to defer typechecking of code. Maybe it doesn&#x27;t matter in the real world though.<br> <p> I really love how zig does generics; you write type constructors as just .... normal functions that return a type!<br> </div> Thu, 10 Mar 2022 21:47:07 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887537/ https://lwn.net/Articles/887537/ pankyraghv <div class="FormattedComment"> I know many people in the kernel community hates cpp but I would recommend everyone to take a look at SerenityOS. They have done a fantastic job of rolling out an in-house standard library that actually makes coding in cpp enjoyable (mostly) while reaping the benefits of all the modern features that can catch bugs at compile time. IMO a subset of cpp with a good custom standard library would be a better fit to writing kernel drivers than use something like Rust which has a very different programming paradigm. I also feel something light like Zig might also be a good option.<br> </div> Thu, 10 Mar 2022 20:04:40 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887520/ https://lwn.net/Articles/887520/ EnigmaticSeraph <div class="FormattedComment"> The kernel generally tolerates no performance hits, esp. with such a widely-used structure. And while there exists e.g.:<br> <a href="https://www.boost.org/doc/libs/1_78_0/doc/html/intrusive/list.html">https://www.boost.org/doc/libs/1_78_0/doc/html/intrusive/...</a><br> , which can be configured to be as would&#x27;ve been at the C level, but generic. However, kernel devs tend to be wary of C++ in the kernel, for not-unfounded reasons.<br> </div> Thu, 10 Mar 2022 17:52:18 +0000 Toward a better list iterator for the kernel https://lwn.net/Articles/887515/ https://lwn.net/Articles/887515/ jengelh <div class="FormattedComment"> <font class="QuotedText">&gt;One might argue that all of this is self-inflicted pain caused by the continued use of C in the kernel. That may be true, but better alternatives are in somewhat short supply.</font><br> <p> I disagree; alternatives are not in short supply, but they have tradeoffs that some people may be unwilling to go with. The Linux kernel linked list implementation has two properties:<br> <p> - the linked list metadata (prev/next) pointers is not separated from the struct =&gt; layering violation<br> - an object can be part of multiple linked list =&gt; who&#x27;s the owner responsible for cleanup?<br> (- the iterator interface of list_head is just a consequence of the two)<br> <p> And these are approached with<br> <p> - inode is, in terms of responsibility, strictly a child of the list and/or its internal metadata<br> - only one owner is allowed so there is no doubt who has to clean up<br> <p> Using C++ as a concrete example now, one would arrive at:<br> <p> 1. Use std::list&lt;std::shared_ptr&lt;inode&gt;&gt;. Safe from leaks, safe from dangling pointers, but it needs refcounting to do the job, and some people may not like the performance characteristics of that refcounting.<br> 2. Use std::list&lt;inode&gt; for the main list, and std::list&lt;inode *&gt; for the secondaries. Cleanup is still guaranteed, but you traded refcounting for the potential danger of dangling pointers.<br> (3. Keep on allocating your inodes and lists manually like before... as C++ is almost source-compatible with C.)<br> </div> Thu, 10 Mar 2022 17:08:06 +0000 My linked lists https://lwn.net/Articles/887513/ https://lwn.net/Articles/887513/ abatters For non-circular linked lists in my userspace code, my approach is to define a dedicated macro for each specific list, defining the name of its head, tail, forward, and back pointers. Then that specific macro can be used in combination with one of ~25 generic doubly-linked list macros I have defined. Works well for having a single object in multiple lists, and having multiple lists in a single container. No iteration macro is necessary, since you have pointers to the real types (no container_of needed) and it terminates with NULL. <pre> // Generic doubly-linked list macro (one of ~25) #define DLIST_ADD_TAIL(head, tail, forw, back, item) \ do { \ if ((tail) == NULL) { \ (head) = (item); \ } else { \ (item)-&gt;back = (tail); \ (tail)-&gt;forw = (item); \ } \ (tail) = (item); \ } while (0) // Example linked list structs struct item { struct item *iforw, *iback; }; struct container { struct item *ihead, *itail; }; // Example macro defining a specific list #define CONTAINER_ITEM_LIST(container, func, args...) \ func((container)-&gt;ihead, (container)-&gt;itail, iforw, iback , ## args) void container_add_item_tail(struct container *container, struct *item) { CONTAINER_ITEM_LIST(container, DLIST_ADD_TAIL, item); } </pre> Thu, 10 Mar 2022 16:15:56 +0000