From 965df7faefabdd5f571b5eb46d45470223c91e12 Mon Sep 17 00:00:00 2001
From: "Nicolas \"Pixel\" Noble" <pixel@nobis-crew.org>
Date: Mon, 24 Jan 2011 10:35:19 +0100
Subject: Adding sbrk, and fixing a couple of other makefile problems.

---
 os/src/sbrk.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 os/src/sbrk.c

(limited to 'os/src')

diff --git a/os/src/sbrk.c b/os/src/sbrk.c
new file mode 100644
index 0000000..3c674ba
--- /dev/null
+++ b/os/src/sbrk.c
@@ -0,0 +1,47 @@
+#include <reent.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include "FreeRTOS.h"
+#include "task.h"
+#include "mpu_wrappers.h"
+
+// Mostly stolen from mbed-freertos
+
+extern unsigned int __cs3_heap_start, __cs3_heap_end;
+
+/* Low-level bulk RAM allocator -- used by Newlib's Malloc */
+void *heap_end = NULL;
+PRIVILEGED_FUNCTION void *_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
+{
+    void *prev_heap_end, *next_heap_end, *ret;
+
+    taskENTER_CRITICAL();
+    {
+        /* Initialize on first call */
+        if (heap_end == NULL)
+        {
+            heap_end = (void *)&__cs3_heap_start;
+        }
+
+        prev_heap_end = heap_end;
+
+        /* Align to always be on 8-byte boundaries */
+        next_heap_end = (void *)((((unsigned int)heap_end + incr) + 7) & ~7);
+
+        /* Check if this allocation would exceed the end of the ram - would probably get into the stack first however */
+        if (next_heap_end > (void *)&__cs3_heap_end)
+        {
+            ptr->_errno = ENOMEM;
+            ret = NULL;
+        }
+        else
+        {
+            heap_end = next_heap_end;
+            ret = (void *)prev_heap_end;
+        }
+    }
+    taskEXIT_CRITICAL();
+    return ret;
+}
+
-- 
cgit v1.2.3