Re: writing drivers using C++



Wolfgang Draxinger wrote:
I've read a lot of awnsers on using C++ in the kernel here, some
are going into the right direction, but none got it right.

Let's get this straight: There's only very little runtime support
nedded for C++. Those are:
* calling constructors of globally declared, statically
instanciated class instances
* cast operators
* new and delete operators

I beg to differ, exceptions and RTTI require some code.

All the other things can be perfectly done at compile time.
Polymorphism internally works much like e.g. the fops structure
used to implement inode syscalls.

In C++ each class instance carries a table (or a pointer to that
table) of function pointers to virtual functions.

All classes that use virtual functions, that is. Others don't have this
pointer. Further, the table (the struct fops equivalent) is created on a
per-class base, not per instance. The instance only carries a pointer to
this table.

By inheriting from a class, all the entries in these tables are inherited,
too, but those functions that are overridden replace those
entries in the derived table. Similairily you can create fops
structures that share a subset of syscalls, while descendants
differ.

If one got enough discipline he can do OOP perfectly with a
language like C. Just look at the GObject system. Or the Linux
kernel, which internally uses a lot of OOP concepts, only that
those things are done "manually" using structs and macros.

But if one would like to he could patch the GCC to do C++ stuff
using custom runtime functions, that are also linked into the
kernel. Then add some extra spice into the module init part (to
get static class instances properly initialized) and there you
are: C++ support for kernel programming.

In case you wonder why it's not done in Linux, simple reason:
Sometimes C++ can be a pain in the a*** if you have to get
something done very low level or to utilize a dirty trick.

I'd say that is a bad reason. Firstly, C integrates pretty well with C++, so
you can always use some C code, just like you can step to assembly today.
I'd much rather say that C++ is a very sharp tool, and most programmers
can't use it safely. In particular many C programmers, because they are not
aware of idioms common in C++, but that language lives of idioms and
conventions, otherwise it provides way too much rope to shoot yourself into
the foot.

And it makes dealing with binary objects a whole lot more
complicated, especially if you've to deal with different
compiler versions (there is no strict C++ ABI, yet).

Well, binary objects are a pain in the ass either way, whether it's Linux
that changes its internal APIs or whether it's the C++ compiler matters
little. I'd say that this could be fixed with tools though, but nobody's
writing them. :/

regards

Uli

.