Skip to content

Commit 3969326

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 17d2e2f commit 3969326

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed
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) 2016 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

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#######################################
2+
# Syntax Coloring Map For MemoryFree
3+
#######################################
4+
5+
#######################################
6+
# Methods and Functions (KEYWORD2)
7+
#######################################
8+
9+
freeMemory KEYWORD2
+10
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+
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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) 2015 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+
31+
extern char __start_heap;
32+
extern char __end_heap;
33+
extern char __stack_size;
34+
extern char __stack_start;
35+
36+
int freeStack() {
37+
int stack_end;
38+
int mark;
39+
40+
stack_end = ((int)&__stack_start) - ((int)&__stack_size);
41+
return ((int)&mark) - stack_end;
42+
}
43+
44+
int freeHeap (void) {
45+
int hsize;
46+
struct mallinfo mi;
47+
48+
mi = mallinfo();
49+
hsize = (int)&__end_heap - (int)&__start_heap;
50+
return (hsize - mi.arena) + mi.fordblks;
51+
}
52+
53+
int freeMemory (void) {
54+
int heap = freeHeap();
55+
int stack = freeStack();
56+
return (stack < 0) ? heap : stack + heap;
57+
}

libraries/MemoryFree/src/MemoryFree.h

+48
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)