scc

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

commit 5c36de466db99bd44b3ef29b5b8519db129531a3
parent 4e05de3bc5db833754c3b87dacecd190a253649c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 21 Nov 2021 18:27:11 +0100

libc: Do some stilistic changes to realloc() and malloc()

Diffstat:
Msrc/libc/stdlib/malloc.c | 21+++++++++++++--------
Msrc/libc/stdlib/realloc.c | 23++++++++++++-----------
2 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/src/libc/stdlib/malloc.c b/src/libc/stdlib/malloc.c @@ -7,6 +7,7 @@ #include "../libc.h" #undef malloc +#undef free #define MAXADDR ((char *)-1) #define ERRADDR ((char *)-1) @@ -43,20 +44,21 @@ _prevchunk(Header *hp) void free(void *mem) { - Header *hp, *prev; + Header *hp, *prev, *next; if (!mem) return; hp = (Header *) mem - 1; prev = _prevchunk(hp); + next = prev->h.next; /* join to next */ - if (hp + hp->h.size == prev->h.next) { - hp->h.size += prev->h.next->h.size; - hp->h.next = prev->h.next->h.next; + if (hp + hp->h.size == next) { + hp->h.size += next->h.size; + hp->h.next = next->h.next; } else { - hp->h.next = prev->h.next; + hp->h.next = next; } /* join to previous */ @@ -94,7 +96,7 @@ sbrk(uintptr_t inc) static Header * morecore(size_t nunits) { - char *rawmem; + void *rawmem; Header *hp; if (nunits < NALLOC) @@ -104,7 +106,7 @@ morecore(size_t nunits) if (rawmem == ERRADDR) return NULL; - hp = (Header*)rawmem; + hp = (Header *) rawmem; hp->h.size = nunits; /* integrate new memory into the list */ @@ -135,7 +137,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; @@ -147,7 +149,10 @@ malloc(size_t nbytes) cur += cur->h.size; cur->h.size = nunits; } + + cur->h.next = NULL; freep = prev; + return cur + 1; } diff --git a/src/libc/stdlib/realloc.c b/src/libc/stdlib/realloc.c @@ -16,51 +16,52 @@ realloc(void *ptr, size_t nbytes) if (!ptr) return malloc(nbytes); - nunits = (nbytes + sizeof(Header) - 1) / sizeof(Header) + 1; - oh = (Header*) ptr - 1; + nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) + 1; + oh = (Header *) ptr - 1; if (oh->h.size == nunits) return ptr; new = oh + nunits; - if (nunits < oh->h.size - 1) { + if (nunits < oh->h.size) { new->h.size = oh->h.size - nunits; oh->h.size = nunits; free(new + 1); - return oh + 1; + return ptr; } prev = _prevchunk(oh); + next = prev->h.next; - if (oh + oh->h.size == prev->h.next) { + if (oh + oh->h.size == next) { /* * if there is free space adjacent * to the current memory */ - next = prev->h.next; avail = oh->h.size + next->h.size; if (avail == nunits) { oh->h.size = nunits; prev->h.next = next->h.next; - return oh + 1; + return ptr; } - if (avail > nunits) { + if (nunits < avail) { oh->h.size = nunits; prev->h.next = new; new->h.next = next->h.next; new->h.size = avail - nunits; - return oh + 1; + return ptr; } } - onbytes = (oh->h.size - 1) * sizeof(Header); if ((new = malloc(nbytes)) == NULL) return NULL; - n = (onbytes > nbytes) ? nbytes : onbytes; + n = (oh->h.size - 1) * sizeof(Header); + if (n > nbytes) + n = nbytes; memcpy(new, ptr, n); free(ptr);