Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pj/easier to test #7

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions examples/FreeRam/FreeRam.ino
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#include <MemoryUsage.h>

byte * p = 0;


int updateStack(void)
{
char volatile stuff[] = "updating stack!";
stuff[0] = 'U';
Serial.println((char *) stuff);
FREERAM_PRINT
return MU::getFreeRam();
}

void setup()
{
Serial.begin(115200);
Serial.println(F( "Running " __FILE__ ", Built " __DATE__));
Serial.println(F("Starting state of the memory:"));
Serial.println();

Expand All @@ -13,12 +26,19 @@ void setup()
MEMORY_PRINT_END
MEMORY_PRINT_HEAPSIZE

updateStack();

Serial.println();
Serial.println();

FREERAM_PRINT;

//byte *p = new byte[3000];
byte *p = new byte[300]; // Uno (ATmega328) only has 2k RAM

byte *p = new byte[3000];
if(!p) {
Serial.println(F("could not allocate bytes for p[] array!"));
}

Serial.println();
Serial.println();
Expand All @@ -37,9 +57,15 @@ void setup()
Serial.println();

FREERAM_PRINT;

Serial.print(F("num STACK_COMPUTE calls: "));
Serial.println(numStackComputeCalls);

delete p;
p = 0;
}

void loop()
{

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

// Simple example to report memory sizes

void reportAllocation(int numBytes) {

Serial.print(F("Allocating for "));
Serial.print( numBytes );
Serial.print(F(" bytes; "));

byte *p = new byte[numBytes];

if (p) {
Serial.println(F("...success."));
} else {
Serial.println(F("...allocation FAILED"));
}

MEMORY_PRINT_HEAPSIZE
FREERAM_PRINT

Serial.println(F("\ndeleting byte array with delete"));
delete p; // don't want a memory leak!
p = 0; // don't want a dangling/obsolete pointer

MEMORY_PRINT_HEAPSIZE
FREERAM_PRINT
}

void setup() {
Serial.begin(115200);
delay(1000);
Serial.println(F( "Running " __FILE__ ", Built " __DATE__));

Serial.println(F("\nStarting conditions"));
MEMORY_PRINT_TOTALSIZE
MEMORY_PRINT_HEAPSIZE
FREERAM_PRINT

Serial.println(F("\nallocate a byte array with new; too big to fit in RAM?"));
reportAllocation(3000);

Serial.println(F("\nallocate smaller byte array with new (it should fit)"));
reportAllocation(300);

Serial.println(F("\nFinal conditions"));
MEMORY_PRINT_HEAPSIZE
FREERAM_PRINT
}

void loop() {
// User reads output from setup().
}
70 changes: 34 additions & 36 deletions examples/Stack/Stack.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,30 @@ void subPointer(rhaaa *apSample);
void subSmartPointer(rhaaa &aSample);
void subConstSmartPointer(const rhaaa &aSample);

inline void reportMemoryInfo(void)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reportMemoryInfo() helps ensure the console text dump is consistent each time it is called. This makes it easier to compare output and recognize changes at each step. (Stack.ino oputput may have added or rearranged lines to make this happen, so comparing output files from earlier runs may see changes)

{
STACK_COMPUTE

MEMORY_PRINT_START
MEMORY_PRINT_HEAPSTART
MEMORY_PRINT_HEAPEND
MEMORY_PRINT_STACKSTART
MEMORY_PRINT_END
MEMORY_PRINT_STACKSIZE

STACK_PRINT

STACKPAINT_PRINT
Serial.println();
}

void setup()
{
Serial.begin(115200);
Serial.println(F( "Running " __FILE__ ", Built " __DATE__));

reportMemoryInfo();
//STACK_COMPUTE

// An instance of the sample is declared, and the string is filled with
// some string to see how to access to it inside functions !
Expand All @@ -31,14 +52,7 @@ void setup()
Serial.println(F("Starting state of the memory:"));
Serial.println();

MEMORY_PRINT_START
MEMORY_PRINT_HEAPSTART
MEMORY_PRINT_HEAPEND
MEMORY_PRINT_STACKSTART
MEMORY_PRINT_END
MEMORY_PRINT_STACKSIZE

STACKPAINT_PRINT
reportMemoryInfo();

Serial.println();
Serial.println();
Expand Down Expand Up @@ -68,20 +82,18 @@ void setup()
// No data as argument, nut a nig array of doubles inside the function...
subLocalData();

STACKPAINT_PRINT
//STACKPAINT_PRINT

Serial.println();
Serial.println();

Serial.println(F("Ending state of the memory:"));
Serial.println();

MEMORY_PRINT_START
MEMORY_PRINT_HEAPSTART
MEMORY_PRINT_HEAPEND
MEMORY_PRINT_STACKSTART
MEMORY_PRINT_END
MEMORY_PRINT_STACKSIZE
reportMemoryInfo();

Serial.print(F("num STACK_COMPUTE calls: "));
Serial.println(numStackComputeCalls);

Serial.println();
Serial.println();
Expand All @@ -91,43 +103,31 @@ void subFull(rhaaa aSample)
{
Serial.println("subFull");
Serial.println(aSample.text);
MEMORY_PRINT_STACKSTART
MEMORY_PRINT_END
MEMORY_PRINT_STACKSIZE
STACK_PRINT
reportMemoryInfo();
Serial.println();
}

void subPointer(rhaaa *apSample)
{
Serial.println("subPointer");
Serial.println(apSample->text);
MEMORY_PRINT_STACKSTART
MEMORY_PRINT_END
MEMORY_PRINT_STACKSIZE
STACK_PRINT
reportMemoryInfo();
Serial.println();
}

void subSmartPointer(rhaaa &aSample)
{
Serial.println("subSmartPointer");
Serial.println(aSample.text);
MEMORY_PRINT_STACKSTART
MEMORY_PRINT_END
MEMORY_PRINT_STACKSIZE
STACK_PRINT
reportMemoryInfo();
Serial.println();
}

void subConstSmartPointer(const rhaaa &aSample)
{
Serial.println("subConstSmartPointer");
Serial.println(aSample.text);
MEMORY_PRINT_STACKSTART
MEMORY_PRINT_END
MEMORY_PRINT_STACKSIZE
STACK_PRINT
reportMemoryInfo();
Serial.println();
}

Expand All @@ -141,14 +141,12 @@ void subLocalData()
v[i] = (double)i;

Serial.println(v[10]);
MEMORY_PRINT_STACKSTART
MEMORY_PRINT_END
MEMORY_PRINT_STACKSIZE
STACK_PRINT
reportMemoryInfo();
Serial.println();
}

void loop()
{

}
}

Loading