Skip to content

Add new library MemoryFree #396

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

Merged
merged 1 commit into from
Jan 31, 2017
Merged
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
41 changes: 41 additions & 0 deletions libraries/MemoryFree/README.md
Original file line number Diff line number Diff line change
@@ -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.
46 changes: 46 additions & 0 deletions libraries/MemoryFree/examples/freeMemory/freeMemory.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2016 Intel Corporation. All rights reserved.
* See the bottom of this file for the license terms.
*/

#include <MemoryFree.h>

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
*
*/
11 changes: 11 additions & 0 deletions libraries/MemoryFree/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#######################################
# Syntax Coloring Map For MemoryFree
#######################################

#######################################
# Methods and Functions (KEYWORD2)
#######################################

freeStack KEYWORD2
freeHeap KEYWORD2
freeMemory KEYWORD2
10 changes: 10 additions & 0 deletions libraries/MemoryFree/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=MemoryFree
version=1.0
author=Erik Nyquist
maintainer=Erik Nyquist <eknyquist@gmail.com>
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

58 changes: 58 additions & 0 deletions libraries/MemoryFree/src/MemoryFree.cpp
Original file line number Diff line number Diff line change
@@ -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 <malloc.h>
#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;
}
48 changes: 48 additions & 0 deletions libraries/MemoryFree/src/MemoryFree.h
Original file line number Diff line number Diff line change
@@ -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