Skip to content

Commit e5890b1

Browse files
committed
Add an optional handler for detecting memory.grow() use in the runtime
This handler can be used with an import for users not using ESM bindings to recreate views into memory only when necessary.
1 parent 1126ef1 commit e5890b1

File tree

165 files changed

+6118
-1436
lines changed

Some content is hidden

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

165 files changed

+6118
-1436
lines changed

Diff for: std/assembly/rt/index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ declare function __finalize(ptr: usize): void;
2929
declare const ASC_RTRACE: bool;
3030
declare const ASC_PROFILE: bool;
3131

32+
// Memory growth handling
33+
declare function ASC_RT_GROW_HANDLER(): void;
34+
3235
// Incremental GC constants
3336
declare const ASC_GC_GRANULARITY: i32;
3437
declare const ASC_GC_SWEEPFACTOR: i32;

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

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function maybeGrowMemory(newOffset: usize): void {
1818
if (memory.grow(pagesWanted) < 0) {
1919
if (memory.grow(pagesNeeded) < 0) unreachable(); // out of memory
2020
}
21+
if (isDefined(ASC_RT_GROW_HANDLER)) ASC_RT_GROW_HANDLER();
2122
}
2223
offset = newOffset;
2324
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ function growMemory(root: Root, size: usize): void {
444444
if (memory.grow(pagesWanted) < 0) {
445445
if (memory.grow(pagesNeeded) < 0) unreachable();
446446
}
447+
if (isDefined(ASC_RT_GROW_HANDLER)) ASC_RT_GROW_HANDLER();
447448
let pagesAfter = memory.size();
448449
addMemory(root, <usize>pagesBefore << 16, <u64>pagesAfter << 16);
449450
}
@@ -468,7 +469,10 @@ function initialize(): void {
468469
let rootOffset = (__heap_base + AL_MASK) & ~AL_MASK;
469470
let pagesBefore = memory.size();
470471
let pagesNeeded = <i32>((((rootOffset + ROOT_SIZE) + 0xffff) & ~0xffff) >>> 16);
471-
if (pagesNeeded > pagesBefore && memory.grow(pagesNeeded - pagesBefore) < 0) unreachable();
472+
if (pagesNeeded > pagesBefore) {
473+
if (memory.grow(pagesNeeded - pagesBefore) < 0) unreachable();
474+
if (isDefined(ASC_RT_GROW_HANDLER)) ASC_RT_GROW_HANDLER();
475+
}
472476
let root = changetype<Root>(rootOffset);
473477
root.flMap = 0;
474478
SETTAIL(root, changetype<Block>(0));

Diff for: tests/compiler/bindings/esm.debug.wat

+11-9
Original file line numberDiff line numberDiff line change
@@ -1338,18 +1338,18 @@
13381338
local.get $pagesNeeded
13391339
local.get $pagesBefore
13401340
i32.gt_s
1341-
if (result i32)
1341+
if
13421342
local.get $pagesNeeded
13431343
local.get $pagesBefore
13441344
i32.sub
13451345
memory.grow $0
13461346
i32.const 0
13471347
i32.lt_s
1348-
else
1348+
if
1349+
unreachable
1350+
end
13491351
i32.const 0
1350-
end
1351-
if
1352-
unreachable
1352+
drop
13531353
end
13541354
local.get $rootOffset
13551355
local.set $root
@@ -1470,7 +1470,7 @@
14701470
if
14711471
i32.const 0
14721472
i32.const 672
1473-
i32.const 562
1473+
i32.const 566
14741474
i32.const 3
14751475
call $~lib/builtins/abort
14761476
unreachable
@@ -1799,7 +1799,7 @@
17991799
if
18001800
i32.const 336
18011801
i32.const 672
1802-
i32.const 461
1802+
i32.const 462
18031803
i32.const 29
18041804
call $~lib/builtins/abort
18051805
unreachable
@@ -2095,6 +2095,8 @@
20952095
unreachable
20962096
end
20972097
end
2098+
i32.const 0
2099+
drop
20982100
memory.size $0
20992101
local.set $pagesAfter
21002102
local.get $root
@@ -2244,7 +2246,7 @@
22442246
if
22452247
i32.const 0
22462248
i32.const 672
2247-
i32.const 499
2249+
i32.const 503
22482250
i32.const 16
22492251
call $~lib/builtins/abort
22502252
unreachable
@@ -2264,7 +2266,7 @@
22642266
if
22652267
i32.const 0
22662268
i32.const 672
2267-
i32.const 501
2269+
i32.const 505
22682270
i32.const 14
22692271
call $~lib/builtins/abort
22702272
unreachable

Diff for: tests/compiler/bindings/esm.release.wat

+8-10
Original file line numberDiff line numberDiff line change
@@ -852,18 +852,16 @@
852852
local.tee $1
853853
i32.const 0
854854
i32.le_s
855-
if (result i32)
855+
if
856856
i32.const 1
857857
local.get $1
858858
i32.sub
859859
memory.grow $0
860860
i32.const 0
861861
i32.lt_s
862-
else
863-
i32.const 0
864-
end
865-
if
866-
unreachable
862+
if
863+
unreachable
864+
end
867865
end
868866
i32.const 35056
869867
i32.const 0
@@ -1161,7 +1159,7 @@
11611159
if
11621160
i32.const 0
11631161
i32.const 1696
1164-
i32.const 562
1162+
i32.const 566
11651163
i32.const 3
11661164
call $~lib/builtins/abort
11671165
unreachable
@@ -1396,7 +1394,7 @@
13961394
if
13971395
i32.const 1360
13981396
i32.const 1696
1399-
i32.const 461
1397+
i32.const 462
14001398
i32.const 29
14011399
call $~lib/builtins/abort
14021400
unreachable
@@ -1494,7 +1492,7 @@
14941492
if
14951493
i32.const 0
14961494
i32.const 1696
1497-
i32.const 499
1495+
i32.const 503
14981496
i32.const 16
14991497
call $~lib/builtins/abort
15001498
unreachable
@@ -1509,7 +1507,7 @@
15091507
if
15101508
i32.const 0
15111509
i32.const 1696
1512-
i32.const 501
1510+
i32.const 505
15131511
i32.const 14
15141512
call $~lib/builtins/abort
15151513
unreachable

Diff for: tests/compiler/bindings/noExportRuntime.debug.wat

+11-9
Original file line numberDiff line numberDiff line change
@@ -1253,18 +1253,18 @@
12531253
local.get $pagesNeeded
12541254
local.get $pagesBefore
12551255
i32.gt_s
1256-
if (result i32)
1256+
if
12571257
local.get $pagesNeeded
12581258
local.get $pagesBefore
12591259
i32.sub
12601260
memory.grow $0
12611261
i32.const 0
12621262
i32.lt_s
1263-
else
1263+
if
1264+
unreachable
1265+
end
12641266
i32.const 0
1265-
end
1266-
if
1267-
unreachable
1267+
drop
12681268
end
12691269
local.get $rootOffset
12701270
local.set $root
@@ -1385,7 +1385,7 @@
13851385
if
13861386
i32.const 0
13871387
i32.const 512
1388-
i32.const 562
1388+
i32.const 566
13891389
i32.const 3
13901390
call $~lib/builtins/abort
13911391
unreachable
@@ -1714,7 +1714,7 @@
17141714
if
17151715
i32.const 176
17161716
i32.const 512
1717-
i32.const 461
1717+
i32.const 462
17181718
i32.const 29
17191719
call $~lib/builtins/abort
17201720
unreachable
@@ -2010,6 +2010,8 @@
20102010
unreachable
20112011
end
20122012
end
2013+
i32.const 0
2014+
drop
20132015
memory.size $0
20142016
local.set $pagesAfter
20152017
local.get $root
@@ -2159,7 +2161,7 @@
21592161
if
21602162
i32.const 0
21612163
i32.const 512
2162-
i32.const 499
2164+
i32.const 503
21632165
i32.const 16
21642166
call $~lib/builtins/abort
21652167
unreachable
@@ -2179,7 +2181,7 @@
21792181
if
21802182
i32.const 0
21812183
i32.const 512
2182-
i32.const 501
2184+
i32.const 505
21832185
i32.const 14
21842186
call $~lib/builtins/abort
21852187
unreachable

Diff for: tests/compiler/bindings/noExportRuntime.release.wat

+8-10
Original file line numberDiff line numberDiff line change
@@ -785,18 +785,16 @@
785785
local.tee $1
786786
i32.const 0
787787
i32.le_s
788-
if (result i32)
788+
if
789789
i32.const 1
790790
local.get $1
791791
i32.sub
792792
memory.grow $0
793793
i32.const 0
794794
i32.lt_s
795-
else
796-
i32.const 0
797-
end
798-
if
799-
unreachable
795+
if
796+
unreachable
797+
end
800798
end
801799
i32.const 34560
802800
i32.const 0
@@ -1094,7 +1092,7 @@
10941092
if
10951093
i32.const 0
10961094
i32.const 1536
1097-
i32.const 562
1095+
i32.const 566
10981096
i32.const 3
10991097
call $~lib/builtins/abort
11001098
unreachable
@@ -1329,7 +1327,7 @@
13291327
if
13301328
i32.const 1200
13311329
i32.const 1536
1332-
i32.const 461
1330+
i32.const 462
13331331
i32.const 29
13341332
call $~lib/builtins/abort
13351333
unreachable
@@ -1427,7 +1425,7 @@
14271425
if
14281426
i32.const 0
14291427
i32.const 1536
1430-
i32.const 499
1428+
i32.const 503
14311429
i32.const 16
14321430
call $~lib/builtins/abort
14331431
unreachable
@@ -1442,7 +1440,7 @@
14421440
if
14431441
i32.const 0
14441442
i32.const 1536
1445-
i32.const 501
1443+
i32.const 505
14461444
i32.const 14
14471445
call $~lib/builtins/abort
14481446
unreachable

Diff for: tests/compiler/bindings/raw.debug.wat

+11-9
Original file line numberDiff line numberDiff line change
@@ -1341,18 +1341,18 @@
13411341
local.get $pagesNeeded
13421342
local.get $pagesBefore
13431343
i32.gt_s
1344-
if (result i32)
1344+
if
13451345
local.get $pagesNeeded
13461346
local.get $pagesBefore
13471347
i32.sub
13481348
memory.grow $0
13491349
i32.const 0
13501350
i32.lt_s
1351-
else
1351+
if
1352+
unreachable
1353+
end
13521354
i32.const 0
1353-
end
1354-
if
1355-
unreachable
1355+
drop
13561356
end
13571357
local.get $rootOffset
13581358
local.set $root
@@ -1473,7 +1473,7 @@
14731473
if
14741474
i32.const 0
14751475
i32.const 672
1476-
i32.const 562
1476+
i32.const 566
14771477
i32.const 3
14781478
call $~lib/builtins/abort
14791479
unreachable
@@ -1802,7 +1802,7 @@
18021802
if
18031803
i32.const 336
18041804
i32.const 672
1805-
i32.const 461
1805+
i32.const 462
18061806
i32.const 29
18071807
call $~lib/builtins/abort
18081808
unreachable
@@ -2098,6 +2098,8 @@
20982098
unreachable
20992099
end
21002100
end
2101+
i32.const 0
2102+
drop
21012103
memory.size $0
21022104
local.set $pagesAfter
21032105
local.get $root
@@ -2247,7 +2249,7 @@
22472249
if
22482250
i32.const 0
22492251
i32.const 672
2250-
i32.const 499
2252+
i32.const 503
22512253
i32.const 16
22522254
call $~lib/builtins/abort
22532255
unreachable
@@ -2267,7 +2269,7 @@
22672269
if
22682270
i32.const 0
22692271
i32.const 672
2270-
i32.const 501
2272+
i32.const 505
22712273
i32.const 14
22722274
call $~lib/builtins/abort
22732275
unreachable

0 commit comments

Comments
 (0)