Skip to content

Commit

Permalink
fix: Add BLOCK_OVERHEAD before round size (AssemblyScript#2652)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrCai0907 committed Jul 18, 2023
1 parent a0c27fa commit 382aabe
Show file tree
Hide file tree
Showing 153 changed files with 4,712 additions and 4,302 deletions.
24 changes: 13 additions & 11 deletions std/assembly/rt/tlsf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ function removeBlock(root: Root, block: Block): void {
// must perform those updates.
}

function roundSize(size: usize): usize {
const halfMaxSize = BLOCK_MAXSIZE >> 1; // don't round last fl
const inv: usize = sizeof<usize>() * 8 - 1;
const invRound = inv - SL_BITS;
return size < halfMaxSize
? size + (1 << (invRound - clz<usize>(size))) - 1
: size;
}

/** Searches for a free block of at least the specified size. */
function searchBlock(root: Root, size: usize): Block | null {
// size was already asserted by caller
Expand All @@ -317,13 +326,8 @@ function searchBlock(root: Root, size: usize): Block | null {
fl = 0;
sl = <u32>(size >> AL_BITS);
} else {
const halfMaxSize = BLOCK_MAXSIZE >> 1; // don't round last fl
const inv: usize = sizeof<usize>() * 8 - 1;
const invRound = inv - SL_BITS;
let requestSize = size < halfMaxSize
? size + (1 << (invRound - clz<usize>(size))) - 1
: size;
fl = inv - clz<usize>(requestSize);
const requestSize = roundSize(size);
fl = sizeof<usize>() * 8 - 1 - clz<usize>(requestSize);
sl = <u32>((requestSize >> (fl - SL_BITS)) ^ (1 << SL_BITS));
fl -= SB_BITS - 1;
}
Expand Down Expand Up @@ -428,10 +432,8 @@ function growMemory(root: Root, size: usize): void {
return;
}
// Here, both rounding performed in searchBlock ...
const halfMaxSize = BLOCK_MAXSIZE >> 1;
if (size < halfMaxSize) { // don't round last fl
const invRound = (sizeof<usize>() * 8 - 1) - SL_BITS;
size += (1 << (invRound - clz<usize>(size))) - 1;
if (size >= SB_SIZE) {
size = roundSize(size);
}
// and additional BLOCK_OVERHEAD must be taken into account. If we are going
// to merge with the tail block, that's one time, otherwise it's two times.
Expand Down
32 changes: 16 additions & 16 deletions tests/compiler/bindings/esm.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2026,22 +2026,6 @@
(local $pagesAfter i32)
i32.const 0
drop
local.get $size
i32.const 536870910
i32.lt_u
if
local.get $size
i32.const 1
i32.const 27
local.get $size
i32.clz
i32.sub
i32.shl
i32.const 1
i32.sub
i32.add
local.set $size
end
memory.size $0
local.set $pagesBefore
local.get $size
Expand All @@ -2063,6 +2047,22 @@
i32.add
local.set $size
local.get $size
i32.const 536870910
i32.lt_u
if
local.get $size
i32.const 1
i32.const 27
local.get $size
i32.clz
i32.sub
i32.shl
i32.const 1
i32.sub
i32.add
local.set $size
end
local.get $size
i32.const 65535
i32.add
i32.const 65535
Expand Down
59 changes: 31 additions & 28 deletions tests/compiler/bindings/esm.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@
call $~lib/rt/tlsf/initialize
end
global.get $~lib/rt/tlsf/ROOT
local.set $4
local.set $3
local.get $0
i32.const 16
i32.add
Expand All @@ -1401,7 +1401,7 @@
call $~lib/builtins/abort
unreachable
end
local.get $4
local.get $3
i32.const 12
local.get $2
i32.const 19
Expand All @@ -1421,8 +1421,9 @@
if
memory.size $0
local.tee $2
local.get $5
i32.const 4
local.get $4
local.get $3
i32.load $0 offset=1568
local.get $2
i32.const 16
Expand All @@ -1431,46 +1432,48 @@
i32.sub
i32.ne
i32.shl
local.get $5
i32.const 1
i32.const 27
local.get $5
i32.clz
i32.sub
i32.shl
i32.const 1
i32.sub
i32.add
local.get $5
local.get $5
local.tee $4
i32.const 536870910
i32.lt_u
select
i32.add
if (result i32)
local.get $4
i32.const 1
i32.const 27
local.get $4
i32.clz
i32.sub
i32.shl
i32.const 1
i32.sub
i32.add
else
local.get $4
end
i32.const 65535
i32.add
i32.const -65536
i32.and
i32.const 16
i32.shr_u
local.tee $3
local.tee $4
local.get $2
local.get $3
local.get $4
i32.gt_s
select
memory.grow $0
i32.const 0
i32.lt_s
if
local.get $3
local.get $4
memory.grow $0
i32.const 0
i32.lt_s
if
unreachable
end
end
local.get $4
local.get $3
local.get $2
i32.const 16
i32.shl
Expand All @@ -1479,7 +1482,7 @@
i64.const 16
i64.shl
call $~lib/rt/tlsf/addMemory
local.get $4
local.get $3
local.get $5
call $~lib/rt/tlsf/searchBlock
local.tee $2
Expand Down Expand Up @@ -1507,12 +1510,12 @@
call $~lib/builtins/abort
unreachable
end
local.get $4
local.get $3
local.get $2
call $~lib/rt/tlsf/removeBlock
local.get $2
i32.load $0
local.set $3
local.set $4
local.get $5
i32.const 4
i32.add
Expand All @@ -1526,7 +1529,7 @@
call $~lib/builtins/abort
unreachable
end
local.get $3
local.get $4
i32.const -4
i32.and
local.get $5
Expand All @@ -1537,7 +1540,7 @@
if
local.get $2
local.get $5
local.get $3
local.get $4
i32.const 2
i32.and
i32.or
Expand All @@ -1547,19 +1550,19 @@
i32.add
local.get $5
i32.add
local.tee $3
local.tee $4
local.get $6
i32.const 4
i32.sub
i32.const 1
i32.or
i32.store $0
local.get $4
local.get $3
local.get $4
call $~lib/rt/tlsf/insertBlock
else
local.get $2
local.get $3
local.get $4
i32.const -2
i32.and
i32.store $0
Expand Down
Loading

0 comments on commit 382aabe

Please sign in to comment.