All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bart Samwel <bart@samwel.tk>
To: Linus Torvalds <torvalds@osdl.org>
Cc: Robin Rosenberg <robin.rosenberg.lists@dewire.com>,
	Linux kernel <linux-kernel@vger.kernel.org>
Subject: Re: Compiling C++ kernel module + Makefile
Date: Tue, 20 Jan 2004 11:46:35 +0100	[thread overview]
Message-ID: <400D070B.2070407@samwel.tk> (raw)
In-Reply-To: <Pine.LNX.4.58.0401192241080.2311@home.osdl.org>

Linus Torvalds wrote:
> 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.

I second that. The basic reason the C++ exception handling mechanism is 
broken is because of what happens when an exception is thrown from a 
destructor, during a stack unwind caused by an exception. There are 
paths of exception/destructor combinations that you just cannot get out 
of correctly. This is perfectly OK for most userland apps, but for 
kernels it's definitely not OK.

>  - any compiler or language that likes to hide things like memory
>    allocations behind your back just isn't a good choice for a kernel.

True, when you're doing code that requires locking etc. you don't want a 
destructor coming in and deallocating something that you haven't locked. 
I don't see when C++ can hide memory *allocations* though, any more than 
C can hide them behind trees of function calls.

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

True, C++ _is_ full of crap -- that's what C++ lovers refer to as 
"power". For work, I program in C++, and I love it because of the power 
of expression. I hate it because of the amount of rope it gives me to 
hang myself with. And because most C++ compilers are _so_ immature. 
Still, OO can be written in C (cfront does it :) ) but it's not always 
convenient. Especially when you're doing inheritance-like things, your C 
code will be sprinkled with casts (neatly hidden in macros, probably), 
and no C compiler will typecheck that. More static checking is good, I'd 
say, so there might be reasons to do C++ if you might otherwise get 
bogged down in a sea of casts.

> In general, I'd say that anybody who designs his kernel modules for C++ is 
> either 
>  (a) looking for problems

Mostly true, I guess. But sometimes, if you stick to a wisely chosen 
subset, it can work out OK. For instance, exceptions should not be used. 
Destructors become much less of a problem when you rule out exceptions, 
and especially when combined it with lock objects for the locks. And it 
is very well possible to write C++ that is low on allocations, like 
kernel code should be. I think writing kernel code in C is also looking 
for problems, unless you know exactly what you're doing -- in that 
respect, it's pretty much the same.

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

This argument can, of course, be made for any language. In that case, 
the kernel developers' community can be said to be made up from C bigots 
that can't see that what they are writing is really just asm anyway. :)

Anyway, I can definitely see your point if you think of people who use 
C++ as an "improved C", without much use of OO and templates etc. There 
are lots of people out there who do that, and transferring that mode of 
usage to the kernel is simply not a good idea. C++ should only be the 
language of choice when you need the things where it shines: inheritance 
hierarchies, templates, object finalization.


My opinion is that if people want to use C++ in a module, they must do so:
1. Only if they _really_ need all that crap.
2. Only if they _really_ know what they're doing.
3. Only if they use a proper subset that cooperates correctly with the 
kernel.
4. Only if it's worth the extra effort that's needed to use C++ in a 
kernel module.

Basically, I think that if you know the problems, and know them well, 
you don't need to go looking for them anymore, you might be able to just 
walk around them. Let's get back to the situation that started this 
discussion: the Click people have shown that they can walk around the 
problems (point 2), their code is true C++ (they seem to need all that 
crap, point 1) and it works fine inside it's own kernel module (point 
3). Who are we to judge them, if it works for them? They don't really 
bother us with it -- we only bother ourselves with it *a lot* as soon as 
their existence is brought to our attention. :) And I think the only 
ones who can tell if it was worth the extra effort (point 4) are they 
themselves.

-- Bart

  parent reply	other threads:[~2004-01-20 10:46 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-16 21:09 Compiling C++ kernel module + Makefile Ashish sddf
2004-01-16 22:07 ` Richard B. Johnson
2004-01-17 12:59   ` Bart Samwel
2004-01-19 13:46     ` Richard B. Johnson
2004-01-19 17:40       ` Bart Samwel
2004-01-19 18:39         ` Richard B. Johnson
2004-01-19 20:02           ` Bart Samwel
2004-01-19 20:37             ` Richard B. Johnson
2004-01-19 21:24               ` Bart Samwel
2004-01-20 15:20                 ` Richard B. Johnson
2004-01-20 17:34                   ` Zan Lynx
2004-01-20 18:10                     ` Richard B. Johnson
2004-01-20 13:38                       ` Thomas Lahoda
2004-01-21  2:24                       ` Michael Clark
2004-01-20 18:16                     ` Chris Friesen
2004-01-21 17:01                 ` Giuliano Pochini
2004-01-21 17:16                   ` Bart Samwel
2004-01-20  0:59               ` Robin Rosenberg
2004-01-20  6:46                 ` Linus Torvalds
2004-01-20  7:32                   ` Robin Rosenberg
2004-01-20 10:46                   ` Bart Samwel [this message]
2004-01-20  5:29         ` Valdis.Kletnieks
2004-01-20  9:48           ` Bart Samwel
2004-01-16 22:40 ` Sam Ravnborg
2004-01-19 13:21 Petr Vandrovec

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=400D070B.2070407@samwel.tk \
    --to=bart@samwel.tk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robin.rosenberg.lists@dewire.com \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.