OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Sep 29, 2020 8:24 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 38 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 2:12 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7469
Location: Germany
BTW, one thing they say in the LKML is quite true: You can write object-based code in C, too. C++ offers stuff like namespaces, "class" declarations and inheritance, but you can write C code in a way that focusses on the data object instead of the functions just as well.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 2:52 am 
Yet when you do this, you reinvent most things C++ does for you ...


Top
  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 2:54 am 
i don't think it's better nor do i think i a bad thing to write a kernel in c++, i do think it's a chalange and i want to see if i can do it,I do realize with that litle recources out there it'll be hard but then aggain i have a lifetime to learn right :) ;)


Top
  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 3:00 am 
Offline
Member
Member

Joined: Wed Oct 18, 2006 11:59 am
Posts: 1600
Location: Vienna/Austria
I usually do it like this to maintain objects in c:

Code:
struct myobject{
  int anydata;
  char anydate[10];
  int anymoredata;
  struct myobject *next;
  struct myobject *prev;
  int(* insert)(struct myobject *,struct myobject *);
  int(* delete)(struct myobject *,struct myobject *);
  char *(* serialize)(struct myobject *);
  ...
}


the first argument of each function is the pointer *this* - the self reference. It comes in handy to have this at hands *gg*.
The function pointers you have to fill in with the appropriate implementations at creation of the object, and here you go. It is of course a bit tedious to have to do nearby everything by yourself, but you *can* use objects in c too.

If someone has a better way to do this, pls feel invited to speak :-)

_________________
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image


Top
 Profile  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 3:05 am 
I think this example is better suited for C++ ;)

Code:
class myobject {
public:
  myobject ();

  int insert (myobject *);
  int delete (myobject *);
  char* serialize ();

private: /* Now this will be a problem to do in C */
  int anydata;
  char anydate[10];
  int anymoredata;
  myobject *next;
  myobject *prev;
};


Of course you can do this in C, but I think in C++ it works out much better at the details ...


And using C++ in Linux and using C++ in your own kernel are two different topics. Linux is a bit far too much C code to be ported now reasonably, however, this should stop no one from using C++ in their own kernels! ;)


Top
  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 3:23 am 
Offline
Member
Member

Joined: Wed Oct 18, 2006 11:59 am
Posts: 1600
Location: Vienna/Austria
hmmm .... Legend, you *have* seen, that I spake in terms of C, not C++, haven't you?

It comes down to the sort of run time support, c++ requires for the handling of namespaces, objects, classes and exceptions (to speak roughly), that c doesn't offer. Where is example code for that kind of stuff? how do you implement the runtime support for a structure like try...catch ... finally (we know, we know, its sorta setjmp,longjmp - but there are exception callbacks added to the JCB of the process - by the C++ runtime. They work 'like' signal handlers: a flow of control is redirected to a place, where data is stored away and then the stack is wound back with longjmp - to the location of catch?) Now, where outta there is information on how to implement this in kernel land? To get support for it?

Time to get my share of the fill - and have brunch

_________________
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image


Top
 Profile  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 3:23 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7469
Location: Germany
Legend, no-one argues the fact that C++ was designed to work with objects, while C is not.

But, as engine252 pointed out, there's much more information on kernel space C available, you don't need that much inner knowledge of the language, and you can't get bitten by some of the problems that lurk in the depths of C++. It can be an alternative to write object-based C instead of going for a C++ kernel. (Again, I don't argue that there are nice things to be done in C++, which is why I do my own kernel in C++, too.)

The "problem" with C++ is that it might create objects where you don't expect it to do so (which can become tricky if you are doing stuff in your constructors), and that it's easy to waste clock cycles and memory if you're careless.

Legend wrote:
Code:
private: /* Now this will be a problem to do in C */



All that a "private" declaration in C++ does for you is to remind you if you're doing some access you shouldn't do, at compile time. If you need that kind of reminder, you rather shouldn't touch kernel space C++...

What C++ does, and which I consider the biggest "pro" for kernel space C++, are namespaces, function / operator overloading, and default parameters - which C can't do for you at all.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 3:30 am 
I know that your structure was for C code, but I think it was a good example for C++.

Yet exception handling in kernel space would be a bad idea. Exception handling mostly needs os help, and you are the os, so where to get the help? At least it would be a bigger problem. When you use G++, you may try to include the libs that come with it, they implement those functions (and require some libc functions that you have to implement then)

And Solar, if it creates objects where you don't except it to do, I could say then better don't touch kernel space c++ either :P

Having private fields in a class is mostly not for you, it is mostly for other developers (if you have some ;) ) that use your code and should not mess around with those fields directly. Perhaps they don't know that they should not do this access, and now they will see that they should not (even if they get a compilation error then)!

Of course you could still do some funny pointer math to access the field, but if it is that urgent, then your program is strange.


Top
  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 3:46 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
as for "private:"

Code:
struct my_stuff {
     blah blah blah
#ifdef __OWNER_MODULE_C
     define private stuff
#else
     equivalent amount of padding
#endif
};


