Skip to content

Commit 195db42

Browse files
committed
fix: add BLOCK_OVERHEAD before round size
1 parent b745216 commit 195db42

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Diff for: std/assembly/rt/tlsf.ts

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

0 commit comments

Comments
 (0)