public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* C++ and the kernel
@ 2002-04-09 10:16 T. A.
  2002-04-09  9:54 ` Martin Dalecki
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: T. A. @ 2002-04-09 10:16 UTC (permalink / raw)
  To: Linux Kernel Mailing List

Hi all,

    I am in the initial stages of writing some C++ wrapper classes for the
kernel.  So far its been an interesting process, mainly due to the use of
some C++ keywords in the kernel header files.  Mostly gratuitous at that as
most of the C++ keyword uses I've encountered so far are the use of the
keyword "new" as variable names in some inlined functions.  Well I've fixed
up the header files (I've used so far) and have been able to successfully
overload the new and delete keywords thus allowing me to properly construct
and deconstruct C++ objects within a kernel module.  Well I've encountered a
couple of issues I hope someone can help me here.

    So far my overloading of "new" works if I compile the module without
exceptions (-fno-exceptions).  This is fine for myself as I prefer checking
for a NULL on a memory allocation error over handling an exception, plus the
resulting code appears to be much more efficient without exceptions by a
large order of magnitude.  However I would like my C++ kernel support to
include exception support if possible so those whom may want to use it,
besides which it may yet prove itself useful.  However allowing exceptions
in (if used apparently, via so far my overloading of "new") appears to put
an undefined reference to throw and (I think) terminate into the resulting
object file.  Does someone know how I can resolve this?

    I would like to add the ability for the wrapper classes' users to use
static member functions as the module initiation and cleanup functions.  For
example:

class my_module
{
    public:
        static int load();
        static void unload();
};

    And use my_module::load() as the init_module function and
my_module::unload() as the cleanup_module function.  The provided macros
module_init and module_exit should do this for me however I've encountered a
limitation in how gcc's "alias" feature is used.  Apparently for C++ one
must pass the mangled name of the function in question.  Is there a gcc
macro or function of some kind to do something like this:

int init_module() __attribute__((alias(mangle_name("load__9my_module"))));
int cleanup_module()
__attribute__((alias(mangle_name("unload__9my_module"))));

    I need a function or macro like mangle_name above.

    Another issue that I currently have is that I haven't been able to
figure out a way to get the module to properly initiate global objects.
Like so:

my_module mod1;

    If I put this in the source file as a global function and load the
module, I've found that this module's constructor doesn't get called.  I
figured out why and gcc's documentation backed me up.  However I can't seam
to find any way to tell gcc to initiate the object file's global objects
from within the init_module function.  I know a workaround would be to have
a pointer instead and allocate the object in init_module however I think its
best to avoid pointers whenever possible to reduce the chance of errors.
Anyone know how I can get gcc to insert the global initiation code from
within init_module?


On a side note:

    So far C++'s use of the kernel headers has found a couple of areas in
which possible bugs exists.  In one header file the typedef ssize_t is used
despite its definition not appearing in the source file or any of the
included header files.  I've also encountered negative numbers being
assigned to unsigned numbers without a cast.  And I've found completely
unnecessary use of the C++ keyword "new" as variable names in some inlined
functions.

    Would patches be welcomed for one or more of these issues?

    Thanks.

^ permalink raw reply	[flat|nested] 20+ messages in thread
* Re: C++ and the kernel
@ 2002-04-09 16:45 Sau Dan Lee
  0 siblings, 0 replies; 20+ messages in thread
From: Sau Dan Lee @ 2002-04-09 16:45 UTC (permalink / raw)
  To: Chris Friesen; +Cc: linux-kernel

>>>>> "Chris" == Chris Friesen <cfriesen@nortelnetworks.com> writes:

    >> It is quite unlikely that a C++ compiler will make more
    >> efficient code than a C compiler. In fact, the code generator
    >> will likely be the same. The C++ compiler will end up
    >> generating some preamble code as part of the function-calling
    >> mechanism, that is not necessary in C. This means that it will
    >> generate a bit more code.

    Chris> C++ has tigher constraints on code than C.  This can allow
    Chris> a compiler to generate better code because it has more
    Chris> knowledge about what is going on.

There are  also many  GNU extensions  to C that  makes it  possible to
generate  better  code,  e.g.    marking  certain  functions  as  pure
functions.



    Chris> Your example is needlessly complex, and I'm sure you know
    Chris> this.  A more realistic comparison would be:

    Chris> cout << "Hello World!\n";

This has demonstrated that you don't know C++ _very_ well.  Instead of
the trailing "\n", a C++ programemr ought to write:

        cout << "Hello World!" << endl;

You seem  to *understand* C++  even worse than Richard.   Even Richard
used "endl"  in his unnecessarily  complex example.  And you're  not a
better programmer, either,  because you failed to distill  out the EOL
concept and represent it in an abstract, OS/platform-independent way.


    Chris> Now I don't for a moment think that we should go and
    Chris> convert everything to = C++.=20 But I do think that certain
    Chris> features of the language can be useful, and tha= t there
    Chris> are cases when OO style programming makes the code easier
    Chris> to read and understand.

I don't find the current Linux fs code is too difficult to understand,
although it is doing OO in a non-OO language.  Rather, I think this is
easier  to maintain,  because you  don't have  to go  through  all the
burdens of writing the C++ code.  (It's easy to get a working C++ code
work.  But  making it efficient  (e.g.  sprinking "const"  to wherever
possible  so  that it  becomes  a potential  for  the  compiler to  do
optimization)  and   *flawless*  (e.g.   multithread-safe,  reentrant,
SMP-safe,   **side-effect-less**,  privatizing   all   dangerous  copy
constructors as  well as  assignment operators, no  memory leakage...)
would  be very  devoting.  The  time could  better be  spent on  the C
code.)

Unless  the  FS  code  gets  the complexity  comparable  to  Xt  (tall
inheritance  trees), I don't  think the  overhead of  doing it  in C++
worths.  (Xt is still bearable when used as a user.  But if you try to
extend it (by writing a few custom widget classes) or maintain code in
it, you'd wish it  were in C++.)  How deep would the  FS code grow to?
If it  is shallow, then  IMO, don't do  it in C++; you'll  be spending
more time for the cosmetics than really getting advantage out of it.


-- 
Sau Dan LEE                     李守敦(Big5)                    ~{@nJX6X~}(HZ) 

E-mail: danlee@informatik.uni-freiburg.de
Home page: http://www.informatik.uni-freiburg.de/~danlee


^ permalink raw reply	[flat|nested] 20+ messages in thread
* Re: C++ and the kernel
@ 2002-04-09 16:51 Sau Dan Lee
  2002-04-09 16:58 ` Richard B. Johnson
  0 siblings, 1 reply; 20+ messages in thread
