shithub: scc

Download patch

ref: 440e24ce81386865be595f5825bc7287d5063ccc
parent: faa82abe38fca557949e44a1745039e39ee5f232
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Sep 14 10:40:52 EDT 2021

libc: Fix _brk() in Darwin

Darwin lacks a brk() syscall, and the system code was implement
a very basic version using static memory. The code was using the
Linux prototype, while we moved to use the SUSv2 interface, that
just returns 0 or -1 in case of error. The implementation didn't
set the value of errno in case of error, that could generate
strange error diagnosis.

--- a/src/libc/arch/amd64/darwin/_getheap.c
+++ /dev/null
@@ -1,18 +1,0 @@
-static char heap[16384];
-
-void *
-_getheap(void)
-{
-	return heap;
-}
-
-void *
-_brk(void *addr)
-{
-	static char *cur = heap;
-	char *p = addr;
-
-	if (p < heap || p > &heap[sizeof(heap) - 1])
-		return (void *)-1;
-	return cur = p;
-}
--- /dev/null
+++ b/src/libc/arch/darwin/Makefile
@@ -1,0 +1,9 @@
+.POSIX:
+PROJECTDIR = ../../../..
+include $(PROJECTDIR)/scripts/rules.mk
+include ../../rules.mk
+
+OBJS=\
+	_getheap.$O\
+
+all: $(OBJS)
--- /dev/null
+++ b/src/libc/arch/darwin/_getheap.c
@@ -1,0 +1,31 @@
+#include <errno.h>
+#include <stddef.h>
+
+#include "../../syscall.h"
+
+static char heap[16384];
+
+/*
+ * TODO: Temporary solution until we implement mmap in Darwin
+ *       because it does not support any form of brk().
+ */
+void *
+_getheap(void)
+{
+	return heap;
+}
+
+int
+_brk(void *addr)
+{
+	static char *cur = heap;
+	char *p = addr;
+
+	if (p < heap || p > &heap[sizeof(heap) - 1]) {
+		errno = ENOMEM;
+		return -1;
+	}
+	cur = p;
+
+	return 0;
+}