jimkmc / beta-lib

Automatically exported from code.google.com/p/beta-lib
0 stars 0 forks source link

Problem with FreeRam()'s __malloc_heap_start and __brkval with newer avr-gcc #9

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. using avr-gcc 4.7.0 and avr-libc 1.8.0, w/Arduino IDE 1.0.3 or 1.5.2
2. and calling FreeRam()

What is the expected output? to successfully compile.
What do you see instead? Compile errors about __malloc_heap_start and __brkval.

What version of the product are you using? I have seen it on SdFatBeta20130207 
and sdfatlib20121219. 

On what operating system? Windows.

Please provide any additional information below.
I was able to resolve the issue with the following, tweaks to SdFatUtil.cpp, as 
below tagged with MPF:

...
@@ -22,6 +22,8 @@
 #ifdef __arm__
 // should use uinstd.h to define sbrk but Due causes a conflict
 extern "C" char* sbrk(int incr);
+#else
+extern char *__brkval;  // MPF moved from below
 #endif  // __arm__
 //------------------------------------------------------------------------------
 /** Amount of free RAM
@@ -31,9 +33,12 @@ int SdFatUtil::FreeRam() {
   char top;
 #ifdef __arm__
   return &top - reinterpret_cast<char*>(sbrk(0));
+#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
+//  extern char *__brkval; // MPF moved to above
+  return &top - __brkval;
 #else  // __arm__
-  extern char *__malloc_heap_start;
-  extern char *__brkval;
+//  extern char *__brkval; // MPF moved to above
+//  extern char *__malloc_heap_start; // MPF just not needed, in either 
avr-gcc 4.3 or 4.7.
   return __brkval ? &top - __brkval : &top - __malloc_heap_start;
 #endif  // __arm__
 }
...

Granted I recall from other issues you only support avr-gcc native to Arduino 
IDE. Where as this seems related to Issue 8 and the causes for 
SdFatBeta20130207.

It looks like that newer gcc's already have __malloc_heap_start globally 
existing, not needing the extern (cause for duplicate in 4.7) and the extern 
char *__brkval; can NOT be located in the actual function (again stricter 
enforcement). I have tested backwards and forwards compatibility between 4.3.2 
in IDE 1.0.3 and 1.5.2 and using 4.7.0 in these, all appears fine. I have not 
tested it on the Duo. Where it should be a no change.

Original issue reported on code.google.com by mpfl...@gmail.com on 18 Feb 2013 at 3:26

GoogleCodeExporter commented 9 years ago
I have made this mod and it will appear in the next beta.

Original comment by Bill.Gre...@gmail.com on 18 Feb 2013 at 2:07

GoogleCodeExporter commented 9 years ago
I found that __malloc_heap_start may not be defined in all variations of 
Arduino like systems.

I am adding an include for stdlib.h, the official place where 
__malloc_heap_start is defined, to SdFatUtil.cpp.

This should insure __malloc_heap_start is defined exactly once.

Original comment by Bill.Gre...@gmail.com on 19 Feb 2013 at 2:53

GoogleCodeExporter commented 9 years ago
FYI, as expected.
...
#include <SdFatUtil.h>   
+#include <stdlib.h>   
#ifdef __arm__   
...
the above had no issue with windows IDE 1.5.2 and avr-gcc 4.7.0 

Original comment by mpfl...@gmail.com on 19 Feb 2013 at 3:22