ref: e94611fde97ac59b94c43ba702dd3908962ec39f
parent: a673d0b0fe5be7a502cf309be5d95c462adbad50
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Mar 29 12:28:06 EDT 2022
libc: Readjust freep realloc() modifies the free list Freep can be pointing to the block that is modified by realloc(), so it is needed to set freep to some valid value (the actual value is not important).
--- a/src/libc/stdlib/malloc.c
+++ b/src/libc/stdlib/malloc.c
@@ -13,7 +13,7 @@
#define ERRADDR ((char *)-1)
static Header base = { .h.next = &base };
-static Header *freep = &base;
+Header *_freep = &base;
/*
* Run over the free list looking for the nearest previous
@@ -25,7 +25,7 @@
{
Header *p;
- for (p = freep; ;p = p->h.next) {
+ for (p = _freep; ;p = p->h.next) {
/* hp between p and p->h.next? */
if (p < hp && hp < p->h.next)
break;
@@ -69,7 +69,7 @@
prev->h.next = hp;
}
- freep = prev;
+ _freep = prev;
}
static void *
@@ -112,7 +112,7 @@
/* integrate new memory into the list */
free(hp + 1);
- return freep;
+ return _freep;
}
/*
@@ -142,7 +142,7 @@
/* 1 unit for header plus enough units to fit nbytes */
nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) + 1;
- for (prev = freep; ; prev = cur) {
+ for (prev = _freep; ; prev = cur) {
cur = prev->h.next;
if (cur->h.size >= nunits) {
if (cur->h.size == nunits) {
@@ -154,12 +154,12 @@
}
cur->h.next = NULL;
- freep = prev;
+ _freep = prev;
return cur + 1;
}
- if (cur == freep) {
+ if (cur == _freep) {
if ((cur = morecore(nunits)) == NULL)
return NULL;
}
--- a/src/libc/stdlib/malloc.h
+++ b/src/libc/stdlib/malloc.h
@@ -12,3 +12,5 @@
};
extern void *_prevchunk(Header *);
+
+extern Header *_freep;
--- a/src/libc/stdlib/realloc.c
+++ b/src/libc/stdlib/realloc.c
@@ -53,6 +53,7 @@
prev->h.next = new;
new->h.next = next->h.next;
new->h.size = avail - nunits;
+ _freep = new;
return ptr;
}
}