The fear of C++ in operating systems kernels
- einsteinjunior
- Member
- Posts: 90
- Joined: Tue Sep 11, 2007 6:42 am
The fear of C++ in operating systems kernels
I just wish to know why people fear ( or should i say dislike) so much to write an OS in C++.
Whats wrong with C++?
In any case,i am designing a new Operating System Architecture for Object Oriented Language so people will think much more about writting object oriented kernels.
You will surely be surprised by the end result......
Whats wrong with C++?
In any case,i am designing a new Operating System Architecture for Object Oriented Language so people will think much more about writting object oriented kernels.
You will surely be surprised by the end result......
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
I chose not to use C++ in my kernel, not because I feared it, but because I wasn't interested in learning how to implement the ABI. I wanted to keep things as simple as possible so I could focus on learning the kernel implementation details rather than language implementation details.
If you're wondering about the Linux kernel developers' negative opinions about C++, I think it's a lot of FUD and techno-religious zeal more than anything else.
If you're wondering about the Linux kernel developers' negative opinions about C++, I think it's a lot of FUD and techno-religious zeal more than anything else.
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
There's nothing inherently wrong with C++. The main reason people use C is that C++ is slightly crippled in a freestanding environment. You can't use part of the language features, nor the STL libraries without implementing a runtime first. Some of those features practically require a working kernel while you yet have to build one.
The advantage of C is that you can use everything that is part of the actual language without having to care about that nonexistant runtime. It's basically a burden less, which people don't mind sacrificing over the absence of OOP features.
People make fun of me for writing an OS with Basic, because there the runtime problem is several magnitudes larger than in C++.
The advantage of C is that you can use everything that is part of the actual language without having to care about that nonexistant runtime. It's basically a burden less, which people don't mind sacrificing over the absence of OOP features.
People make fun of me for writing an OS with Basic, because there the runtime problem is several magnitudes larger than in C++.
Hi,
I'm writing my kernel in C++ and have previously written test-kernels in C. I have working new and delete operators, pure virtual functions, global and static objects and palcement new. I do not have RTTI or exceptions, but do not want these in the kernel (I plan to port libsupc++ once I have worked on my system call interface).
Personally, I much prefer using C++ to C and this is something which is going to vary a lot depending on the programmer. Pick whatever you are more comfortable with. Please read the wiki C++ page for more detail, but to give you some thoughts:
1) New and Delete
No worse than in C. You need some kind of working malloc() anyway. Once you have malloc() and free(), new and delete just sort of happen. A word of caution: make sure your heap manager works reliably. Once you have new and delete, a lot of your memory allocations seem to happen 'transparently' - or you think about it less. If you heap manager is buggy, you will have a hell of a job debugging anything else.
2) Global and Static objects
Really easy. My architecture-dependent code fires up, starting the physical memory manager, paging and the heap manager. Once this has happened and New and Delete work, a support function cycles through calling all global and static constructors (found from a linker script label). In this way, a global object can use New/Delete in its constructor.
To be honest, these are the two main things to implement for C++ support. Placement New and Pure Virtual Function support, just need a few lines of code. Decent exception handling and RTTI are a different kettle of fish - better to port an existing standard library a bit later on, IMHO.
As for bootstrap code, my entry point is a C++ main function. This is called directly by my boot loader which also passes an argc and argv, containing parameters passed from the boot loader with some machine state information.
Anyway, if you want to code a C++ kernel and are already very familiar with C++, go for it!
Cheers,
Adam
I'm writing my kernel in C++ and have previously written test-kernels in C. I have working new and delete operators, pure virtual functions, global and static objects and palcement new. I do not have RTTI or exceptions, but do not want these in the kernel (I plan to port libsupc++ once I have worked on my system call interface).
Personally, I much prefer using C++ to C and this is something which is going to vary a lot depending on the programmer. Pick whatever you are more comfortable with. Please read the wiki C++ page for more detail, but to give you some thoughts:
1) New and Delete
No worse than in C. You need some kind of working malloc() anyway. Once you have malloc() and free(), new and delete just sort of happen. A word of caution: make sure your heap manager works reliably. Once you have new and delete, a lot of your memory allocations seem to happen 'transparently' - or you think about it less. If you heap manager is buggy, you will have a hell of a job debugging anything else.
2) Global and Static objects
Really easy. My architecture-dependent code fires up, starting the physical memory manager, paging and the heap manager. Once this has happened and New and Delete work, a support function cycles through calling all global and static constructors (found from a linker script label). In this way, a global object can use New/Delete in its constructor.
To be honest, these are the two main things to implement for C++ support. Placement New and Pure Virtual Function support, just need a few lines of code. Decent exception handling and RTTI are a different kettle of fish - better to port an existing standard library a bit later on, IMHO.
As for bootstrap code, my entry point is a C++ main function. This is called directly by my boot loader which also passes an argc and argv, containing parameters passed from the boot loader with some machine state information.
Anyway, if you want to code a C++ kernel and are already very familiar with C++, go for it!
Cheers,
Adam
I concur - there's nothing scary about writing a kernel in C++. Pedigree's entire C++ support file comes to 2.5KB, and most of that is the BSD licence at the top! The most complex bit of C++ support code is probably trawling through the initial constructor list, but when you've got it simplified this is only a few lines:
And even that could be reduced more, but I decided not to, for clarity.
There's nothing to fear about C++, and the purported speed reduction is actually rubbish: It is only seen when using virtual functions, which have no native equivalent in C anyway! You get things like object orientation and inheritance for free.
I would advise avoiding multiple inheritance, RTTI and exceptions though.
Code: Select all
/// Calls the constructors for all global objects.
/// Call this before using any global objects.
void initialiseConstructors()
{
// Constructor list is defined in the linker script.
// The .ctors section is just an array of function pointers.
// iterate through, calling each in turn.
uintptr_t *iterator = reinterpret_cast<uintptr_t*>(&start_ctors);
while (iterator < reinterpret_cast<uintptr_t*>(&end_ctors))
{
void (*fp)(void) = reinterpret_cast<void (*)(void)>(*iterator);
fp();
iterator++;
}
}
There's nothing to fear about C++, and the purported speed reduction is actually rubbish: It is only seen when using virtual functions, which have no native equivalent in C anyway! You get things like object orientation and inheritance for free.
I would advise avoiding multiple inheritance, RTTI and exceptions though.
The bad part with C++ is not what people seem to think. With exceptions disabled you don't get any overhead using C++ (of course you could use virtual functions, but the equivalent code in C will boil down to the similar solution).
The real issue is that C++ is not a simplier language but rather much more complex. There's a lot of mechanisms in the language that generate function calls and other code implicitely if you don't pay attention. There may also be situations where the language is not doing quite what you might expect (dependent on what your knowledge of the language is). You have to know what's going on behind the scene, more so than in C (then the recommendations become obsolete because you'd know what you pay for RTTI, exceptions and virtual functions and so on).
C++ is mostly OK, but make sure you know the language well.
The real issue is that C++ is not a simplier language but rather much more complex. There's a lot of mechanisms in the language that generate function calls and other code implicitely if you don't pay attention. There may also be situations where the language is not doing quite what you might expect (dependent on what your knowledge of the language is). You have to know what's going on behind the scene, more so than in C (then the recommendations become obsolete because you'd know what you pay for RTTI, exceptions and virtual functions and so on).
C++ is mostly OK, but make sure you know the language well.
The main reason being that the implementation of those three things are extremely complex, and could take a non-veteran c++ user unawares, along with the fact that they require more runtime assistance (especially rtti).Candy wrote:Why?JamesM wrote:I would advise avoiding multiple inheritance, RTTI and exceptions though.
Apple decided against them because they were "unsafe", in their words, for their project. I forget which one it was - could have been IOKit, or some part of macosx. I forget because I've lost the url I read it on.
- einsteinjunior
- Member
- Posts: 90
- Joined: Tue Sep 11, 2007 6:42 am
I will really not implement RTTI and multiple inheritance in my OS,I would advise avoiding multiple inheritance, RTTI and exceptions though.
they are way tooo complex to be implemented at the moment.
So for all of you wanting to writre a C++ opearing system,you should stick to very simple designs,only put in place "useful abstractions" keep away from exception handling.
Although a structured exception handler could help.But the design is also complex to put in place.
come on, no c and asm in microsoft windows?Candy wrote:Maybe you should start listening then?binutils wrote:i have never heard of anything about os written in c++ in first place, even microsoft windows
afaik, core is c&asm, wrap in c++. wrap in c++ library.
--
PS: if you don't believe me, see winnt leak source or reactos-0.34(current version) source
Last edited by binutils on Sun Apr 06, 2008 7:12 am, edited 1 time in total.