Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Trusty77 committed Jul 26, 2015
1 parent 46d71f1 commit 1feb5ef
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 1 deletion.
56 changes: 56 additions & 0 deletions MemoryUsage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
MemoryUsage.h - MemoryUsage library
Copyright (c) 2015 Thierry Paris. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef __MemoryUsage_h__
#define __MemoryUsage_h__

// Must be used only one time, outside any function.
#define STACK_DECLARE byte *stack_end_max;

// Must be used inside a function, as soon as possible, basicaly at the beginning of the setup().
// Can be recalled at any time to reset the stack counter and have a local count of stack bytes.
#define STACK_START byte b_Stack; stack_end_max = &b_Stack;

// Must be call to update the current maximum size of the stack, at each function beginning.
#define STACK_COMPUTE { byte b_Stack; \
byte *stack_local_end = &b_Stack; \
if ((int) stack_local_end < (int) stack_end_max)\
stack_end_max = stack_local_end; }

// Compute the current maximum and show it now with customized text.
#define STACK_PRINT(text) { STACK_COMPUTE; Serial.print(text); Serial.println(RAMEND - (int) stack_end_max); }

// Compute the current maximum and show it now with default text.
#define STACK_PRINT STACK_PRINT(F("Stack Size:"));

// Shows the current free SRAM memory with customized text.
#define FREERAM_PRINT(text) Serial.print(text); Serial.println(freeRam());

// Shows the current free SRAM memory with default text.
#define FREERAM_PRINT FREERAM_PRINT(F("Free Ram Size:"));

// Thanks to adafruit : https://learn.adafruit.com/memories-of-an-arduino/measuring-free-memory
int freeRam ()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

#endif
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This library can be used to detect memory over use. Use it to check Stack size, and free memory size...
16 changes: 16 additions & 0 deletions examples/FreeRam/FreeRam.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <MemoryUsage.h>

void setup()
{
Serial.begin(115200);
FREERAM_PRINT;

byte *p = new byte[3000];

FREERAM_PRINT;
}

void loop()
{

}
93 changes: 93 additions & 0 deletions examples/Stack/Stack.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <MemoryUsage.h>

STACK_DECLARE;

// Dummy structure sample, with a complex content...
struct rhaaa
{
int ival[50];
double dval[10];
char text[80];
};

void subFull(rhaaa aSample);
void subPointer(rhaaa *apSample);
void subSmartPointer(rhaaa &aSample);
void subConstSmartPointer(const rhaaa &aSample);

void setup()
{
Serial.begin(115200);

// An instance of the sample is declared, and the string is filled with
// some string to see how to access to it inside functions !
rhaaa sample;
strcpy(sample.text, "Test string");

{
// First attempt, just pass the structure. The full content is duplicated on the stack.
// If the sub function modifies the content, the original structure from the caller
// function will not be affected.
STACK_START;
STACK_PRINT(F("Stack Size start:"));
subFull(sample);
}

{
// Here we pass a pointer to the original structure instance.
// Only this pointer is added to the stack.
// The content is fully modifiable by the sub function.
// This is the best way to let a sub funtion modify an argument.
STACK_START;
STACK_PRINT(F("Stack Size start:"));
subPointer(&sample);
}

{
// Here also, this is a pointer which is passed, but the sub function see its argument
// as a normal data, not a pointer. Be careful here because the sub function can modify
// the structure content and because this is not a pointer syntax, you can believe that
// you only modify a copy !
STACK_START;
STACK_PRINT(F("Stack Size start:"));
subSmartPointer(sample);
}

{
// You have here the best way to pass a structure if you dont want to modify it.
// Only a pointer is added to the stack, and any try to modify the struture content
// will be detected as an error by the compiler.
STACK_START;
STACK_PRINT(F("Stack Size start:"));
subConstSmartPointer(sample);
}
}

void subFull(rhaaa aSample)
{
Serial.println(aSample.text);
STACK_PRINT(F("Stack Size subFull:"));
}

void subPointer(rhaaa *apSample)
{
Serial.println(apSample->text);
STACK_PRINT(F("Stack Size subPointer:"));
}

void subSmartPointer(rhaaa &aSample)
{
Serial.println(aSample.text);
STACK_PRINT(F("Stack Size subSmartPointer:"));
}

void subConstSmartPointer(const rhaaa &aSample)
{
Serial.println(aSample.text);
STACK_PRINT(F("Stack Size subConstSmartPointer:"));
}

void loop()
{

}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=MemoryUsage
version=1.0.0
author=Thierry PARIS
maintainer=Thierry PARIS <[email protected]>
sentence=Use this library to check your memory usage.
paragraph=
category=Memory
url=https://github.com/adafruit/Adafruit-GFX-Library
architectures=*
24 changes: 24 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Software License Agreement (BSD License)

Copyright (c) 2015 Thierry Paris. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
1 change: 0 additions & 1 deletion test

This file was deleted.

0 comments on commit 1feb5ef

Please sign in to comment.