error in use of operator "new" with rtems

Peter Dufault dufault at hda.com
Mon Mar 4 15:59:09 UTC 2013


On Mar 4, 2013, at 07:21 , Sebastian Huber <sebastian.huber at embedded-brains.de> wrote:

> You can look at the linker map file to see which modules are pulled in due to 
> the new operator (GCC option -Wl,-Map,map.txt).
> 

If you aren't using exceptions (and in a small footprint project you probably are not and are compiling to disable them) you can provide your own new and delete operators as wrappers around malloc() and free() and check your return codes as you would from malloc() and free().

As a test I created a C++ file with a do-nothing "fubar()" function and added a call to that "fubar" function in the RTEMS test suite "hello world" example, then compiled and linked (using g++ for the link) and checked the size of my MPC5554 executable:

fubar.cc:
extern "C" void fubar(void);
void fubar(void) {}

[dufault at litho9099 hello]$ powerpc-rtems4.11-size hello.exe 
   text	   data	    bss	    dec	    hex	filename
  86393	   1828	4258104	4346325	 4251d5	hello.exe
[dufault at litho9099 hello]$

Then I had fubar allocate something with new:

fubar.cc:
extern "C" void fubar(void);
extern int *foo;
int *foo;
void fubar(void) { foo = new int; }

powerpc-rtems4.11-size hello.exe 
   text	   data	    bss	    dec	    hex	filename
 198228	   8296	4257936	4464460	 441f4c	hello.exe
[dufault at litho9099 hello]$ 

So yes, the text size (relatively) exploded, which for a small footprint project will be a problem.  I then modified fubar.cc to provide the new and delete operators:

#include <sys/types.h>
extern "C" {
    void fubar(void);
    void *malloc(size_t size);
    void free(void *ptr);
}
void * operator new(size_t s) { return malloc(s); }
void operator delete(void *p) { free(p); }
extern int *foo;
int *foo;
void fubar(void) { foo = new int; }

The size is now back close to where it was:

powerpc-rtems4.11-size hello.exe 
   text	   data	    bss	    dec	    hex	filename
  86425	   1880	4258104	4346409	 425229	hello.exe
[dufault at litho9099 hello]$ 

You'll need to provide array new and deletes as well I suspect.

Peter
-----------------
Peter Dufault
HD Associates, Inc.      Software and System Engineering





More information about the users mailing list