From 09ee83e34b3280d8876dd920aae877c3798a4a97 Mon Sep 17 00:00:00 2001 From: quou Date: Thu, 19 Dec 2024 19:09:36 +1100 Subject: fix the heap allocator --- qstd/memory.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'qstd') diff --git a/qstd/memory.c b/qstd/memory.c index 91e4a5b..f245289 100644 --- a/qstd/memory.c +++ b/qstd/memory.c @@ -95,24 +95,25 @@ void* imp2_heap_alloc( for (i = o = 0; i < h->blocks; i++) { int* phdr = (int*)&h->buf[o]; int hdr = *phdr, bs; - assert(aligned(phdr, sizeof hdr)); bs = hdr & ~f; + assert(bs); + assert(aligned(phdr, sizeof hdr)); if (~hdr & f) { if (as == bs) { - phdr[0] |= 1; + phdr[0] |= f; return phdr + 1; } else { int ns = bs - as; - if (ns > hs) { - int* nhdr = &phdr[ns / 4]; + if (ns > hs * 2) { + int* nhdr = (int*)&h->buf[o + ns]; phdr[0] = ns; nhdr[0] = as | f; h->blocks++; return &nhdr[1]; } } - } else - o += bs; + } + o += bs; } return 0; } -- cgit v1.2.3-54-g00ecf