Live data from Hacker News

Converting the Kernel to C++

lore.kernel.org

21–30 of 56 posts

Re: Converting the Kernel to C++

#21

I'm skeptical that this is better than starting to rewrite the kernel in Rust. It would be better if everyone can focus on rewriting things into Rust rather than having a choice of rewriting into C++ or Rust. Unlike this email, C can be converted into Rust piecemeal and integrate with the rest of the kernel. It's also in kernel developers best interest to start learning Rust, and getting them to start earlier than la…

> I'm skeptical that this is better than starting to rewrite the kernel in Rust.

I recently migrated a complex cross platform C99 embedded project to C++17. We got the build systems ported in a day and are able to chip away at it piece by piece.

We looked at Rust but the main issue wasn't technical it was the large learning curve for the team. Rust is not as intuitive to C developers are C++ is.

Re: Converting the Kernel to C++

#22
post #12
post #6

Earlier quoted context omitted.

> Unlike this email, C can be converted into Rust piecemeal and integrate with the rest of the kernel. Did you read the entire email? Here's a quote that seems to directly contradict you: > converting C code to Rust isn't something that can be done piecemeal, whereas with some cleanups the existing C code can be compiled as C++. As far as I can tell, the author is correct. What is the pattern for converting C to Rust…

> What is the pattern for converting C to Rust piecemeal? You can call Rust from C, and C from rust. You can convert it function by function.

Won’t you end up with mostly _unsafe_ code this way?

Re: Converting the Kernel to C++

#23

Earlier quoted context omitted.

EDIT: I failed to notice the date stamp, you can ignore this. Jeeze. Linus writes like C++ killed his dog or something. There's plenty of great C++ 'wins' over C that make just doing everyday programming tasks so much easier and simpler for no cost. You don't need to write so abstract and in the clouds that it becomes impossible to maintain - I certainly don't. And frankly, you can shoot yourself in the foot just as…

> makes it out to be. made it out to be. That post is over 15 years old at this point, which goes to the very point of the posted article - C++ has (subjective opinion here) improved a lot in that time. No idea what his current opinion on C++ is. Personally I'd rather there was no C++ or Rust in the Linux kernel. IMHO it would be significantly less of a cognitive burden to convert the entire kernel gradually to C++ t…

Mea culpa, I did not notice the date stamp. C++ 15 years ago was certainly much worse than today.

Re: Converting the Kernel to C++

#24
post #12

Earlier quoted context omitted.

> What is the pattern for converting C to Rust piecemeal? You can call Rust from C, and C from rust. You can convert it function by function.

Won’t you end up with mostly _unsafe_ code this way?

It is true that the boundary will be unsafe, and that's more than if it were to be all in pure Rust, but that does not change that, even when doing kernel level work, in practice it has generally been shown that unsafe code is still relatively small in amount.

Re: Converting the Kernel to C++

#25
post #12

Earlier quoted context omitted.

> What is the pattern for converting C to Rust piecemeal? You can call Rust from C, and C from rust. You can convert it function by function.

Won’t you end up with mostly _unsafe_ code this way?

It really depends. If the interface that you're wrapping can have a nice, abstract API (like "parse this JPEG header into a struct" or something), then the hope is that you can figure out how to express that API in safe Rust, even though underneath it might be a big pile of C. The trouble is when there is no clean API boundary that can fit within safe Rust's rules, like when the underlying C code is "object soup" where everything has pointers to everything else, and it's all up to the programmer to know when it's safe to free things. In that case trying to wrap all that in Rust either gives you unsafe code everywhere, or maybe worse, "safe" APIs that are lying to you and are actually unsound.

Re: Converting the Kernel to C++

#26
As with all good writing, it is hard for me to tell serious suggestions from satire.

However, hpa seems to be the crazy genius type, so maybe it's all for real.

The thing I'd like to know is, given that the kernel is written in "kernel C", a shared culture about how C should be used together with a hairy mountain of macros, why not make kernel-C a proper language?

It's probably just a number of extensions away. Together with some rules about code generation, it could make for a fairly neat dialect of C that would be much easier to use and understand. It would also pave the way for further experiments with compile time guarantees about soundness of isolated sections of the code.

Because, and we should be honest about this, going any C++-like route would impact the long term quality of submitted code. There should be no technical reason for this, but that does not make it something that should be turned a blind eye on.

Re: Converting the Kernel to C++

#27
post #20

The path to migrate to Zig is, or would be, the most straightforward, except that that language is not ready. But in design terms it's definitely got the things the OP is championing C++ for, and a better story for actually addressing longstanding systems programming issues instead of heaping stuff onto the C toolchain and therefore making everyone debug C build-time errors.

Seeing that the last time I tried to use Zig–completely inexperienced, trying to do completely basic stuff–I hit a compiler bug within two hours, I'd say that "not ready" is about right.

To be fair, the latest Github version had the bug already fixed, but it's still a sign the language is basically still in an alpha stage.

Re: Converting the Kernel to C++

#28
post #13

I'm skeptical that this is better than starting to rewrite the kernel in Rust. It would be better if everyone can focus on rewriting things into Rust rather than having a choice of rewriting into C++ or Rust. Unlike this email, C can be converted into Rust piecemeal and integrate with the rest of the kernel. It's also in kernel developers best interest to start learning Rust, and getting them to start earlier than la…

I'm skeptical because C++ is a slippery slope. Glad to see they are going for templates and concepts, and not constructors and destructors. Yes, what C++ is supposedly good for – RAII, it actually got a little wrong: 1. Default construction / value initialization: Causes lots of initialization before assignment that is obviously unnecessary. Try allocating a buffer: `std::make_unique ` surprisingly memsets the buffer…

> 4. Destructors: Can't take arguments, forcing objects to contain implicit references to each other.

This issue has been mentioned back in 2018: https://lore.kernel.org/lkml/CAOMGZ=HwTjk9JbGNeWHd+Jr3rwOkFO...

Re: Converting the Kernel to C++

#30
post #20

The path to migrate to Zig is, or would be, the most straightforward, except that that language is not ready. But in design terms it's definitely got the things the OP is championing C++ for, and a better story for actually addressing longstanding systems programming issues instead of heaping stuff onto the C toolchain and therefore making everyone debug C build-time errors.

The reasoning behind switching to C++ today rather than Rust, Zig, or any other language is familiarity. C++, while it currently doesn't have memory safety, is extremely easy for any C programmer to ease into, especially given the limited subset that Anvin is proposing for kernel use. Meanwhile, Zig and Rust look absolutely awful.

As somebody who's been using C++ for eight years, I despise Rust and Zig (and Nim and...) syntax. Don't get me wrong, I like the concept of memory safety, but I think too many languages are going "We have to stand out!" and therefore are creating completely new syntaxes. That may work for you, but frankly, I'm happiest in a language that sticks close to C++ syntax (which is probably one reason I love D so much).

Also, C build errors have nothing to do with C++? What are you getting at there?

Post reply on HN