summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-12-19 19:09:36 +1100
committerquou <quou@disroot.org>2024-12-19 19:09:36 +1100
commit09ee83e34b3280d8876dd920aae877c3798a4a97 (patch)
treeb9cd9d4780b723b0ecdfc86bb0f82dcb2830d65c
parent1b8008c41a04ceeb3ac4970f469ce9420ec29241 (diff)
fix the heap allocator
-rw-r--r--qstd/memory.c13
1 files changed, 7 insertions, 6 deletions
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;
}