 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
rajanikanth@gmail.com Guest
|
Posted: Wed Jan 19, 2005 11:23 pm Post subject: STL and Software Drivers |
|
|
hi!
Could somebody tell me if STL(Standard Template Library) can be used
for writing software drivers. Most people told me that it wont be as
efficient as C language.
Thanks,
Raj
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Jan 20, 2005 6:13 pm Post subject: Re: STL and Software Drivers |
|
|
[email]rajanikanth (AT) gmail (DOT) com[/email] wrote:
| Quote: | Could somebody tell me if STL (Standard Template Library) can
be used for writing software drivers.
|
I suspect that it depends on the driver, and the system. To
tell the truth, I can't see much use for most of the STL in
drivers; a basic driver just doesn't do that much.
| Quote: | Most people told me that it won't be as efficient as C
language.
|
In what regard?
There may be some restrictions, depending on the system; I can
imagine systems in which operator new can't be used in an
interrupt service routine, for example, or even that it cannot
be used in kernel level software (like a driver) at all. In
such cases, of course, any of the standard containers are out.
On the other hand, I would imagine that most of the time, the
standard algorithms are at least as fast as their C
counterparts, if not faster.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
msalters Guest
|
Posted: Thu Jan 20, 2005 6:15 pm Post subject: Re: STL and Software Drivers |
|
|
[email]rajanikanth (AT) gmail (DOT) com[/email] wrote:
| Quote: | hi!
Could somebody tell me if STL(Standard Template Library) can be used
for writing software drivers. Most people told me that it wont be as
efficient as C language.
|
Sure. The STL is a Template library, which means that a lot of
overhead is kept to compile time. E.g. std::sort is often faster
then C's qsort, because the sort order can be detected at compile
time. In C, qsort only gets a function pointer, which is much
slower on typical CPUs. A speedup of a factor 6 has been reported
going from qsort to std::sort.
Another good example is std::string. Modern std::string
implementations are a lot better than malloc(), for a number
of reasons: better locality than a char*, no alignment overhead
( malloc() usually needs to return 4 or 8byte aligned memory ),
no heap access for small strings.
So, C++ can be more efficient than C. In fact, C is almost a
subset of C++, so it's very likely it will be more efficient
if used by a good C++ programmer. But just like C, C++ is not
a very forgiving language - especially if efficiency matters.
( a C programmer who changes char*s into std::string*s instead
of std::strings will see no speedup. )
Regards,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Darren Bane Guest
|
Posted: Thu Jan 20, 2005 6:51 pm Post subject: Re: STL and Software Drivers |
|
|
This question is almost off-topic. The answer totally depends on your
operating system.
For Mac OS X, drivers _must_ be written in C++, but I think it's the
Embedded C++ subset, which doesn't have the STL.
For embedded systems, where most of my experience is (VRTX & VXWorks),
I'm not sure that I would recommend C++ at all for code which is
coupled tightly with the kernel. A C++ runtime assumes the existence
of some facilities, like a memory manager, which may not be available
to device drivers, and certainly won't be at system start-up time.
I'd just copy what other people do on your platform. For instance,
most UNIX drivers are written in C--you can read them in the source
tree. In contrast, I think the idea with microkernel-based OSs is that
your drivers are application programs, for which C++ is a fine
(business, not technical) choice.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Thu Jan 20, 2005 8:06 pm Post subject: Re: STL and Software Drivers |
|
|
[email]rajanikanth (AT) gmail (DOT) com[/email] wrote:
| Quote: | Could somebody tell me if STL(Standard Template Library) can be used
for writing software drivers.
|
Yes of course. If you can use C++ at all, and the compiler you're
using supports templates, you can use STL.
| Quote: | Most people told me that it wont be as
efficient as C language.
|
It all depends on what "efficient" means, and how you measure it.
Embedded programmers often complain that any use of templates at
all induces "code bloat." This is a controversial opinion, partly
fueled by early compilers that didn't handle templates very well,
and bad use of templates that failed to factor out the code that
didn't depend on the template arguments. I don't want to trigger
1000 posts on whether templates cause "bloat" or not, so I'll
point out that for much of the STL, this "bloat" is irrelevant.
Most of the STL is optimized for speed, and one of the primary
ways this is accomplished is through the use of inline functions.
Sometimes inline functions actually produce LESS code than a
call to an out-of-line function, but we all know that isn't the
typical case. In many cases, expanding template code inline for
speed is exactly what you want for drivers. On the other hand,
depending on what kind of drivers you're writing and the OS
you're writing for, you could have severe memory limitations --
the inline aspect of the STL could actually work against you.
Also, nothing in the STL comes for free. The containers, for
example, are very fast compared to alternatives I've seen, but
additional features come at the cost of some overhead. As an
obvious example, you'll find that vector is extremely fast, but
there is some overhead to keep track of some things such as the
current size and capacity, and the ability to grow automatically.
Suppose your driver needs to access 27 Foo objects -- always
exactly 27. In user code, I might recommend that you use a vector
for this, but in driver code I would stick to a raw "C-style"
array. That way the size can be a constant and you won't include
any code that checks the current size before it does an access.
I guess the bigger answer is that it depends on what you need
to do in the driver. If the STL fills those needs, use it! If
it's only close, you need to figure out if you can do better
on your own.
HTH. Good luck.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Thu Jan 20, 2005 8:07 pm Post subject: Re: STL and Software Drivers |
|
|
On 19 Jan 2005 18:23:26 -0500, <rajanikanth (AT) gmail (DOT) com> wrote:
| Quote: | Could somebody tell me if STL(Standard Template Library) can be used
for writing software drivers. Most people told me that it wont be as
efficient as C language.
|
It depends on how you use STL. STL containers and algorithms on their own
do not impose much of performance waste.
There are two main performance killers to consider:
* memory allocation
* data copy
The first one can be avoided by not using STL containers or using them
with a custom allocator that gets memory from a preallocated pool. Except
containers, there are no memory allocations in STL (please note, that
streams are not a part of STL, rather they are part of the standard
C++ library).
As for the second, you've got no control over how many copies of your
objects are made inside containers and algorithms, although it's resonably
to assume that the authors of an STL implementation put some effort to
minimize them. But you do have control over your objects' implementation
and you can make their copy operation cheap, or use pointers instead of
objects just like you do it in C.
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Sat Jan 22, 2005 5:02 am Post subject: Re: STL and Software Drivers |
|
|
Allan W wrote:
| Quote: | rajanikanth (AT) gmail (DOT) com wrote:
Could somebody tell me if STL(Standard Template Library) can
be used for writing software drivers.
Yes of course. If you can use C++ at all, and the compiler
you're using supports templates, you can use STL.
|
Not at all. If you're writing device drivers, you are very
definitly in a freestanding environment, and according to the
C++ standard (§17.4.1.3), the presence of the STL is optional.
Operator new and exceptions aren't. In practice, however, I
imagine that on most systems, they won't work, or won't work
correctly, in an interrupt handler. (An exception certainly
shouldn't be allowed to propagate out of the interrupt handler.
And if your library uses a system request to get the memory it
hands out, I'd be very surprised that new works correctly in a
device driver, regardless of what the standard says.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
wkaras@yahoo.com Guest
|
Posted: Sat Jan 22, 2005 11:26 am Post subject: Re: STL and Software Drivers |
|
|
[email]rajanikanth (AT) gmail (DOT) com[/email] wrote:
| Quote: | hi!
Could somebody tell me if STL(Standard Template Library) can be used
for writing software drivers. Most people told me that it wont be as
efficient as C language.
Thanks,
Raj
|
If you need an associative container, I have written an AVL tree
template that allows/requires the instantiation to contruct,
destroy, and locate the nodes in memory:
http://www.geocities.com/wkaras/gen_cpp/avl_tree.html
I have also written equivalent templates for hash tables,
doubly-linked lists, etc. (Did this on company time, so I can't
make these public domain.)
This AVL tree template does not throw exceptions. I don't
believe there's anything inherent about exceptions that would
make it impossible to use them in a driver. But the run-time code
to support exceptions in a particular compiler implementation
might allocate memory from the heap, or do something else you
potentially can't do in a driver. So far, I haven't seen a
C++ compiler that didn't come with an STL version that doesn't
throw exceptions, so the exception issue is not a show stopper.
Probably, you can use template in the STL that doesn't use
the 'new' operator.
If you're going to write kernel code in any language besides
assembler, I'd suggest you not use any code unless you feel you
could hand-translate it into assembler if you had to. The
information you need to have this level of understanding is
harder to come by for C++ than for C.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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 vote in polls in this forum
|
|