Skip to content

Commit 382aabe

Browse files
authoredJul 18, 2023
fix: Add BLOCK_OVERHEAD before round size (#2652)
1 parent a0c27fa commit 382aabe

File tree

153 files changed

+4712
-4302
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+4712
-4302
lines changed
 

‎std/assembly/rt/tlsf.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,15 @@ function removeBlock(root: Root, block: Block): void {
307307
// must perform those updates.
308308
}
309309

310+
function roundSize(size: usize): usize {
311+
const halfMaxSize = BLOCK_MAXSIZE >> 1; // don't round last fl
312+
const inv: usize = sizeof<usize>() * 8 - 1;
313+
const invRound = inv - SL_BITS;
314+
return size < halfMaxSize
315+
? size + (1 << (invRound - clz<usize>(size))) - 1
316+
: size;
317+
}
318+
310319
/** Searches for a free block of at least the specified size. */
311320
function searchBlock(root: Root, size: usize): Block | null {
312321
// size was already asserted by caller
@@ -317,13 +326,8 @@ function searchBlock(root: Root, size: usize): Block | null {
317326
fl = 0;
318327
sl = <u32>(size >> AL_BITS);
319328
} else {
320-
const halfMaxSize = BLOCK_MAXSIZE >> 1; // don't round last fl
321-
const inv: usize = sizeof<usize>() * 8 - 1;
322-
const invRound = inv - SL_BITS;
323-
let requestSize = size < halfMaxSize
324-
? size + (1 << (invRound - clz<usize>(size))) - 1
325-
: size;
326-
fl = inv - clz<usize>(requestSize);
329+
const requestSize = roundSize(size);
330+
fl = sizeof<usize>() * 8 - 1 - clz<usize>(requestSize);
327331
sl = <u32>((requestSize >> (fl - SL_BITS)) ^ (1 << SL_BITS));
328332
fl -= SB_BITS - 1;
329333
}
@@ -428,10 +432,8 @@ function growMemory(root: Root, size: usize): void {
428432
return;
429433
}
430434
// Here, both rounding performed in searchBlock ...
431-
const halfMaxSize = BLOCK_MAXSIZE >> 1;
432-
if (size < halfMaxSize) { // don't round last fl
433-
const invRound = (sizeof<usize>() * 8 - 1) - SL_BITS;
434-
size += (1 << (invRound - clz<usize>(size))) - 1;
435+
if (size >= SB_SIZE) {
436+
size = roundSize(size);
435437
}
436438
// and additional BLOCK_OVERHEAD must be taken into account. If we are going
437439
// to merge with the tail block, that's one time, otherwise it's two times.

‎tests/compiler/bindings/esm.debug.wat

+16-16
Original file line numberDiff line numberDiff line change
@@ -2026,22 +2026,6 @@
20262026
(local $pagesAfter i32)
20272027
i32.const 0
20282028
drop
2029-
local.get $size
2030-
i32.const 536870910
2031-
i32.lt_u
2032-
if
2033-
local.get $size
2034-
i32.const 1
2035-
i32.const 27
2036-
local.get $size
2037-
i32.clz
2038-
i32.sub
2039-
i32.shl
2040-
i32.const 1
2041-
i32.sub
2042-
i32.add
2043-
local.set $size
2044-
end
20452029
memory.size $0
20462030
local.set $pagesBefore
20472031
local.get $size
@@ -2063,6 +2047,22 @@
20632047
i32.add
20642048
local.set $size
20652049
local.get $size
2050+
i32.const 536870910
2051+
i32.lt_u
2052+
if
2053+
local.get $size
2054+
i32.const 1
2055+
i32.const 27
2056+
local.get $size
2057+
i32.clz
2058+
i32.sub
2059+
i32.shl
2060+
i32.const 1
2061+
i32.sub
2062+
i32.add
2063+
local.set $size
2064+
end
2065+
local.get $size
20662066
i32.const 65535
20672067
i32.add
20682068
i32.const 65535

0 commit comments

Comments
 (0)
Please sign in to comment.