1 /*
2   OrangutanResources.cpp - Measures available RAM on the AVR
3 */
4 
5 /*
6  * Written by Paul Grayson, 2008.
7  * Copyright (c) 2008-2012 Pololu Corporation. For more information, see
8  *
9  *   http://www.pololu.com
10  *   http://forum.pololu.com
11  *   http://www.pololu.com/docs/0J18
12  *
13  * You may freely modify and share this code, as long as you keep this
14  * notice intact (including the two links above).  Licensed under the
15  * Creative Commons BY-SA 3.0 license:
16  *
17  *   http://creativecommons.org/licenses/by-sa/3.0/
18  *
19  * Disclaimer: To the extent permitted by law, Pololu provides this work
20  * without any warranty.  It might be defective, in which case you agree
21  * to be responsible for all resulting costs and damages.
22  */
23 
24 #include "OrangutanResources.h"
25 
26 
get_free_ram()27 extern "C" int get_free_ram()
28 {
29 	return OrangutanResources::getFreeRAM();
30 }
31 
32 
33 // constructor
34 
OrangutanResources()35 OrangutanResources::OrangutanResources()
36 {
37 }
38 
39 extern int __bss_end; // the top of static variable memory
40 extern void *__brkval; // the top of memory used by malloc()
41 
getFreeRAM()42 int OrangutanResources::getFreeRAM()
43 {
44 	// A local variable gives us the location of the
45 	// stack pointer:
46 	int free_memory;
47 
48 	if((int)__brkval == 0) // if malloc is not in use
49 		free_memory = ((int)&free_memory) - ((int)&__bss_end);
50 	else
51 		free_memory = ((int)&free_memory) - ((int)__brkval);
52 
53 	return free_memory;
54 }
55 
56 
57 // Local Variables: **
58 // mode: C++ **
59 // c-basic-offset: 4 **
60 // tab-width: 4 **
61 // indent-tabs-mode: t **
62 // end: **
63