scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 65ca72ef8bc98b2ebb229dbddf1b28c294bb313f
parent 648ccc16cf9179e23f9a90c41598d7ccbb2e4c88
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 14 Sep 2021 14:40:52 +0200

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.

Diffstat:
Dsrc/libc/arch/amd64/darwin/_getheap.c | 18------------------
Asrc/libc/arch/darwin/Makefile | 9+++++++++
Asrc/libc/arch/darwin/_getheap.c | 31+++++++++++++++++++++++++++++++
3 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/src/libc/arch/amd64/darwin/_getheap.c b/src/libc/arch/amd64/darwin/_getheap.c @@ -1,18 +0,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; -} diff --git a/src/libc/arch/darwin/Makefile b/src/libc/arch/darwin/Makefile @@ -0,0 +1,9 @@ +.POSIX: +PROJECTDIR = ../../../.. +include $(PROJECTDIR)/scripts/rules.mk +include ../../rules.mk + +OBJS=\ + _getheap.$O\ + +all: $(OBJS) diff --git a/src/libc/arch/darwin/_getheap.c b/src/libc/arch/darwin/_getheap.c @@ -0,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; +}