Why not write OS kernel with C++?

来源:互联网 发布:床垫推荐 知乎 编辑:程序博客网 时间:2024/05/16 15:06

Summary as a memo.

 

Some technical reasons why C might be preferred over C++ for things like kernels.

- Performance. C++ requires a more complicated runtime to support things like exception handling and RTTI. 

  This can be hard to provide in an unhosted environment. Compilers do permit you to switch them off.

- Portability.

  Portability of C code between compilers has been far better. 

  Portability of C++ code was long something that required a lot of discipline to achieve. 

  See the Mozilla portability guide for insight into the lengths that programmers go to to create portable C++.

- New architectures and platforms will typically have a C compiler long before they have a C++ compiler. 

  C is a much simpler and easier language to implement.

- Controllability. Apparently simple statements can hide expensive operations, thanks to operator-overloading. 

  This would normally be considered a Good Thing, but in an embedded/kernel development world 

  people like to be able to see where expensive operations are being performed.

- C++ compilers mangle symbols, which can be a bitch in linker scripts and other supporting infrastructure.

- C++ more complex, seduce programmer to write bloatware, not everyone is C++ uber-guru.

 

Some non-reasons:

- C++ is slower than C. 

  C++ has the same overheads as C. Additional overheads typically only arise when using features C doesn't support.

- Virtual dispatch is slow. 

  Virtual dispatch is slower than static dispatch, but the performance penalty is modest, particularly 

  when used judiciously. The Linux kernel already makes wide use of jump tables for performing dynamic dispatch.

- Templates cause massive code bloat. 

  This is potentially true. However the Linux kernel uses macros to perform similar code generation effects, 

  for instance creating typed data structures, or for retrieving a containing structure from a pointer to a member.

- Encapsulation hurts performance.

- C++ supports exception. Actually, C can support exception with setjmp/longjmp. However, it's not a good idea to

  implement exception in kernel because it only only bloats code size, but also increases maintaining workload

  (they're architecture-dependent and need to be updated with any changes to kernel registers)

 

Some reasons that a kernel in C++ might be a good idea:

- Less boiler plate code to use the common dynamic dispatch pattern.

- Templates give a safer way to perform simple code generation, with no performance penalty over macros.

- The class mechanism encourages programmers to encapsulate their code.

 

 

Why isn't Linux kernel written in C++?

 

Linux answered:

The fact is, C++ compilers are not trustworthy. They were even worse in 1992, but some fundamental facts haven't changed:

 - the whole C++ exception handling thing is fundamentally broken. It's _especially_ broken for kernels.

 - any compiler or language that likes to hide things like memory allocations behind your back 

   just isn't a good choice for a kernel.

 - you can write object-oriented code (useful for filesystems etc) in C, _without_ the crap that is C++.

 

In general, I'd say that anybody who designs his kernel modules for C++ is either 

 (a) looking for problems

 (b) a C++ bigot that can't see what he is writing is really just C anyway

 (c) was given an assignment in CS class to do so.