From 0c7b8be8e7d257f2f584121d97bfb899085aa350 Mon Sep 17 00:00:00 2001 From: quou Date: Tue, 31 Dec 2024 13:14:00 +1100 Subject: Fix heap allocator header alignment --- qstd/memory.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'qstd') diff --git a/qstd/memory.c b/qstd/memory.c index 1dac1c5..7d669d2 100644 --- a/qstd/memory.c +++ b/qstd/memory.c @@ -109,11 +109,13 @@ void* imp2_heap_alloc( phdr[0] |= f; return phdr + 1; } else { - int ns = bs - as; - if (ns > hs * 2) { - int* nhdr = (int*)&h->buf[o + ns]; - phdr[0] = ns; - nhdr[0] = as | f; + int ps = bs - as; + int aps = align_size(ps, hs) - hs; + if (aps > hs * 2) { + int* nhdr = (int*)&h->buf[o + aps]; + assert(aligned(nhdr, sizeof hs)); + phdr[0] = aps; + nhdr[0] = (as + (ps - aps)) | f; h->blocks++; return &nhdr[1]; } -- cgit v1.2.3-54-g00ecf