LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2014, 08:07 AM   #76
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742

Quote:
Originally Posted by jpollard View Post
I guess you haven't work in embedded.
you guess wrong.
C++ is widely used in the embedded wold, on application level

Quote:
Originally Posted by jpollard View Post
There is absolutely no support available to support it.
I do not understand this sentence, maybe because I am not a native english speaker?

That unix OS domain is C is ok, good and I do not advocate to change this, just look some posts up

but as already mentioned, there are vendors that do not even ship a C compiler and they require you to use a C++ compiler for operating system / driver code.
That C++ is not used on OS level is not true
BeOS, now Haiku, is an other example.
so a lot of things said, not just here, about C++ are just somewhere between wrong and stupid and show a huge level of missing knowledge.
 
Old 07-17-2014, 07:21 AM   #77
lmenten
LQ Newbie
 
Registered: Apr 2007
Posts: 15

Rep: Reputation: 0
Wink No kernel patches are necessary to use C++ for kernel modules

I have been creating very sophisticated Linux networking modules for many years and continue to develop them on the very latest Linux kernels. It is not necessary to patch the kernel to do this if you are willing to give up support for exceptions. Don't worry about the rude comments you might receive from some of the Linux fanboys. There is a lot of childishness in the community. I wouldn't patch the kernel. If you don't patch then supporting multiple Linux releases becomes easy. The kernel API is not particularly stable. Or particularly well documented. That is not the Linux way. So you will have to reverse engineer occasionally to figure out how the modified kernel APIs should be used. You will also get "kernel tainted" messages if your driver is not GNU licensed. The non-open-source nvidia drivers gave the same message. But were much finer quality than the open source versions.

My modules are also used, without change, in various embedded environments, including VxWorks. All of those are friendly to C++ and provide full support. And the embedded OSes do not report that the kernel is "tainted." ; )

There are a couple of global symbols used by the C++ runtime, and you will need to write functions for new and delete that call the kernel APIs for these functions. Trivial stuff. A few dozen lines in all.
 
Old 07-17-2014, 11:48 AM   #78
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
It is only "tainted" by being a proprietary module that prevent developers from debugging the kernel. If it were GPL then there would be no problem. But it is a proprietary module that is also a derivative of kernel sources, thus the kernel is "tainted".

The other "OSes" are not attempting to be a general purpose kernel that can be configured for any purpose.

