From e728833cb33b4d5c3f62965c73dd75b5d37814c3 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Wed, 24 Jul 2019 23:01:42 +0200
Subject: [PATCH] fix(pycardium): Remove all uses of newlib malloc

Fixes #44

Signed-off-by: Rahix <rahix@rahix.de>
---
 pycardium/meson.build |  1 +
 pycardium/mphalport.c | 25 ++++-----------
 pycardium/patch.c     | 71 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+), 19 deletions(-)
 create mode 100644 pycardium/patch.c

diff --git a/pycardium/meson.build b/pycardium/meson.build
index 895be888e..2a1f08939 100644
--- a/pycardium/meson.build
+++ b/pycardium/meson.build
@@ -71,6 +71,7 @@ upy = static_library(
 elf = executable(
   name + '.elf',
   'main.c',
+  'patch.c',
   'mphalport.c',
   frozen_source,
   modsrc,
diff --git a/pycardium/mphalport.c b/pycardium/mphalport.c
index 95d4bffa9..ee9e2f44f 100644
--- a/pycardium/mphalport.c
+++ b/pycardium/mphalport.c
@@ -8,6 +8,7 @@
 #include "py/mpstate.h"
 #include "py/obj.h"
 #include "py/runtime.h"
+#include "py/mpprint.h"
 
 #include "mxc_delay.h"
 #include "max32665.h"
@@ -15,6 +16,7 @@
 
 #include "epicardium.h"
 #include "api/common.h"
+
 /******************************************************************************
  * Serial Communication
  */
@@ -36,30 +38,15 @@ int DEBUG_printf(const char *fmt, ...)
 {
 	va_list args;
 	va_start(args, fmt);
-	int ret = vprintf(fmt, args);
+	int ret = mp_vprintf(MP_PYTHON_PRINTER, fmt, args);
 	va_end(args);
 	return ret;
 }
 
-/* newlib syscall to allow printf to work */
-long _write(int fd, const char *buf, size_t cnt)
+void __attribute__((noreturn)) sbrk_is_not_implemented___see_issue_44(void);
+intptr_t _sbrk(int incr)
 {
-	/*
-	 * Only print one line at a time.  Insert `\r` between lines so
-	 * they are properly displayed on the serial console.
-	 */
-	size_t i, last = 0;
-	for (i = 0; i < cnt; i++) {
-		if (buf[i] == '\n') {
-			epic_uart_write_str(&buf[last], i - last);
-			epic_uart_write_str("\r", 1);
-			last = i;
-		}
-	}
-	if (last != i) {
-		epic_uart_write_str(&buf[last], cnt - last);
-	}
-	return cnt;
+	sbrk_is_not_implemented___see_issue_44();
 }
 
 void epic_isr_ctrl_c(void)
diff --git a/pycardium/patch.c b/pycardium/patch.c
new file mode 100644
index 000000000..902653ec5
--- /dev/null
+++ b/pycardium/patch.c
@@ -0,0 +1,71 @@
+#include "epicardium.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "py/mpprint.h"
+#include "py/mphal.h"
+
+#include <string.h>
+
+int printf(const char *fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	int ret = mp_vprintf(MP_PYTHON_PRINTER, fmt, ap);
+	va_end(ap);
+	return ret;
+}
+
+#ifdef putc
+#undef putc
+#endif /* putc */
+int putc(int c, FILE *f)
+{
+	char chr = (char)c;
+	mp_hal_stdout_tx_strn_cooked(&chr, 1);
+	return c;
+}
+
+#ifdef putchar
+#undef putchar
+#endif /* putchar */
+int putchar(int c)
+{
+	char chr = (char)c;
+	mp_hal_stdout_tx_strn_cooked(&chr, 1);
+	return c;
+}
+
+int puts(const char *s)
+{
+	int length = strlen(s);
+	if (length) {
+		mp_hal_stdout_tx_strn_cooked(s, length);
+	}
+	return length;
+}
+
+/* Used by mp_hal_move_cursor_back() */
+int snprintf(char *str, size_t size, const char *format, ...)
+{
+	/* TODO: What should we do with this? */
+	return -EIO;
+}
+
+/* Use by assert() */
+int fiprintf(FILE *f, const char *fmt, ...)
+{
+	/* TODO: Maybe printing everything to UART isn't the correct thing? */
+	va_list ap;
+	va_start(ap, fmt);
+	int ret = mp_vprintf(MP_PYTHON_PRINTER, fmt, ap);
+	va_end(ap);
+	return ret;
+}
+
+int raise(int sig)
+{
+	/* Ignore signals */
+	return 0;
+}
-- 
GitLab