<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: getFreeMemory code on the maple.</title>
		<link>http://forums.leaflabs.com/topic.php?id=2369</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:08:28 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=2369" rel="self" type="application/rss+xml" />

		<item>
			<title>feurig on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-20841</link>
			<pubDate>Wed, 07 Nov 2012 01:43:26 +0000</pubDate>
			<dc:creator>feurig</dc:creator>
			<guid isPermaLink="false">20841@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Also I couldn't get anything meaningful out of _sbrk by itself my current solution assumes that newlib is pretty dumb about its actual allocation so it remains to me unsolved. So I am perplexed at best.....&#60;/p&#62;
&#60;p&#62;&#60;code&#62;&#60;/p&#62;
&#60;p&#62;unsigned long int getFreeMemory() // none of this works.&#60;br /&#62;
{   static long int *testMalloc;&#60;br /&#62;
    unsigned long int freeMemory;&#60;/p&#62;
&#60;p&#62;    testMalloc = (long int *) malloc(10);&#60;br /&#62;
    if (testMalloc==0) return 0;&#60;br /&#62;
//    freeMemory = (unsigned long int)CONFIG_HEAP_END - (unsigned long int)(testMalloc);&#60;br /&#62;
    freeMemory = (unsigned long int)(&#38;amp;freeMemory) - (unsigned long int)(testMalloc);&#60;br /&#62;
//    freeMemory = (unsigned long int)CONFIG_HEAP_END - (unsigned long int) _sbrk(0);&#60;br /&#62;
//    freeMemory = (long unsigned int) (&#38;amp;freeMemory) - (unsigned long int) _sbrk(0);&#60;/p&#62;
&#60;p&#62;    free(testMalloc);&#60;br /&#62;
    return freeMemory;&#60;br /&#62;
}&#60;br /&#62;
&#60;/code&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>feurig on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-20833</link>
			<pubDate>Tue, 06 Nov 2012 17:34:16 +0000</pubDate>
			<dc:creator>feurig</dc:creator>
			<guid isPermaLink="false">20833@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;This doesn't take into account the stack though.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>feurig on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-20830</link>
			<pubDate>Tue, 06 Nov 2012 15:56:36 +0000</pubDate>
			<dc:creator>feurig</dc:creator>
			<guid isPermaLink="false">20830@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;As I was revisiting sys calls for other things. I was able to resolve this in a similar way &#60;/p&#62;
&#60;pre&#62;&#60;code&#62;#ifndef CONFIG_HEAP_START
extern char _lm_heap_start;
#define CONFIG_HEAP_START               ((caddr_t)&#38;amp;_lm_heap_start)
#endif
#ifndef CONFIG_HEAP_END
extern char _lm_heap_end;
#define CONFIG_HEAP_END                 ((caddr_t)&#38;amp;_lm_heap_end)
#endif
unsigned long int getFreeMemory()
{   static long int *testMalloc;
    unsigned long int freeMemory;
    testMalloc = (long int *) malloc(10);
    if (testMalloc==0) return 0;
    freeMemory = (unsigned long int)CONFIG_HEAP_END - (unsigned long int)(testMalloc);
    free(testMalloc);
    return freeMemory;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;I am getting a similar number ~13K which makes sense given that there is 20k to work with.&#60;br /&#62;
This also flys in the face of the documentation that claims that you can't malloc.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>chipaudette on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-20313</link>
			<pubDate>Tue, 16 Oct 2012 11:56:44 +0000</pubDate>
			<dc:creator>chipaudette</dc:creator>
			<guid isPermaLink="false">20313@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Has there been any progress on this?  I used the information above to get a value returned by _sbrk, but I'm not sure if it is meaningful.  Here's what I did to get it to return something at all...&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;#include &#38;quot;syscalls.c&#38;quot;
extern caddr_t _sbrk(int incr);
#ifndef CONFIG_HEAP_END
extern char _lm_heap_end;
#define CONFIG_HEAP_END ((caddr_t)&#38;amp;_lm_heap_end)
#endif
int getFreeMemory()
{
  return CONFIG_HEAP_END - _sbrk(0);
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;I did not edit my common.inc.  It is stock from the 0.0.12 distrubtion.  For my current program, calling &#60;code&#62;SerialUSB.println(getFreeMemory());&#60;/code&#62; returns a value of 14224.  Is this really the number of bytes that I have free?  Or, do I need to do the mod to the common.inc?&#60;/p&#62;
&#60;p&#62;Thanks,&#60;/p&#62;
&#60;p&#62;Chip
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-12510</link>
			<pubDate>Fri, 17 Aug 2012 16:22:35 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">12510@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I see. Bummer. I've never used FreeRTOS, so I can't really help here.&#60;/p&#62;
&#60;p&#62;Speaking of toolchains, have you tried the new gcc-arm-embedded support out? Should work in latest master.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>feurig on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-12473</link>
			<pubDate>Thu, 16 Aug 2012 20:13:34 +0000</pubDate>
			<dc:creator>feurig</dc:creator>
			<guid isPermaLink="false">12473@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Actually the freertos that you are distributing uses heap_2 which does not use newlib, and i couldn't get heap_3 which would wrap newlib's implementation to work (which sucks because I am very fond of stdio and most of the ansi stuff). I will try to post some blog stuff about my experiences with &#34;FreeRtos vs libmaple vs Newlib&#34;. As the toolchain moves forward it should get less painful one would hope. &#60;a href=&#34;http://michaldemin.wordpress.com/2010/03/09/freertos-newlib-on-cortex-m3/&#34; rel=&#34;nofollow&#34;&#62;http://michaldemin.wordpress.com/2010/03/09/freertos-newlib-on-cortex-m3/&#60;/a&#62; does a pretty good job of describing some of the issue. Although things that depend on how you compile newlib really don't generalize or integrate well.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-12451</link>
			<pubDate>Wed, 15 Aug 2012 14:51:36 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">12451@http://forums.leaflabs.com/</guid>
			<description>&#60;blockquote&#62;&#60;p&#62;
 I was trying to debug some FreeRTOS code which allocates from the heap outside of this mechanism anyways.
&#60;/p&#62;&#60;/blockquote&#62;
&#60;p&#62;Curious -- what do you mean? If the FreeRTOS code uses malloc(), then it should be using newlib's implementation, which uses our _sbrk() to request more space. Our _sbrk() respects _lm_heap_end, so you should be OK, unless I've misunderstood.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>feurig on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-12434</link>
			<pubDate>Wed, 15 Aug 2012 01:36:07 +0000</pubDate>
			<dc:creator>feurig</dc:creator>
			<guid isPermaLink="false">12434@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Mbolivar, Thanks. I will come back to this. I was trying to debug some FreeRTOS code which allocates from the heap outside of this mechanism anyways.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-12290</link>
			<pubDate>Fri, 10 Aug 2012 16:26:07 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">12290@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;libmaple's linker script will always define &#60;code&#62;_lm_heap_end&#60;/code&#62;; however, on most boards, its value is the initial stack pointer (so the heap and stack overlap).&#60;/p&#62;
&#60;p&#62;Probably the right thing to do is put it somewhere below the stack, so there's a minimum guaranteed stack size. Then, as long as you don't use too much stack, you could use &#60;code&#62;_lm_heap_end&#60;/code&#62; safely to build a getFreeMemory() type function.&#60;/p&#62;
&#60;p&#62;Would you care to prepare a patch? See support/ld/common.inc:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;/*
         * Heap: Linker scripts may choose a custom heap by overriding
         * _lm_heap_start and _lm_heap_end. Otherwise, the heap is in
         * internal SRAM, beginning after .bss, and growing towards
         * the stack.
         *
         * I&#38;#39;m shoving these here naively; there&#38;#39;s probably a cleaner way
         * to go about this. [mbolivar]
         */
        _lm_heap_start = DEFINED(_lm_heap_start) ? _lm_heap_start : _end;
        _lm_heap_end   = DEFINED(_lm_heap_end) ? _lm_heap_end : __msp_init;&#60;/code&#62;&#60;/pre&#62;</description>
		</item>
		<item>
			<title>feurig on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-12284</link>
			<pubDate>Fri, 10 Aug 2012 12:50:37 +0000</pubDate>
			<dc:creator>feurig</dc:creator>
			<guid isPermaLink="false">12284@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;The reason that doesnt compile is because its basically the same avr-gcc code as above.&#60;br /&#62;
I am playing around with something along these lines. &#60;/p&#62;
&#60;p&#62;extern caddr_t _sbrk(int incr);&#60;br /&#62;
#ifndef CONFIG_HEAP_END&#60;br /&#62;
extern char _lm_heap_end;&#60;br /&#62;
#define CONFIG_HEAP_END                 ((caddr_t)&#38;amp;_lm_heap_end)&#60;br /&#62;
#endif&#60;/p&#62;
&#60;p&#62;int getFreeMemory()&#60;br /&#62;
{&#60;br /&#62;
    return CONFIG_HEAP_END - _sbrk(0);&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;If anyone has any better ideas that would be swell.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>shark2600 on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-12280</link>
			<pubDate>Fri, 10 Aug 2012 08:53:33 +0000</pubDate>
			<dc:creator>shark2600</dc:creator>
			<guid isPermaLink="false">12280@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;There is a FreeRam function in dinau's SdFatMaple-vld lib. It is under condition #if 0, I couldn't get it compiled, one symbol is missing. Hope this can help you.&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;https://bitbucket.org/dinau/sdfatmaple-vld/src/a386d1505010/SdFatUtil.h&#34; rel=&#34;nofollow&#34;&#62;https://bitbucket.org/dinau/sdfatmaple-vld/src/a386d1505010/SdFatUtil.h&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;/** Return the number of bytes currently free in RAM. */&#60;br /&#62;
#if 0&#60;br /&#62;
static int FreeRam(void) {&#60;br /&#62;
  extern int  __bss_end;&#60;br /&#62;
  extern int* __brkval;&#60;br /&#62;
  int free_memory;&#60;br /&#62;
  if (reinterpret_cast&#38;lt;int&#38;gt;(__brkval) == 0) {&#60;br /&#62;
    // if no heap use from end of bss section&#60;br /&#62;
    free_memory = reinterpret_cast&#38;lt;int&#38;gt;(&#38;amp;free_memory)&#60;br /&#62;
                  - reinterpret_cast&#38;lt;int&#38;gt;(&#38;amp;__bss_end);&#60;br /&#62;
  } else {&#60;br /&#62;
    // use from top of stack to heap&#60;br /&#62;
    free_memory = reinterpret_cast&#38;lt;int&#38;gt;(&#38;amp;free_memory)&#60;br /&#62;
                  - reinterpret_cast&#38;lt;int&#38;gt;(__brkval);&#60;br /&#62;
  }&#60;br /&#62;
  return free_memory;&#60;br /&#62;
}&#60;br /&#62;
#endif
&#60;/p&#62;</description>
		</item>
		<item>
			<title>feurig on "getFreeMemory code on the maple."</title>
			<link>http://forums.leaflabs.com/topic.php?id=2369#post-12262</link>
			<pubDate>Thu, 09 Aug 2012 14:41:13 +0000</pubDate>
			<dc:creator>feurig</dc:creator>
			<guid isPermaLink="false">12262@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Dumb question. What is the best way to get the free memory available on the maple?&#60;/p&#62;
&#60;p&#62;In avr-gcc you would do this. &#60;/p&#62;
&#60;p&#62;extern int __bss_end;&#60;br /&#62;
extern void *__brkval;&#60;/p&#62;
&#60;p&#62;int getFreeMemory()&#60;br /&#62;
{&#60;br /&#62;
    int free_memory;&#60;br /&#62;
    if((int)__brkval == 0)&#60;br /&#62;
        free_memory = ((int)&#38;amp;free_memory) - ((int)&#38;amp;__bss_end);&#60;br /&#62;
    else&#60;br /&#62;
        free_memory = ((int)&#38;amp;free_memory) - ((int)__brkval);&#60;br /&#62;
    return free_memory;&#60;br /&#62;
}
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
