Skip to content

Commit 703b3b5

Browse files
committed
Update MemoryAllocationStatistics sketch
Use linker script definition to get RAM area + some coscmetic update Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 8a568c7 commit 703b3b5

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

examples/Benchmarking/MemoryAllocationStatistics/MemoryAllocationStatistics.ino

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@
1414
#include <malloc.h>
1515

1616
extern "C" char *sbrk(int i);
17-
extern char _end; /* Defined by the linker */
17+
/* Use linker definition */
18+
extern char _end;
19+
extern char _sdata;
20+
extern char _estack;
21+
extern char _Min_Stack_Size;
1822

19-
// Get from memory area defined in linker script
20-
// Example for STM32F103C8
21-
// RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
22-
char *ramstart=(char *)0x20000000;
23-
char *ramend=(char *)0x20005000;
23+
static char *ramstart = &_sdata;
24+
static char *ramend = &_estack;
25+
static char *minSP = (char*)(ramend - &_Min_Stack_Size);
2426

2527
#define NUM_BLOCKS 100
2628
#define BLOCK_SIZE 4
2729

2830
void display_mallinfo(void)
2931
{
30-
char *heapend=(char*)sbrk(0);
31-
register char * stack_ptr asm("sp");
32-
struct mallinfo mi=mallinfo();
32+
char *heapend = (char*)sbrk(0);
33+
char * stack_ptr = (char*)__get_MSP();
34+
struct mallinfo mi = mallinfo();
3335

3436
Serial.print("Total non-mmapped bytes (arena): ");
3537
Serial.println(mi.arena);
@@ -52,26 +54,25 @@ void display_mallinfo(void)
5254
Serial.print("Topmost releasable block (keepcost): ");
5355
Serial.println(mi.keepcost);
5456

55-
Serial.print("RAM Start 0x");
57+
Serial.print("RAM Start at: 0x");
5658
Serial.println((unsigned long)ramstart, HEX);
57-
Serial.print("Data/Bss end 0x");
59+
Serial.print("Data/Bss end at: 0x");
5860
Serial.println((unsigned long)&_end, HEX);
59-
Serial.print("Heap End 0x");
61+
Serial.print("Heap end at: 0x");
6062
Serial.println((unsigned long)heapend, HEX);
61-
Serial.print("Stack Ptr 0x");
63+
Serial.print("Stack Ptr end at: 0x");
6264
Serial.println((unsigned long)stack_ptr, HEX);
63-
Serial.print("RAM End 0x");
65+
Serial.print("RAM End at: 0x");
6466
Serial.println((unsigned long)ramend, HEX);
6567

66-
Serial.print("Heap RAM Used: ");
68+
Serial.print("Heap RAM Used: ");
6769
Serial.println(mi.uordblks);
68-
Serial.print("Program RAM Used ");
70+
Serial.print("Program RAM Used: ");
6971
Serial.println(&_end - ramstart);
70-
Serial.print("Stack RAM Used ");
72+
Serial.print("Stack RAM Used: ");
7173
Serial.println(ramend - stack_ptr);
72-
7374
Serial.print("Estimated Free RAM: ");
74-
Serial.println(stack_ptr - heapend + mi.fordblks);
75+
Serial.println(((stack_ptr < minSP) ? stack_ptr : minSP) - heapend + mi.fordblks);
7576
}
7677

7778
void setup() {
@@ -81,7 +82,7 @@ void setup() {
8182
}
8283

8384
void loop() {
84-
unsigned int n=0;
85+
unsigned int n = 0;
8586
char *alloc[NUM_BLOCKS];
8687

8788
Serial.println("============== Before allocating blocks ==============");
@@ -92,7 +93,7 @@ void loop() {
9293
if (alloc[n] == NULL) {
9394
Serial.print("Failed to allocate blocks ");
9495
Serial.println(n);
95-
while(1);
96+
while (1);
9697
}
9798
}
9899

@@ -104,7 +105,7 @@ void loop() {
104105

105106
Serial.println("============== After freeing blocks ==============");
106107
display_mallinfo();
107-
while(1);
108+
while (1);
108109
}
109110

110111

0 commit comments

Comments
 (0)