Revised test using private new/delete methods suggested in http://mbed.org/forum/mbed/post/24686/

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Wed Jun 11 10:59:02 2014 +0000
Commit message:
Added custom new/delete methods suggested in http://mbed.org/forum/mbed/post/24686/

Changed in this revision

Utility.cpp Show annotated file Show diff for this revision Revisions of this file
Utility.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Utility.cpp	Wed Jun 11 10:59:02 2014 +0000
@@ -0,0 +1,22 @@
+
+#include "Utility.h"
+
+uint32_t Free() {
+    uint32_t max = 50000;
+    uint32_t x = max / 2;
+    uint32_t min = 0;
+
+    __disable_irq();
+    while (min < max-1) {
+        void * p = malloc(x);
+        if (p) {
+            free(p);
+            min = x;
+        } else {
+            max = x;
+        }
+        x = (max + min)/2;
+    }
+    __enable_irq();
+    return(x);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Utility.h	Wed Jun 11 10:59:02 2014 +0000
@@ -0,0 +1,3 @@
+#include "mbed.h"
+
+uint32_t Free(void);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jun 11 10:59:02 2014 +0000
@@ -0,0 +1,56 @@
+#include "mbed.h"   // blinky starts with mbed lib 76, however this was updated to lib 81
+#include "Utility.h"
+//#include "RawSerial.h"
+
+Serial pc(USBTX, USBRX);
+DigitalOut myled(LED1);
+extern "C" void mbed_reset();
+
+void* operator new(size_t size) throw()
+{   
+    void *ptr;
+ 
+    ptr = malloc(size);
+    return ptr;
+}
+ 
+void operator delete(void *ptr) throw()
+{
+   free(ptr);
+}
+ 
+void operator delete[](void *ptr) throw()
+{
+   free(ptr);
+}
+
+int main() {
+    //pc.baud(460800);    // I like a snappy terminal, so crank it up!
+    int f = Free();
+    pc.printf("\r\nBlinky eating memory - Build " __DATE__ " " __TIME__ "\r\n");
+    pc.printf("  %d bytes free\r\n", f);
+    int i = 2000;
+    int total = 0;
+    
+    while(1) {
+        myled = 1;
+        wait(0.2);
+        pc.printf("Allocating %d bytes in addition to %d already allocated.\r\n", i, total);
+        char * p = new char [i];      // This never returns after 24000
+        //char * p = (char *)malloc(i);     // This fails gracefully with NULL return after 24000
+        pc.printf("  %d bytes free.\r\n", Free());
+        __heapvalid((__heapprt)fprintf, stdout, 0);
+        if (p) {
+            total += i;
+            pc.printf("  Success! \r\n");
+        } else {
+            pc.printf("  Failed...\r\n");
+            break;
+        }
+        myled = 0;
+        wait(0.2);
+    }
+    pc.printf("done.\r\n");
+    while (!pc.readable()) {}
+    mbed_reset();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Jun 11 10:59:02 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/0b3ab51c8877
\ No newline at end of file