From: Sau Dan Lee @ 2002-04-09 16:51 UTC (permalink / raw)
  To: Richard B. Johnson; +Cc: linux-kernel

>>>>> "Richard" == Richard B Johnson <root@chaos.analogic.com> writes:

    Richard> On Tue, 9 Apr 2002, Dr. David Alan Gilbert wrote:
    >> * Richard B. Johnson (root@chaos.analogic.com) wrote:
    >> I would like to rewrite the kernel in FORTRAN because this was 
    >> one of the first languages I learned.  Seriously, the
    >> kernel MUST be written in a procedural language.  It is the
    >> mechanism by which something is accomplished that defines an
    >> operating system kernel.  C++ is an object-oriented
    >> language, in fact the opposite of a procedural language. It
    >> is not suitable.  Bollox!  There are many places in the
    >> kernel that are actually very OO - look at filesystems for
    >> example. The super_operations sturcture is in effect a virtual
    >> function table.

    Richard> The file operations structure(s) are structures. They are
    Richard> not object- oriented in any way, and they are certainly
    Richard> not virtual.

The  term  "virtual"  has  a  very  specific  in  OO,  esp.   in  C++.
Unfortunately, this word isn't a  very faithful description of what it
means.  Java uses the keyword "abstract" for what is "virtual" in C++.
This is much more appropriate.

And "virtual function table", "vptr", "vtable" are also specific terms
in OO which  refer to implementation details of the  run-time of an OO
language.   To  you,  this  shouldn't  be anything  new.   A  "virtual
function table" is just an  array of pointers to functions.  It serves
essentially the same purpose  as the super_operations structure in the
Linux  kernel.  Instead  of having  to building  the table  (in source
code, not run-time) yourself, the  compiler of C++ and any OO language
would do  it for you  automatically, thereby saving typing  effort and
avoiding trivial typos.



-- 
Sau Dan LEE                     李守敦(Big5)                    ~{@nJX6X~}(HZ) 

E-mail: danlee@informatik.uni-freiburg.de
Home page: http://www.informatik.uni-freiburg.de/~danlee


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2002-04-10  1:52 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-04-09 10:16 C++ and the kernel T. A.
2002-04-09  9:54 ` Martin Dalecki
2002-04-09 12:10   ` Richard B. Johnson
2002-04-09 12:26     ` Dr. David Alan Gilbert
2002-04-09 13:28       ` Richard B. Johnson
2002-04-09 14:00         ` Chris Friesen
2002-04-09 13:55           ` Sean Neakums
2002-04-09 14:00             ` Alexander Viro
2002-04-09 14:02         ` Michael Clark
2002-04-09 14:30         ` Rik van Riel
2002-04-10  1:52         ` H. Peter Anvin
2002-04-09 17:17     ` T. A.
2002-04-09 21:51       ` J. Dow
2002-04-09 23:11         ` Rui Sousa
2002-04-09 17:27   ` T. A.
2002-04-09 11:29 ` Erik Mouw
2002-04-09 16:21 ` Kurt Wall
  -- strict thread matches above, loose matches on Subject: below --
2002-04-09 16:45 Sau Dan Lee
2002-04-09 16:51 Sau Dan Lee
2002-04-09 16:58 ` Richard B. Johnson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox