From 43de3041805a9363654a0ddaa0ccc336e82fb446 Mon Sep 17 00:00:00 2001 From: Erik Nyquist Date: Tue, 10 Jan 2017 15:18:27 -0800 Subject: [PATCH] Add new library MemoryFree This is the MemoryFree libary from http://playground.arduino.cc/code/AvailableMemory, rewritten specifically for Arduino 101 and tinyTILE. It provides some functions for querying available stack & heap space. --- libraries/MemoryFree/README.md | 41 +++++++++++++ .../examples/freeMemory/freeMemory.ino | 46 +++++++++++++++ libraries/MemoryFree/keywords.txt | 11 ++++ libraries/MemoryFree/library.properties | 10 ++++ libraries/MemoryFree/src/MemoryFree.cpp | 58 +++++++++++++++++++ libraries/MemoryFree/src/MemoryFree.h | 48 +++++++++++++++ 6 files changed, 214 insertions(+) create mode 100644 libraries/MemoryFree/README.md create mode 100644 libraries/MemoryFree/examples/freeMemory/freeMemory.ino create mode 100644 libraries/MemoryFree/keywords.txt create mode 100644 libraries/MemoryFree/library.properties create mode 100644 libraries/MemoryFree/src/MemoryFree.cpp create mode 100644 libraries/MemoryFree/src/MemoryFree.h diff --git a/libraries/MemoryFree/README.md b/libraries/MemoryFree/README.md new file mode 100644 index 00000000..d08aa930 --- /dev/null +++ b/libraries/MemoryFree/README.md @@ -0,0 +1,41 @@ +## MemoryFree library for Arduino/Genuino101 and tinyTILE + +This library is a re-write of http://playground.arduino.cc/Code/AvailableMemory +specifically for Curie-based devices. This library defines the same header file +`MemoryFree.h`, and provides the function `freeMemory()`. In addition, two extra +functions are provided; `freeHeap()`, for heap space only, and `freeStack()`, +for free stack space. `freeStack()` can also be used to detect a stack overflow. + +The rewrite is necessary for two reasons: + +* AVR-based boards use a different implementation of `malloc()` than Curie-based + boards, and in order to determine the amount of free heap space, we depend + on specific symbols defined by the `malloc()` implementation. The `malloc()` + implementation used by Curie-based devices + [can be seen here](https://github.com/foss-for-synopsys-dwc-arc-processors/glibc). + +* Curie-based boards have a different memory layout than AVR-based boards. See + the [linker script](https://github.com/01org/corelibs-arduino101/blob/master/variants/arduino_101/linker_scripts/flash.ld) + for Arduino/Genuino 101 for details of the memory layout + for Curie-based devices. + +## Functions + + +`int freeHeap (void)` + +Returns the number of bytes free in the heap, i.e. the number of bytes free +to be allocated using `malloc()`. + +`int freeStack (void)` + +Returns the number of bytes free in the stack, i.e. the space between the +current stack frame and the end of the stack area. This function will return +a negative number in the event of a stack overflow, representing the size of +the overflow; for example, a return value of -20 means that the current stack +frame is 20 bytes past the end of the stack area. + +`int freeMemory (void)` + +Returns the number of bytes free in both the stack and the heap. If a stack +overflow has occurred, only the number of bytes free in the heap is returned. diff --git a/libraries/MemoryFree/examples/freeMemory/freeMemory.ino b/libraries/MemoryFree/examples/freeMemory/freeMemory.ino new file mode 100644 index 00000000..724f81ac --- /dev/null +++ b/libraries/MemoryFree/examples/freeMemory/freeMemory.ino @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. + */ + +#include + +void setup () { + Serial.begin(9600); + while(!Serial); +} + +void loop() { + char *p; + + Serial.println("Free memory: " + String(freeMemory())); + Serial.println("Allocating 24 bytes ..."); + + p = (char *)malloc(24); + Serial.println("Free memory: " + String(freeMemory())); + + Serial.println("Freeing 24 bytes ..."); + free(p); + Serial.println("Free memory: " + String(freeMemory())); + + delay(2000); +} + +/* + * Copyright (c) 2017 Intel Corporation. All rights 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 Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ diff --git a/libraries/MemoryFree/keywords.txt b/libraries/MemoryFree/keywords.txt new file mode 100644 index 00000000..9da291a1 --- /dev/null +++ b/libraries/MemoryFree/keywords.txt @@ -0,0 +1,11 @@ +####################################### +# Syntax Coloring Map For MemoryFree +####################################### + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +freeStack KEYWORD2 +freeHeap KEYWORD2 +freeMemory KEYWORD2 diff --git a/libraries/MemoryFree/library.properties b/libraries/MemoryFree/library.properties new file mode 100644 index 00000000..1acd530e --- /dev/null +++ b/libraries/MemoryFree/library.properties @@ -0,0 +1,10 @@ +name=MemoryFree +version=1.0 +author=Erik Nyquist +maintainer=Erik Nyquist +sentence=Determines the amount of available memory in the heap +paragraph=Determines the amount of memory, in bytes, that is available for allocation using malloc() +category=Uncategorized +url= +architectures=arc32 + diff --git a/libraries/MemoryFree/src/MemoryFree.cpp b/libraries/MemoryFree/src/MemoryFree.cpp new file mode 100644 index 00000000..8fbba7ee --- /dev/null +++ b/libraries/MemoryFree/src/MemoryFree.cpp @@ -0,0 +1,58 @@ +/* + * MemoryFree.cpp: taken from http://playground.arduino.cc/Code/AvailableMemory, + * re-written for the Arduino 101 which uses a different malloc implementation. + * + * Arduino 101 malloc source: + * https://github.com/foss-for-synopsys-dwc-arc-processors/glibc + * + * mallinfo() struct details: + * http://man7.org/linux/man-pages/man3/mallinfo.3.html + * + * Copyright (c) 2017 Intel Corporation. All rights 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 Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include +#include "MemoryFree.h" + +extern char __start_heap; +extern char __end_heap; +extern char __stack_size; +extern char __stack_start; + +int freeStack() { + int stack_end; + int mark; + + stack_end = ((int)&__stack_start) - ((int)&__stack_size); + return ((int)&mark) - stack_end; +} + +int freeHeap (void) { + int hsize; + struct mallinfo mi; + + mi = mallinfo(); + hsize = (int)&__end_heap - (int)&__start_heap; + return (hsize - mi.arena) + mi.fordblks; +} + +int freeMemory (void) { + int heap = freeHeap(); + int stack = freeStack(); + return (stack < 0) ? heap : stack + heap; +} diff --git a/libraries/MemoryFree/src/MemoryFree.h b/libraries/MemoryFree/src/MemoryFree.h new file mode 100644 index 00000000..bf532bc2 --- /dev/null +++ b/libraries/MemoryFree/src/MemoryFree.h @@ -0,0 +1,48 @@ +/* + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef MEMORYFREE_H +#define MEMORYFREE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* freeHeap: returns the size (in bytes) of unused space on the heap, + * i.e. the number of bytes available for allocation by 'malloc()' */ +int freeHeap(void); + +/* freeStack: returns the size (in bytes) of remaining free space in the stack, + * i.e. the difference between our current position in the stack, and the end + * of usable stack space. + * + * NOTE: This function will return a negative number to indicate a stack + * overflow, i.e. a return value of -20 means you have overrun the allocated + * stack area by 20 bytes. */ +int freeStack(void); + +/* freeMemory: returns the combined free memory in both the stack and heap, + * except in the case where a stack overflow has occurred (i.e. freeStack + * returns a negative number). In this case, only the amount of free heap + * space will be returned. */ +int freeMemory(void); + +#ifdef __cplusplus +} +#endif + +#endif