[SOLVED] [libmaple F4] Very large sketch size when using malloc()

Limited support for STM32F4 Discovery, Nucleo and custom F4 boards
User avatar
Rick Kimball
Posts: 990
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by Rick Kimball » Fri Aug 18, 2017 4:05 pm

victor_pv wrote:
Fri Aug 18, 2017 2:18 pm
When checking the .map file I see new is being pulled from stdc++, so that's what's pulling that library. Then did a search in the repo for new, since I remember there was an implementation in the core, and found out it was only in the F1 repo, Roger added it a while back:
https://github.com/rogerclarkmelbourne/ ... 2a824d80b7
If you are going to be adding things .... this might be a nice addition to new.cpp:

Code: Select all

inline void * operator new( size_t sz, void * here )
{
  return here;
}

inline void * operator new[]( size_t sz, void * here )
{
  return here;
}
This will let you do "placement" new.

unsigned char memory_buffer[sizeof(dumbclass)];
dumbclass *dumb_ptr;
....
main() {
...
dumb_ptr = new(memory_buffer) dumbclass;
...
}
Last edited by Rick Kimball on Fri Aug 18, 2017 8:05 pm, edited 1 time in total.
Reason: Fixed untested code per comments
-rick

victor_pv
Posts: 1605
Joined: Mon Apr 27, 2015 12:12 pm

Re: [libmaple F4] Very large sketch size when using malloc(), perhaps only in sloeber, need help with Arduino IDE test

Post by victor_pv » Fri Aug 18, 2017 5:55 pm

Rick, probably we should add it to the F1 too.
I'll add the functions to the F1 and F4 new.cpp files and send PRs.

EDIT:
And just guessing in the example above, this:
dumppter;
should actually be:
dump_ptr;

victor_pv
Posts: 1605
Joined: Mon Apr 27, 2015 12:12 pm

Re: [SOLVED] [libmaple F4] Very large sketch size when using malloc()

Post by victor_pv » Fri Aug 18, 2017 6:32 pm

Rick, did you test it successfully?
I can't use those operators, even with the libstd++ version (remove new.cpp):

Code: Select all

..\malloc_test.ino:41:33: error: no matching function for call to 'operator new(sizetype, unsigned int [12])'
   dumb_ptr = new(memory_buffer) dumbclass;
                                 ^
..\malloc_test.ino:41:33: note: candidate is:
<built-in>:0:0: note: void* operator new(unsigned int)
<built-in>:0:0: note:   candidate expects 1 argument, 2 provided
The sketch has this:

Code: Select all

unsigned memory_buffer[sizeof(dumbclass)];
dumbclass *dumb_ptr;
...
setup(){
  dumb_ptr = new(memory_buffer) dumbclass;
}
And dumbclass is the one from the test you posted before to test malloc and new.

User avatar
Rick Kimball
Posts: 990
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: [SOLVED] [libmaple F4] Very large sketch size when using malloc()

Post by Rick Kimball » Fri Aug 18, 2017 6:58 pm

Sorry if you include that code in new.cpp .. the "inline" probably got it optimized out. I had tested that code with the placement new inline functions in the .ino file.

The best thing would be to put those functions into a "new.h" and have Arduino.h include "new.h" Something to do another day. Forget I ever mentioned ; )
-rick

victor_pv
Posts: 1605
Joined: Mon Apr 27, 2015 12:12 pm

Re: [SOLVED] [libmaple F4] Very large sketch size when using malloc()

Post by victor_pv » Fri Aug 18, 2017 8:36 pm

I thought the libstdc++ would have the declaration like it does for other 'new' operators. Surprisingly I see the functions in file "new" within the GCC folders but not even those compiled and gave the error above.
Well we will work on that at some point, at the moment I'll send the PR to just add new.cpp to the F4 so it doesn't pull libstdc++, then we can look at adding this as a separate PR.

User avatar
Rick Kimball
Posts: 990
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: [SOLVED] [libmaple F4] Very large sketch size when using malloc()

Post by Rick Kimball » Fri Aug 18, 2017 8:50 pm

If a person is going to use C++ new with embedded chips, they might do themselves well to understand about memory pools and custom new for classes.

Scott Meyers C++ book has some great advice there.

google search: Scott Meyers effective c++ pool filetype:pdf
-rick

victor_pv
Posts: 1605
Joined: Mon Apr 27, 2015 12:12 pm

Re: [SOLVED] [libmaple F4] Very large sketch size when using malloc()

Post by victor_pv » Fri Aug 18, 2017 8:59 pm

I've got Effective C++ second edition in a shelf ;) half way thru...
There is a lot of advise about classes, way more than my brain cares to remember...

Post Reply