Skip to content

Commit 7b7bf0f

Browse files
committed
fix: add BLOCK_OVERHEAD before round size
1 parent a0c27fa commit 7b7bf0f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

std/assembly/rt/tlsf.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,16 +427,16 @@ function growMemory(root: Root, size: usize): void {
427427
unreachable();
428428
return;
429429
}
430+
// and additional BLOCK_OVERHEAD must be taken into account. If we are going
431+
// to merge with the tail block, that's one time, otherwise it's two times.
432+
let pagesBefore = memory.size();
433+
size += BLOCK_OVERHEAD << usize((<usize>pagesBefore << 16) - BLOCK_OVERHEAD != changetype<usize>(GETTAIL(root)));
430434
// Here, both rounding performed in searchBlock ...
431435
const halfMaxSize = BLOCK_MAXSIZE >> 1;
432436
if (size < halfMaxSize) { // don't round last fl
433437
const invRound = (sizeof<usize>() * 8 - 1) - SL_BITS;
434-
size += (1 << (invRound - clz<usize>(size))) - 1;
438+
size += (1 << (invRound - clz<usize>(size))) - 1; // size should be larger than 15
435439
}
436-
// and additional BLOCK_OVERHEAD must be taken into account. If we are going
437-
// to merge with the tail block, that's one time, otherwise it's two times.
438-
let pagesBefore = memory.size();
439-
size += BLOCK_OVERHEAD << usize((<usize>pagesBefore << 16) - BLOCK_OVERHEAD != changetype<usize>(GETTAIL(root)));
440440
let pagesNeeded = <i32>(((size + 0xffff) & ~0xffff) >>> 16);
441441
let pagesWanted = max(pagesBefore, pagesNeeded); // double memory
442442
if (memory.grow(pagesWanted) < 0) {

0 commit comments

Comments
 (0)