At one time, Torvalds did try to use C++ for the kernel. There were too many places the language got in the way making it a most inefficient/bloated result. Not that it couldn't be done - it just wasn't efficient (https://lkml.org/lkml/2004/1/20/20).

And if I remember the kernel memory allocations, they are always in pages... so anything not a multiple of a page always wastes memory and causes memory fragmentation... which make using "new" and "delete" a poor choice.

Last edited by jpollard; 07-17-2014 at 11:54 AM.
 
Old 07-17-2014, 09:51 PM   #79
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by a4z View Post
you guess wrong.
C++ is widely used in the embedded wold, on application level
Applications are in a different arena. They are not (or should not be) part of the kernel. This was just about C++ kernel modules.
Quote:

I do not understand this sentence, maybe because I am not a native english speaker?
I was referring to the non-support of C++. None of the supporting functions for C++ are there. No exceptions, no IO, no memory allocation that C++ can use.
Quote:

That unix OS domain is C is ok, good and I do not advocate to change this, just look some posts up

but as already mentioned, there are vendors that do not even ship a C compiler and they require you to use a C++ compiler for operating system / driver code.
That C++ is not used on OS level is not true
BeOS, now Haiku, is an other example.
so a lot of things said, not just here, about C++ are just somewhere between wrong and stupid and show a huge level of missing knowledge.
different kernel, different use.

Linux is a general kernel for many uses - from the smallest controllers to the fastest mainframes. That requires the kernel code to be as portable as possible, and not make assumptions about what hardware capabilities exist. It will make use of them when they are available, but the software capabilities must be able to function even when the hardware may be "deficient" (for various definitions of deficient). As such, it is written to support all of them - and that requires the visibility of everything possible. Which means hidden memory allocations/deallocations is a big source of inefficiency.

In most platforms, the MMU can only handle a limited range of page sizes (most common used is 4K) - and anything other than page size wastes memory (both in unused space, and in excess page table management).

Last edited by jpollard; 07-17-2014 at 09:52 PM.
 
Old 07-17-2014, 11:33 PM   #80
Michael Hewitt
LQ Newbie
 
Registered: Jul 2014
Distribution: CentOS
Posts: 4

Rep: Reputation: Disabled
C++ Exceptions Do Not Involve The OS

As per the following stack overflow article, C++ exceptions do not involve signals or the operating system. They simply run destructors, unwind the stack, and transfer control back to the nearest catch block. Thus, they should be suitable for kernel development, and if they had the blessing of the kernel would be a great way to propagate rich error information back to user applications. They could still be used without kernel involvement, but would need to be caught at module boundaries and translated into error codes. We do exactly this in our embedded C++ code.

http://stackoverflow.com/questions/4...me-implemented
 
Old 07-17-2014, 11:47 PM   #81
Michael Hewitt
LQ Newbie
 
Registered: Jul 2014
Distribution: CentOS
Posts: 4

Rep: Reputation: Disabled
C++ Templates Produce Tight & Fast Assembly

Having looked at the assembly language produced by templates for embedded applications, I can say with certainty that they produce very fast, very tight code. The C++ template-based queues I authored for an embedded modem application ran circles around the equivalent built-in VxWorks containers that were authored in C.

At the Design West Embedded Systems Conference two years ago, expert C++ presenters were explaining why C++ templates can in many cases produce faster code than pure C, due to the compiler knowing more and being able to apply optimizations that a C compiler cannot safely apply. Having used C++ templates extensively in embedded applications, I can say from experience this is true.

C++ templates enable very flexible, reusable source code, while producing incredibly fast, lean & mean assembly. Kernel code would be faster and safer if templates were used instead of funky pointer manipulation based on member offsets within C structs for containers such as linked lists.
 
Old 07-18-2014, 01:05 AM   #82
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
jpollard
what is so hard to understand on following facts

if you can do it with C you can do the same with C++
if you do not use certain C++ features you will not pay for it
there is nothing you can do with C that can not be done in the same way or better in C++
a C++ compiler will produce the same output for C code like a C compiler
100% compatible, that is a key feature of C++
even if you do not use a lot of features you have things like RAII and destructor guarantee or some use of templates (instead of macros)
what makes you programs more secure and efficient


not pay for what you do not use is a C++ fundamental.
and there is nothing hidden in C++, and you have full control for everything

technically, there are reasons for not use RTTI
or there might be reasons for not use exceptions,
or have special memory allocation/dealocation
this is all possible,

the same result or better (if used correct)
so there is technically no reason that C is required and C++ does not work.
there is not valid as reason why C++ can not be used in general for writing a kernel.
there is no valid reason that C++ would not produce the same or a better result than you could produce with C.
 
Old 07-18-2014, 04:36 AM   #83
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Very true, it is possible to change the file extension from '.c' to '.cc' and state that we have a program written in C++. Why would we do that though?
 
Old 07-18-2014, 05:53 AM   #84
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
And what part of C++ that isn't supported doesn't work and hides the implementation from optimization do you miss?

Not using a feature is not the same as having the feature not available for unintended use. That allows for slopply coding.

Destructors (and constructors) explicitly cause kernel memory fragmentation. Hiding when and where they occur cause thrashing when not desired.

Exception handling is supposed to be an exception - it doesn't happen as a general course - doing that causes tons of cache misses, and causes a rather big performance penalty when they occur. And because the C++ compiler is creating them, you have no control over it.

If you avoid using the features of C++, then why use C++ at all?

There is nothing wrong with C++ for application use. Use it where it is suitable. But it is not that good at low level hardware control interaction where memory is limited, or has other hardware restrictions.

It has been tried. Some people do it anyway, but will not recognize the pitfalls being introduced.

I have yet to see a general purpose kernel written in C++. Have kernels been done in C++? sure. Are they in general use? nope. None of them.

Last edited by jpollard; 07-18-2014 at 06:02 AM.
 
Old 07-18-2014, 07:17 AM   #85
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
ok, I see now that your opinion is immune again facts.

Quote:
Originally Posted by jpollard View Post
Destructors (and constructors) explicitly cause kernel memory fragmentation. Hiding when and where they occur cause thrashing when not desired.
do you just want to troll around?

since the rest of your post has the same quality as the quoted part
I have to assume that you just want to argue for the sake of arguing.

I am sorry for trying to have a serious discussion with you
 
Old 07-18-2014, 07:23 AM   #86
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by NevemTeve View Post
Very true, it is possible to change the file extension from '.c' to '.cc' and state that we have a program written in C++. Why would we do that though?
I do not want to do it.
I just state the opinion that if you do it
you have noting to lose because of the same resul.
and you can win if you use C++ features to improve where this is possible
so:
in the wort case you end up with the same result
in the best case with a better result

sounds like a good deal
 
Old 07-18-2014, 09:26 AM   #87
lmenten
LQ Newbie
 
Registered: Apr 2007
Posts: 15

Rep: Reputation: 0
Beg to differ

Well-written C++ code is at least as compact and efficient as well-written C. Those who can do. Those who can't, write in C.

Overriding new and delete allows optimizing the memory allocation strategy for each type or category of object for the platform upon which the code is used. Clean, efficient, and readable.

So far as that antique Torvalds experiment goes, repeatedly quoting old and bad information does not make it either informative or correct.

The Linux community reaps what it sows with the "tainted" machinations and related licensing nonsense. The best drivers for high performance graphics cards will never be offered for Linux platforms.
 
Old 07-18-2014, 09:58 AM   #88
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by lmenten View Post
Well-written C++ code is at least as compact and efficient as well-written C. Those who can do. Those who can't, write in C.
And achieve the same as the well-written C++ code with less effort.
Quote:

Overriding new and delete allows optimizing the memory allocation strategy for each type or category of object for the platform upon which the code is used. Clean, efficient, and readable.
And slow. You now have two levels of memory allocations to fight with.
Quote:
So far as that antique Torvalds experiment goes, repeatedly quoting old and bad information does not make it either informative or correct.
It also doesn't make it wrong. What was "bad information"?
Quote:
The Linux community reaps what it sows with the "tainted" machinations and related licensing nonsense. The best drivers for high performance graphics cards will never be offered for Linux platforms.
That is totally up to the vendors that write drivers. Since they want to perform poorly, that is their choice.
 
Old 07-18-2014, 11:36 AM   #89
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by jpollard View Post
Actually not. You don't get the same error detection.

The problem is that a kernel has to operate at the lowest level... And that includes skipping the C++ tendency to mangle function names.

Makes it very hard to debug.
That doesn't make sense to me. How does C++ "mangle" function names? In any case, I would have thought encapsulation would help avoid a lot of the bugs that would otherwise occur.

There is nothing to stop a kernel program from operating at the lowest level. At the very worst, you might need to modify the C header files:
#ifdef __cplusplus
extern "C" {
#endif

etc
 
Old 07-18-2014, 03:01 PM   #90
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by psionl0 View Post
That doesn't make sense to me. How does C++ "mangle" function names? In any case, I would have thought encapsulation would help avoid a lot of the bugs that would otherwise occur.
http://en.wikipedia.org/wiki/Name_mangling

In compiler construction, name mangling (also called name decoration) is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.

Quote:
There is nothing to stop a kernel program from operating at the lowest level. At the very worst, you might need to modify the C header files:
#ifdef __cplusplus
extern "C" {
#endif

etc
That is to defeat the name mangling that C++ does.

There is no point to using C++ in the kernel if you have to avoid using C++ features.
 
  


Reply

Tags
arm, c++, compile, fine, kernel, linux, modules



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux kernel development vwal_13 Programming 3 03-09-2005 02:27 AM
Linux Device Driver Development aslv Linux - Certification 0 09-27-2004 08:42 AM
Linux Kernel Development impact on Slackware carboncopy Slackware 5 07-28-2004 03:43 PM
Touch screen driver development for Linux peso Linux - Software 1 02-02-2004 12:15 PM
Lexmark Released A Linux Driver Development Kit FearPasion710 Linux - Software 0 09-09-2003 06:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration