Function to calculate the memory available for malloc

Dependents:   AvailableMemory_HelloWorld MCBBThermostat helloaabbc SP14P1_skeleton

Files at this revision

API Documentation at this revision

Comitter:
segundo
Date:
Sun Nov 07 19:11:52 2010 +0000
Child:
1:d8432d8a5b6d
Commit message:

Changed in this revision

AvailableMemory.cpp Show annotated file Show diff for this revision Revisions of this file
AvailableMemory.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AvailableMemory.cpp	Sun Nov 07 19:11:52 2010 +0000
@@ -0,0 +1,39 @@
+#include "AvailableMemory.h"
+#include <stdlib.h>
+
+namespace segundo {
+namespace Utilities {
+
+int AvailableMemory() {
+
+    return AvailableMemory(256, 0x8000, true);
+}
+
+int AvailableMemory(int resolution, int maximum, bool disableInterrupts) {
+
+    if (resolution < 1) resolution = 1;
+    if (maximum < 0) maximum = 0;
+
+    int low = 0;
+    int high = maximum + 1;
+
+    if (disableInterrupts) __disable_irq();
+
+    while (high - low > resolution) {
+        int mid = (low + high) / 2;
+        void* p = malloc(mid);
+        if (p == NULL) {
+            high = mid;
+        } else {
+            free(p);
+            low = mid;
+        }
+    }
+
+    if (disableInterrupts) __enable_irq();
+
+    return low;
+}
+
+} // namespace Utilities
+} // namespace segundo
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AvailableMemory.h	Sun Nov 07 19:11:52 2010 +0000
@@ -0,0 +1,15 @@
+#ifndef SEGUNDO_UTILITIES_AVAILABLEMEMORY_H
+#define SEGUNDO_UTILITIES_AVAILABLEMEMORY_H
+
+namespace segundo {
+namespace Utilities {
+
+int AvailableMemory();
+int AvailableMemory(int resolution, int maximum, bool disableInterrupts);
+
+} // namespace Utilities
+} // namespace segundo
+
+using namespace segundo::Utilities;
+
+#endif // SEGUNDO_UTILITIES_AVAILABLEMEMORY_H
\ No newline at end of file