summaryrefslogtreecommitdiff
path: root/code/fe310/gloss/sys_sbrk.c
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2019-12-04 06:11:35 +0100
committerUros Majstorovic <majstor@majstor.org>2019-12-04 06:11:35 +0100
commit31578e285a21a749a49e3ac146feb8b02fcc7b52 (patch)
treee67f619360352a87fb6e0f410f5246468fbc1073 /code/fe310/gloss/sys_sbrk.c
parent2c981aec5e5c10f9fd036dfb48105b16f16e4233 (diff)
added new metal sdk
Diffstat (limited to 'code/fe310/gloss/sys_sbrk.c')
-rw-r--r--code/fe310/gloss/sys_sbrk.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/code/fe310/gloss/sys_sbrk.c b/code/fe310/gloss/sys_sbrk.c
new file mode 100644
index 0000000..cc01c8f
--- /dev/null
+++ b/code/fe310/gloss/sys_sbrk.c
@@ -0,0 +1,39 @@
+#include <sys/types.h>
+
+/* brk is handled entirely within the C library. This limits METAL programs that
+ * use the C library to be disallowed from dynamically allocating memory
+ * without talking to the C library, but that sounds like a sane way to go
+ * about it. Note that there is no error checking anywhere in this file, users
+ * will simply get the relevant error when actually trying to use the memory
+ * that's been allocated. */
+extern char metal_segment_heap_target_start;
+extern char metal_segment_heap_target_end;
+static char *brk = &metal_segment_heap_target_start;
+
+int
+_brk(void *addr)
+{
+ brk = addr;
+ return 0;
+}
+
+char *
+_sbrk(ptrdiff_t incr)
+{
+ char *old = brk;
+
+ /* If __heap_size == 0, we can't allocate memory on the heap */
+ if(&metal_segment_heap_target_start == &metal_segment_heap_target_end) {
+ return (void *)-1;
+ }
+
+ /* Don't move the break past the end of the heap */
+ if ((brk + incr) < &metal_segment_heap_target_end) {
+ brk += incr;
+ } else {
+ brk = &metal_segment_heap_target_end;
+ return (void *)-1;
+ }
+
+ return old;
+}