|
From: | Ned Konz |
Subject: | Re: [avr-gcc-list] More on C macros vs. C++ templates |
Date: | Fri, 28 Apr 2006 09:27:34 -0700 |
On Apr 28, 2006, at 7:06 AM, Dave Hylands wrote (in response to a message from "dimax un"):
WOW. I will definitely study your message. It will take time to all of us. But my scepticism was not about templates but about writing embedded code in C++ instead of C/Assembler/InlineAssembler. Interesting what would you getif you would write one more example in plain good C.I pasted some circular buffer code that I did. In C & C++, I used macros in C, and in C++ I used a template. I did a bunch of analysis, and the C++ is very close in size to the C one. The biggest size differences are all related to how unit8_t's get promoted to ints. The C++ version is also missing a bit of functionality but it would be easy to add. I think at the time I didn't fully appreciate what was affecting the size and could probably get the C++ one to be as small as the C one now that I understand whats going on.
OK, here's a modified version of yours that generates the same size object file using GCC 4.1.0 (actually, the C++ version is 2 bytes larger because of a bad register allocation choice, but who's counting?).
If you uncomment the commented-out constructor in the C++ version, it becomes 36 bytes larger (108 vs. 72 bytes) because of the global constructor/destructor mechanism. In this case, of course, the constructor is not necessary for a static CBUF object, as the indices need to start at 0 anyway.
Here's how it looks when you uncomment the constructor and run avr-nm -C --size-sort --print-size testc*.elf testc.elf: 00800060 00000042 B myQ 0000008e 00000046 T main testcpp.elf: 000000ba 0000000e t global constructors keyed to myQ 000000a6 00000014 t __static_initialization_and_destruction_0(int, int) 00800060 00000042 B myQ 000000c8 00000048 T main So you see that main() is only 2 bytes larger in the C++ version.I also made the C interface parameterizable for IndexType and EntryType, added the missing functions to the C++ version (though I'm not sure that they should be public at all), and added some comments.
-- Ned Konz MetaMagix embedded consulting address@hidden
templateDemo.tar.gz
Description: GNU Zip compressed data
[Prev in Thread] | Current Thread | [Next in Thread] |