scc

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

commit 28f27fbca32a4e2b6576f514ee960e3638bb2184
parent 4fdfc69873149fd2ee9faf7fb85ea73e41254cb1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 27 Aug 2021 11:44:57 +0200

libc: Update stdlib to last version

This code is being updated out of the tree of scc and
it is time to synchroniza both copies now.

Diffstat:
Msrc/libc/libc.h | 5+++++
Asrc/libc/stdlib/_Exit.c | 11+++++++++++
Msrc/libc/stdlib/abort.c | 1+
Msrc/libc/stdlib/atexit.c | 6+++---
Msrc/libc/stdlib/errno.c | 2++
Msrc/libc/stdlib/exit.c | 4++++
Msrc/libc/stdlib/malloc.c | 7++++++-
Msrc/libc/stdlib/malloc.h | 4+---
8 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/src/libc/libc.h b/src/libc/libc.h @@ -28,6 +28,8 @@ struct tzone { }; extern void *_getheap(void); +extern int _dtoi(char c); + #ifdef stdin extern int _allocbuf(FILE *); @@ -51,3 +53,6 @@ extern int _tzjulian; extern int _daysmon[12]; extern char *_tzname[2]; extern struct tzone tzones[]; + +extern void (*_exitf[_ATEXIT_MAX])(void); +extern unsigned _exitn; diff --git a/src/libc/stdlib/_Exit.c b/src/libc/stdlib/_Exit.c @@ -0,0 +1,11 @@ +#include <stdlib.h> + +#include "../syscall.h" + +#undef _Exit + +void +_Exit(int status) +{ + _exit(status); +} diff --git a/src/libc/stdlib/abort.c b/src/libc/stdlib/abort.c @@ -1,5 +1,6 @@ #include <signal.h> #include <stdlib.h> + #undef abort void diff --git a/src/libc/stdlib/atexit.c b/src/libc/stdlib/atexit.c @@ -1,9 +1,7 @@ #include <stdlib.h> #include <errno.h> -#undef atexit -extern void (*_exitf[_ATEXIT_MAX])(void); -extern unsigned _exitn; +#undef atexit int atexit(void (*fun)(void)) @@ -12,6 +10,8 @@ atexit(void (*fun)(void)) errno = ENOMEM; return -1; } + _exitf[_exitn++] = fun; + return 0; } diff --git a/src/libc/stdlib/errno.c b/src/libc/stdlib/errno.c @@ -1 +1,3 @@ +#include <errno.h> + int errno; diff --git a/src/libc/stdlib/exit.c b/src/libc/stdlib/exit.c @@ -1,4 +1,7 @@ #include <stdlib.h> + +#include "../libc.h" + #undef exit void (*_exitf[_ATEXIT_MAX])(void); @@ -9,5 +12,6 @@ exit(int status) { while (_exitn > 0) (*_exitf[--_exitn])(); + _Exit(status); } diff --git a/src/libc/stdlib/malloc.c b/src/libc/stdlib/malloc.c @@ -7,6 +7,8 @@ #include "../syscall.h" #include "../libc.h" +#undef malloc + #define MAXADDR ((char *)-1) #define ERRADDR ((char *)-1) @@ -27,6 +29,7 @@ _prevchunk(Header *hp) /* hp between p and p->h.next? */ if (p < hp && hp < p->h.next) break; + /* p before hp and hp at the end of list? */ if (p->h.next <= p && (hp < p->h.next || hp > p)) break; @@ -77,9 +80,11 @@ sbrk(uintptr_t inc) if (!heap) heap = _getheap(); + old = heap; if (old >= MAXADDR - inc) return ERRADDR; + new = old + inc; p = _brk(new); if (p == old || p == ERRADDR) @@ -133,7 +138,7 @@ malloc(size_t nbytes) size_t nunits; /* 1 unit for header plus enough units to fit nbytes */ - nunits = (nbytes+sizeof(Header)-1) / sizeof(Header) + 1; + nunits = (nbytes+sizeof(Header)-1) / sizeof(Header)+1; for (prev = freep; ; prev = cur) { cur = prev->h.next; diff --git a/src/libc/stdlib/malloc.h b/src/libc/stdlib/malloc.h @@ -1,5 +1,3 @@ -#include <stdlib.h> - /* minimum amount of required units */ #define NALLOC 16 @@ -13,4 +11,4 @@ union header { _ALIGNTYPE most; }; -extern void *_prevchunk(Header *hp); +extern void *_prevchunk(Header *);