MemoryUsage
Arduino library MemoryUsage
MemoryUsage.cpp
Go to the documentation of this file.
1 /*
2 MemoryUsage.c - MemoryUsage library
3 Copyright (c) 2015 Thierry Paris. All right reserved.
4 
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9 
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
25 #include "Arduino.h"
26 #include "MemoryUsage.h"
27 
30 {
31  //extern int __heap_start, *__brkval;
32  int v;
33  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
34 }
35 
37 
38 #define STACK_CANARY 0xc5
39 
40 void mu_StackPaint(void) __attribute__((naked)) __attribute__((section(".init1")));
41 
42 
44 void mu_StackPaint(void)
45 {
46 #if 1
47  uint8_t *p = &_end;
48 
49  while (p <= &__stack)
50  {
51  *p = STACK_CANARY;
52  p++;
53  }
54 #else
55  __asm volatile (
56  " ldi r30,lo8(_end)\n"
57  " ldi r31,hi8(_end)\n"
58  " ldi r24,lo8(0xc5)\n" // STACK_CANARY = 0xc5
59  " ldi r25,hi8(__stack)\n"
60  " rjmp .cmp\n"
61  ".loop:\n"
62  " st Z+,r24\n"
63  ".cmp:\n"
64  " cpi r30,lo8(__stack)\n"
65  " cpc r31,r25\n"
66  " brlo .loop\n"
67  " breq .loop"::);
68 #endif
69 }
70 
72 uint16_t mu_StackCount(void)
73 {
74  uint8_t *p = (__brkval == 0 ? (uint8_t *) &__heap_start : __brkval);
75 
76  while (*p == STACK_CANARY && (int) p <= SP)
77  p++;
78 
79  return (uint16_t)RAMEND - (uint16_t)p;
80 }
81 
83 void SRamDisplay(void)
84 {
85  int data_size = (int)&__data_end - (int)&__data_start;
86  int bss_size = (int)&__bss_end - (int)&__data_end;
87  int heap_end = (int) (__brkval == 0 ? (uint8_t *) &__heap_start : __brkval);
88  //int heap_end = (int)SP - (int)&__malloc_margin;
89  int heap_size = heap_end - (int)&__bss_end;
90  int stack_size = RAMEND - (int)SP + 1;
91  int available = (RAMEND - (int)&__data_start + 1);
92 
93  available -= data_size + bss_size + heap_size + stack_size;
94 
95  Serial.print(F("+----------------+ ")); Serial.print((int)&__data_start); Serial.println(" (__data_start)");
96  Serial.print(F("+ data +")); Serial.println();
97  Serial.print(F("+ variables + size = ")); Serial.println(data_size);
98  Serial.print(F("+----------------+ ")); Serial.print((int)&__data_end); Serial.println(" (__data_end / __bss_start)");
99  Serial.print(F("+ bss +")); Serial.println();
100  Serial.print(F("+ variables + size = ")); Serial.println(bss_size);
101  Serial.print(F("+----------------+ ")); Serial.print((int)&__bss_end); Serial.println(" (__bss_end / __heap_start)");
102  Serial.print(F("+ heap + size = ")); Serial.println(heap_size);
103  Serial.print(F("+----------------+ ")); Serial.print((int)heap_end); Serial.println(" (__brkval if not 0, or __heap_start)");
104  Serial.print(F("+ +")); Serial.println();
105  Serial.print(F("+ +")); Serial.println();
106  Serial.print(F("+ FREE RAM + size = ")); Serial.println(available);
107  Serial.print(F("+ +")); Serial.println();
108  Serial.print(F("+ +")); Serial.println();
109  Serial.print(F("+----------------+ ")); Serial.print((int)SP); Serial.println(" (SP)");
110  Serial.print(F("+ stack + size = ")); Serial.println(stack_size);
111  Serial.print(F("+----------------+ ")); Serial.print((int)RAMEND); Serial.println(" (RAMEND / __stack)");
112 
113  Serial.println();
114  Serial.println();
115 }
void SRamDisplay(void)
Displays the 'map' of the current state of the Arduino's SRAM memory on the Serial console...
Definition: MemoryUsage.cpp:83
#define STACK_CANARY
Copy / adaptation of the library StackPaint available here : https://github.com/WickedDevice/StackPa...
Definition: MemoryUsage.cpp:38
void mu_StackPaint(void) __attribute__ ((naked)) __attribute__ ((section (".init1")))
Function called before any other function.
Definition: MemoryUsage.cpp:44
uint16_t mu_StackCount(void)
Show the stack size on console.
Definition: MemoryUsage.cpp:72
int mu_freeRam(void)
Show the free Ram size on console.