Skip to content

Updated with code from http://playground.arduino.cc/code/AvailableMemory #2

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

Open
wants to merge 4 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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
*.o
*.orig
.*.swp
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist
*~
31 changes: 31 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
language: c
sudo: false
before_install:
- "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16"
- sleep 3
- export DISPLAY=:1.0
- wget http://downloads.arduino.cc/arduino-1.6.9-linux64.tar.xz
- tar xf arduino-1.6.9-linux64.tar.xz
- mv arduino-1.6.9 $HOME/arduino_ide
install:
- ln -s $PWD $HOME/arduino_ide/libraries/MemoryFree
- export PATH="$HOME/arduino_ide:$PATH"
- arduino --pref "boardsmanager.additional.urls=https://adafruit.github.io/arduino-board-index/package_adafruit_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json" --save-prefs 2>&1
- arduino --install-boards arduino:sam 2>&1 /dev/null
- arduino --install-boards arduino:samd 2>&1 /dev/null
- arduino --install-boards esp8266:esp8266 2>&1 /dev/null
- arduino --install-boards adafruit:avr 2>&1 /dev/null
- arduino --install-library TinyWireM 2>&1
- arduino --pref "compiler.warning_level=all" --save-prefs
script:
- arduino --verify --board arduino:avr:uno $PWD/examples/FreeMemory/FreeMemory.ino
- arduino --verify --board arduino:sam:arduino_due_x $PWD/examples/FreeMemory/FreeMemory.ino
- arduino --verify --board arduino:samd:zero $PWD/examples/FreeMemory/FreeMemory.ino
- arduino --verify --board esp8266:esp8266:huzzah $PWD/examples/FreeMemory/FreeMemory.ino
- arduino --verify --board arduino:avr:leonardo $PWD/examples/FreeMemory/FreeMemory.ino
- arduino --verify --board adafruit:avr:trinket5 $PWD/examples/FreeMemory/FreeMemory.ino
- arduino --verify --board arduino:avr:gemma $PWD/examples/FreeMemory/FreeMemory.ino
notifications:
email:
on_success: change
on_failure: change
46 changes: 40 additions & 6 deletions MemoryFree.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
extern unsigned int __bss_end;
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

extern unsigned int __heap_start;
extern void *__brkval;

/*
* The free list structure as maintained by the
* avr-libc memory allocation routines.
*/
struct __freelist
{
size_t sz;
struct __freelist *nx;
};

/* The head of the free list structure */
extern struct __freelist *__flp;

#include "MemoryFree.h"

/* Calculates the size of the free list */
int freeListSize()
{
struct __freelist* current;
int total = 0;
for (current = __flp; current; current = current->nx)
{
total += 2; /* Add two bytes for the memory block's header */
total += (int) current->sz;
}

int freeMemory() {
int free_memory;
return total;
}

if((int)__brkval == 0)
free_memory = ((int)&free_memory) - ((int)&__bss_end);
int freeMemory()
{
int free_memory;
if ((int)__brkval == 0)
{
free_memory = ((int)&free_memory) - ((int)&__heap_start);
}
else
{
free_memory = ((int)&free_memory) - ((int)__brkval);

free_memory += freeListSize();
}
return free_memory;
}
5 changes: 4 additions & 1 deletion MemoryFree.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// MemoryFree library based on code posted here:
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213583720/15
// Extended by Matthew Murdoch to include walking of the free list.

#ifndef MEMORY_FREE_H
#define MEMORY_FREE_H

Expand All @@ -12,4 +16,3 @@ int freeMemory();
#endif

#endif

2 changes: 2 additions & 0 deletions README → README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
This is the excellent MemoryFree library from http://www.arduino.cc/playground/Code/AvailableMemory.

I am hosting it here so there is a quick and easy way to pull it down.

(20 Mar 2015) Repository updated with code from http://playground.arduino.cc/code/AvailableMemory
31 changes: 31 additions & 0 deletions examples/FreeMemory/FreeMemory.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <MemoryFree.h>

// On Arduino Leonardo with ATmega32U4:
//
// Reported free memory with str commented out:
// 2373 bytes
//
// Reported free memory with str and Serial.println(str) uncommented:
// 2359 bytes
//
// Difference: 14 bytes (13 ASCII chars + null terminator)

// 14-bytes string
//char str[] = "Hello, world!";

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}

void loop() {
// Serial.println(str);

Serial.print("freeMemory()=");
Serial.println(freeMemory());

delay(1000);
}
28 changes: 0 additions & 28 deletions examples/FreeMemory/FreeMemory.pde

This file was deleted.