summaryrefslogtreecommitdiff
path: root/qstd/memory.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-12-31 13:14:00 +1100
committerquou <quou@disroot.org>2024-12-31 13:14:00 +1100
commit0c7b8be8e7d257f2f584121d97bfb899085aa350 (patch)
tree95b13757e2440b1e9b0daf765b3d6d49a79b029b /qstd/memory.c
parent0f0167c0211acd69c104ab6c7c1caa8fa85d7e4f (diff)
Fix heap allocator header alignment
Diffstat (limited to 'qstd/memory.c')
-rw-r--r--qstd/memory.c12
1 files changed, 7 insertions, 5 deletions
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];
}