Skip to content

Commit b608602

Browse files
committed
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.
1 parent 29daefb commit b608602

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed

libraries/MemoryFree/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## MemoryFree library for Arduino/Genuino101 and tinyTILE
2+
3+
This library is a re-write of http://playground.arduino.cc/Code/AvailableMemory
4+
specifically for Curie-based devices. This library defines the same header file
5+
`MemoryFree.h`, and provides the function `freeMemory()`. In addition, two extra
6+
functions are provided; `freeHeap()`, for heap space only, and `freeStack()`,
7+
for free stack space. `freeStack()` can also be used to detect a stack overflow.
8+
9+
The rewrite is necessary for two reasons:
10+
11+
* AVR-based boards use a different implementation of `malloc()` than Curie-based
12+
boards, and in order to determine the amount of free heap space, we depend
13+
on specific symbols defined by the `malloc()` implementation. The `malloc()`
14+
implementation used by Curie-based devices
15+
[can be seen here](https://github.com/foss-for-synopsys-dwc-arc-processors/glibc).
16+
17+
* Curie-based boards have a different memory layout than AVR-based boards. See
18+
the [linker script](https://github.com/01org/corelibs-arduino101/blob/master/variants/arduino_101/linker_scripts/flash.ld)
19+
for Arduino/Genuino 101 for details of the memory layout
20+
for Curie-based devices.
21+
22+
## Functions
23+
24+
25+
`int freeHeap (void)`
26+
27+
Returns the number of bytes free in the heap, i.e. the number of bytes free
28+
to be allocated using `malloc()`.
29+
30+
`int freeStack (void)`
31+
32+
Returns the number of bytes free in the stack, i.e. the space between the
33+
current stack frame and the end of the stack area. This function will return
34+
a negative number in the event of a stack overflow, representing the size of
35+
the overflow; for example, a return value of -20 means that the current stack
36+
frame is 20 bytes past the end of the stack area.
37+
38+
`int freeMemory (void)`
39+
40+
Returns the number of bytes free in both the stack and the heap. If a stack
41+
overflow has occurred, only the number of bytes free in the heap is returned.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
5+
6+
#include <MemoryFree.h>
7+
8+
void setup () {
9+
Serial.begin(9600);
10+
while(!Serial);
11+
}
12+
13+
void loop() {
14+
char *p;
15+
16+
Serial.println("Free memory: " + String(freeMemory()));
17+
Serial.println("Allocating 24 bytes ...");
18+
19+
p = (char *)malloc(24);
20+
Serial.println("Free memory: " + String(freeMemory()));
21+
22+
Serial.println("Freeing 24 bytes ...");
23+
free(p);
24+
Serial.println("Free memory: " + String(freeMemory()));
25+
26+
delay(2000);
27+
}
28+
29+
/*
30+
* Copyright (c) 2017 Intel Corporation. All rights reserved.
31+
*
32+
* This library is free software; you can redistribute it and/or
33+
* modify it under the terms of the GNU Lesser General Public
34+
* License as published by the Free Software Foundation; either
35+
* version 2.1 of the License, or (at your option) any later version.
36+
*
37+
* This library is distributed in the hope that it will be useful,
38+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
39+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40+
* Lesser General Public License for more details.
41+
*
42+
* You should have received a copy of the GNU Lesser General Public
43+
* License along with this library; if not, write to the Free Software
44+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
45+
*
46+
*/

libraries/MemoryFree/keywords.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#######################################
2+
# Syntax Coloring Map For MemoryFree
3+
#######################################
4+
5+
#######################################
6+
# Methods and Functions (KEYWORD2)
7+
#######################################
8+
9+
freeStack KEYWORD2
10+
freeHeap KEYWORD2
11+
freeMemory KEYWORD2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=MemoryFree
2+
version=1.0
3+
author=Erik Nyquist
4+
maintainer=Erik Nyquist <eknyquist@gmail.com>
5+
sentence=Determines the amount of available memory in the heap
6+
paragraph=Determines the amount of memory, in bytes, that is available for allocation using malloc()
7+
category=Uncategorized
8+
url=
9+
architectures=arc32
10+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* MemoryFree.cpp: taken from http://playground.arduino.cc/Code/AvailableMemory,
3+
* re-written for the Arduino 101 which uses a different malloc implementation.
4+
*
5+
* Arduino 101 malloc source:
6+
* https://github.com/foss-for-synopsys-dwc-arc-processors/glibc
7+
*
8+
* mallinfo() struct details:
9+
* http://man7.org/linux/man-pages/man3/mallinfo.3.html
10+
*
11+
* Copyright (c) 2017 Intel Corporation. All rights reserved.
12+
*
13+
* This library is free software; you can redistribute it and/or
14+
* modify it under the terms of the GNU Lesser General Public
15+
* License as published by the Free Software Foundation; either
16+
* version 2.1 of the License, or (at your option) any later version.
17+
18+
* This library is distributed in the hope that it will be useful,
19+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21+
* Lesser General Public License for more details.
22+
23+
* You should have received a copy of the GNU Lesser General Public
24+
* License along with this library; if not, write to the Free Software
25+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26+
*
27+
*/
28+
29+
#include <malloc.h>
30+
#include "MemoryFree.h"
31+
32+
extern char __start_heap;
33+
extern char __end_heap;
34+
extern char __stack_size;
35+
extern char __stack_start;
36+
37+
int freeStack() {
38+
int stack_end;
39+
int mark;
40+
41+
stack_end = ((int)&__stack_start) - ((int)&__stack_size);
42+
return ((int)&mark) - stack_end;
43+
}
44+
45+
int freeHeap (void) {
46+
int hsize;
47+
struct mallinfo mi;
48+
49+
mi = mallinfo();
50+
hsize = (int)&__end_heap - (int)&__start_heap;
51+
return (hsize - mi.arena) + mi.fordblks;
52+
}
53+
54+
int freeMemory (void) {
55+
int heap = freeHeap();
56+
int stack = freeStack();
57+
return (stack < 0) ? heap : stack + heap;
58+
}

libraries/MemoryFree/src/MemoryFree.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This library is free software; you can redistribute it and/or
3+
* modify it under the terms of the GNU Lesser General Public
4+
* License as published by the Free Software Foundation; either
5+
* version 2.1 of the License, or (at your option) any later version.
6+
7+
* This library is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10+
* Lesser General Public License for more details.
11+
12+
* You should have received a copy of the GNU Lesser General Public
13+
* License along with this library; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15+
*
16+
*/
17+
18+
#ifndef MEMORYFREE_H
19+
#define MEMORYFREE_H
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
/* freeHeap: returns the size (in bytes) of unused space on the heap,
26+
* i.e. the number of bytes available for allocation by 'malloc()' */
27+
int freeHeap(void);
28+
29+
/* freeStack: returns the size (in bytes) of remaining free space in the stack,
30+
* i.e. the difference between our current position in the stack, and the end
31+
* of usable stack space.
32+
*
33+
* NOTE: This function will return a negative number to indicate a stack
34+
* overflow, i.e. a return value of -20 means you have overrun the allocated
35+
* stack area by 20 bytes. */
36+
int freeStack(void);
37+
38+
/* freeMemory: returns the combined free memory in both the stack and heap,
39+
* except in the case where a stack overflow has occurred (i.e. freeStack
40+
* returns a negative number). In this case, only the amount of free heap
41+
* space will be returned. */
42+
int freeMemory(void);
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif

0 commit comments

Comments
 (0)