shithub: scc

Download patch

ref: f5cc8f6eeb677435da24372f738055728c9acb93
parent: 88281a76b99c430f41f651baaa6b53eef27fc07e
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Nov 21 13:27:11 EST 2021

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

--- 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,7 +44,7 @@
 void
 free(void *mem)
 {
-	Header *hp, *prev;
+	Header *hp, *prev, *next;
 
 	if (!mem)
 		return;
@@ -50,13 +51,14 @@
 
 	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 @@
 static Header *
 morecore(size_t nunits)
 {
-	char *rawmem;
+	void *rawmem;
 	Header *hp;
 
 	if (nunits < NALLOC)
@@ -104,7 +106,7 @@
 	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 @@
 	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 @@
 				cur += cur->h.size;
 				cur->h.size = nunits;
 			}
+
+			cur->h.next = NULL;
 			freep = prev;
+
 			return cur + 1;
 		}
 
--- a/src/libc/stdlib/realloc.c
+++ b/src/libc/stdlib/realloc.c
@@ -16,8 +16,8 @@
 	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;
@@ -24,43 +24,44 @@
 
 	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);