But as solar said, do you *really* want this ... You may also simply run a "grep" on your code and see where there are uses of my_stuff that could be untidy

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 3:49 am 
Offline
Member
Member

Joined: Wed Oct 18, 2006 11:59 am
Posts: 1600
Location: Vienna/Austria
Legend, look:

some Exceptions are thrown: you *need* a mechanism to handle the *throw* statement. This is in the library, and the c++ runtime knows which function to call when *throw* comes along.

other exceptions are generated by the cpu. f.ex. Division by Zero exception. If they occur in the c++ process (to speak roughly - you know what I mean), you need a way to redirect the flow of control to our *throw* function.

That's done by registering kinda exception handler inside the kernels JCB structure for this special process - or similar. There may be other ways to do this.

repeat: we *need* a way to communicate *throwable* cpu exceptions to the process so that it can deal with them.

SOmething like a GPF is nothing I consider *throwable*, so it might kill the faulting process, it 's been warned not to leak any data.

Hope I've made myself clear.

_________________
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image


Top
 Profile  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 4:00 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7469
Location: Germany
@ Legend:

The trick in "educating" your developers is usually in making the "right" way to do something the easiest.

And, as Pype pointed out, you could write up "public" and "private" headers to solve the "problem"...

In short, yes C++ caters for OO programming, yes it is possible writing a kernel in C++, no the Linux people don't think it's a good idea, no I don't agree with them.

'nuff said.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 4:13 am 
Offline
Member
Member

Joined: Wed Oct 18, 2006 11:59 am
Posts: 1600
Location: Vienna/Austria
Some folks say no to something because they fear not to be able to *do* it.

There is some saying outta there:

Those who know that *it* can't be done sha'n't interrupt those who are doing *it*.

_________________
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image


Top
 Profile  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 8:03 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
Solar wrote:
ters for OO programming, yes it is possible writing a kernel in C++, no the Linux people don't think it's a good idea, no I don't agree with them.

I haven't read from LKML that they think writing "a kernel in C++" is not a GoodIdea (tm), just that "Rewriting the Linux Kernel" is not a GoodIdea (tm) ... and concerning this, i *do* agree with them. Porting a millions line multi-platform (thus multi-compilers) project towards a language that's beginning to have a commonly accepted ABI ... argh.

But i'm supporting anyone who volunteers to cut the road for C++ osdeving :)

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 8:22 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7469
Location: Germany
Pype.Clicker wrote:
I haven't read from LKML that they think writing "a kernel in C++" is not a GoodIdea (tm), just that "Rewriting the Linux Kernel" is not a GoodIdea (tm)...


Believe me, I've read the whole flame-mega-thread on the subject (back when I was evaluating whether to write my kernel in C++, looking for contra arguments)... the FAQ header might not look like it, but there's a general anti-C++ movement in the Linux kernel camp. Not just in regard to Linux. They consider C++ to be unfit for kernel work, and C++ coders to be third grade citizens at best...

Quote:
...and concerning this, i *do* agree with them. Porting a millions line multi-platform (thus multi-compilers) project towards a language that's beginning to have a commonly accepted ABI ... argh.


The point is that nobody ever asked for a "rewrite"! The whole thing started with someone asking if it would be possible to support people who wanted to write kernel modules (i.e., drivers) in C++. As it is, the kernel headers make liberate use of C++ keywords for identifiers, making it virtually impossible to use C++ for any kind of kernel work.

The answer was, "we think you cannot write kernel space C++, do it in C or all you prove is your incompetence. We don't want C++ kernel modules even if you gave us money for it, and all the C++ keywords will remain in there."

I especially like this part in the LKML FAQ:

Quote:
So Erik Mouw did a short back-of-the-envelope calculation to show that searching the kernel sources for possible C++ keywords is a nightmare. Here is his calculation and comments (dates April, 2002):

% find /usr/src/linux-2.4.19-pre3-rmap12h -name "*.[chS]" | xargs cat | wc -l
4078662

So there's over 4 million lines of kernel source. Let's assume 10% is comments, so there's about 3.6 million lines left. Each of those lines has to be checked for C++ keywords. Assume that you can do about 5 seconds per line (very optimistic), work 24 hours per day, and 7 days a week:

Code:
                  5 s     1 hour     1 day     1 week
3600000 lines * ------ * -------- * ---------- * -------- = 29.8 weeks
                 line     3600 s     24 hours     7 days



That's hilarious. There are less than 20 keywords that C++ added to C. If you can do find / cat / wc magic like that shown above, how hard can it be to do a grep on a measly 20 keywords? Or a simple sed to change them to non-keywords in one go?

Xenophobia, that's the word for it...

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re:C++ Kernel Help needed
PostPosted: Thu Aug 12, 2004 9:04 am 
Some day, they'll get into trouble with their concepts ...
On one hand, it is impressive how good Linux works if you look at the concepts of it (at least pre 2.6 kernel series)


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 38 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: antoni, Majestic-12 [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group