|
| 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 | +} |
0 commit comments