From 8c95a688841a04a0de9dd4923f92fb68ea55909c Mon Sep 17 00:00:00 2001 From: Neil McNeight Date: Fri, 20 Mar 2015 15:28:22 -0700 Subject: [PATCH 1/5] Updated with code from http://playground.arduino.cc/code/AvailableMemory Added vim related entries to .gitignore from https://github.com/github/gitignore/blob/master/Global/Vim.gitignore Verfied code against Arduino 1.6.1 running on Arduino Leonardo --- .gitignore | 6 ++++ MemoryFree.cpp | 48 +++++++++++++++++++++++++----- MemoryFree.h | 5 +++- README => README.md | 2 ++ examples/FreeMemory/FreeMemory.ino | 31 +++++++++++++++++++ examples/FreeMemory/FreeMemory.pde | 28 ----------------- 6 files changed, 84 insertions(+), 36 deletions(-) rename README => README.md (63%) create mode 100644 examples/FreeMemory/FreeMemory.ino delete mode 100644 examples/FreeMemory/FreeMemory.pde diff --git a/.gitignore b/.gitignore index fa86a4b..cdbd978 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ *.o *.orig .*.swp +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +*.un~ +Session.vim +.netrwhist +*~ diff --git a/MemoryFree.cpp b/MemoryFree.cpp index d1883ee..6823004 100644 --- a/MemoryFree.cpp +++ b/MemoryFree.cpp @@ -1,18 +1,52 @@ -extern unsigned int __bss_end; +#if (ARDUINO >= 100) +#include +#else +#include +#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; +}; -#include "MemoryFree.h" +/* The head of the free list structure */ +extern struct __freelist *__flp; +#include "MemoryFree.h"; -int freeMemory() { - int free_memory; +/* 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; + } + + 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; } diff --git a/MemoryFree.h b/MemoryFree.h index 143bf0c..f9d7cad 100644 --- a/MemoryFree.h +++ b/MemoryFree.h @@ -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 @@ -12,4 +16,3 @@ int freeMemory(); #endif #endif - diff --git a/README b/README.md similarity index 63% rename from README rename to README.md index 237533c..527f486 100644 --- a/README +++ b/README.md @@ -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 diff --git a/examples/FreeMemory/FreeMemory.ino b/examples/FreeMemory/FreeMemory.ino new file mode 100644 index 0000000..d69027e --- /dev/null +++ b/examples/FreeMemory/FreeMemory.ino @@ -0,0 +1,31 @@ +#include + +// 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); +} diff --git a/examples/FreeMemory/FreeMemory.pde b/examples/FreeMemory/FreeMemory.pde deleted file mode 100644 index fbc6228..0000000 --- a/examples/FreeMemory/FreeMemory.pde +++ /dev/null @@ -1,28 +0,0 @@ -#include - -// Reported free memory with str commented out: -// 1848 bytes -// -// Reported free memory with str and Serial.println(str) uncommented: -// 1834 -// -// Difference: 14 bytes (13 ascii chars + null terminator - -// 14-bytes string -//char str[] = "Hallo, world!"; - - -void setup() { - Serial.begin(115200); -} - - -void loop() { - //Serial.println(str); - - Serial.print("freeMemory()="); - Serial.println(freeMemory()); - - delay(1000); -} - From d9ddab08183cbd00a3e56f377f0be2a1bc561745 Mon Sep 17 00:00:00 2001 From: per1234 Date: Mon, 25 May 2015 11:28:40 -0700 Subject: [PATCH 2/5] Remove semicolon from #include "MemoryFree.h"; This causes a compiler warning. --- MemoryFree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MemoryFree.cpp b/MemoryFree.cpp index 6823004..bf57184 100644 --- a/MemoryFree.cpp +++ b/MemoryFree.cpp @@ -20,7 +20,7 @@ struct __freelist /* The head of the free list structure */ extern struct __freelist *__flp; -#include "MemoryFree.h"; +#include "MemoryFree.h" /* Calculates the size of the free list */ int freeListSize() From a2d60bd958f80fc3dea8479467a1309643bdbc63 Mon Sep 17 00:00:00 2001 From: Neil McNeight Date: Tue, 17 May 2016 19:05:30 -0700 Subject: [PATCH 3/5] Adds Travis CI --- .travis.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..091f37f --- /dev/null +++ b/.travis.yml @@ -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 From daf273e75095a79bc1619994cbe97862a245a8df Mon Sep 17 00:00:00 2001 From: s-t-a-n <31728406+s-t-a-n@users.noreply.github.com> Date: Sun, 17 Jan 2021 18:37:46 +0100 Subject: [PATCH 4/5] Replaced 'int' type with 'intptr_t' type in pointer arithmetic for compiling without -fpermisive --- MemoryFree.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/MemoryFree.cpp b/MemoryFree.cpp index bf57184..e7f0f0b 100644 --- a/MemoryFree.cpp +++ b/MemoryFree.cpp @@ -4,6 +4,8 @@ #include #endif +#include + extern unsigned int __heap_start; extern void *__brkval; @@ -39,13 +41,13 @@ int freeListSize() int freeMemory() { int free_memory; - if ((int)__brkval == 0) + if ((intptr_t)__brkval == 0) { - free_memory = ((int)&free_memory) - ((int)&__heap_start); + free_memory = ((intptr_t)&free_memory) - ((intptr_t)&__heap_start); } else { - free_memory = ((int)&free_memory) - ((int)__brkval); + free_memory = ((intptr_t)&free_memory) - ((intptr_t)__brkval); free_memory += freeListSize(); } return free_memory; From 3b3b632a8661a3612982d9f69e7b1ddbd8f55b50 Mon Sep 17 00:00:00 2001 From: s-t-a-n <31728406+s-t-a-n@users.noreply.github.com> Date: Mon, 1 Feb 2021 22:13:52 +0100 Subject: [PATCH 5/5] Updated README.md to reflect changes made to repo --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 527f486..a48b462 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,4 @@ This is the excellent MemoryFree library from http://www.arduino.cc/playground/C 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 +(17 Jan 2021) Replaced 'int' type with 'intptr_t' type in pointer arithmetic for compiling without -fpermisive