Resolve hisenbugs regarding GCC and -Os

This commit is contained in:
2025-09-17 21:48:16 +02:00
parent 0a5523f234
commit 91e65bb35a
13 changed files with 74 additions and 93 deletions

View File

@ -22,6 +22,7 @@
#define LACKS_STRINGS_H 1
#define LACKS_SCHED_H 1
#define HAVE_MMAP 0
#define MORECORE_CONTIGUOUS 0
#define ABORT \
do { \
uprintf("dlmalloc: Aborting...\n"); \
@ -54,50 +55,28 @@ static MLOCK_T malloc_global_mutex = { 0 };
#define PAGE_SIZE 0x1000
static uint8_t *heap_start = NULL;
static uint8_t *heap_end = NULL;
static uint8_t *heap_commit = NULL;
static size_t _roundpage(size_t sz) {
return (sz + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
}
void *_last = 0;
void *sbrk(ptrdiff_t inc) {
if (!heap_end) {
size_t allocsz = PAGE_SIZE;
uint8_t *maddr = NULL;
int32_t ret = mman_map(NULL, allocsz, MMAN_MAP_PF_RW, 0, &maddr);
if (ret != E_OK || maddr == NULL) {
return (void *)-1;
}
heap_start = maddr;
heap_end = maddr;
heap_commit = maddr + allocsz;
if (inc < 0) {
return 0;
}
if (!inc) {
return _last;
}
if (inc == 0) {
return heap_end;
uint64_t pages = _roundpage(inc);
uint8_t *maddr = NULL;
int32_t ret = mman_map(NULL, pages, MMAN_MAP_PF_RW, 0, &maddr);
if (ret != E_OK) {
return 0;
}
uint8_t *oldbrk = heap_end;
uint8_t *newbrk = heap_end + inc;
if (newbrk < heap_start) {
return (void *)-1;
}
if (inc > 0) {
if (newbrk > heap_commit) {
size_t need = (size_t)(newbrk - heap_commit);
size_t extra = _roundpage(need);
uint8_t *maddr = NULL;
int32_t ret = mman_map(NULL, extra, MMAN_MAP_PF_RW, 0, &maddr);
if (ret != E_OK || maddr == NULL) {
return (void *)-1;
}
heap_commit += extra;
}
}
heap_end = newbrk;
return (void *)oldbrk;
string_memset(maddr, 0, pages);
_last = (void *)(maddr + inc);
return maddr;
}