Skip to content

Commit f3b1194

Browse files
authored
Simplify dynamic linking table allocation. NFC (#23634)
Also add an extra assert that should catch table layout inconsistencies between threads. I noticed this could be improved while reviewing #23633.
1 parent 1236ec0 commit f3b1194

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/lib/libdylink.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ var LibraryDylink = {
627627
// loadModule loads the wasm module after all its dependencies have been loaded.
628628
// can be called both sync/async.
629629
function loadModule() {
630+
#if PTHREADS
630631
// The first thread to load a given module needs to allocate the static
631632
// table and memory regions. Later threads re-use the same table region
632633
// and can ignore the memory region (since memory is shared between
@@ -636,6 +637,7 @@ var LibraryDylink = {
636637
// locking in `dynlink.c`.
637638
var firstLoad = !handle || !{{{ makeGetValue('handle', C_STRUCTS.dso.mem_allocated, 'i8') }}};
638639
if (firstLoad) {
640+
#endif
639641
// alignments are powers of 2
640642
var memAlign = Math.pow(2, metadata.memoryAlign);
641643
// prepare memory
@@ -648,21 +650,27 @@ var LibraryDylink = {
648650
{{{ makeSetValue('handle', C_STRUCTS.dso.table_addr, 'tableBase', '*') }}};
649651
{{{ makeSetValue('handle', C_STRUCTS.dso.table_size, 'metadata.tableSize', 'i32') }}};
650652
}
653+
#if PTHREADS
651654
} else {
655+
// Read the values for tableBase and memoryBase from shared memory. The
656+
// thread that first loaded the DLL already set these values.
652657
memoryBase = {{{ makeGetValue('handle', C_STRUCTS.dso.mem_addr, '*') }}};
653658
tableBase = {{{ makeGetValue('handle', C_STRUCTS.dso.table_addr, '*') }}};
654659
}
660+
#endif
655661

656-
var tableGrowthNeeded = tableBase + metadata.tableSize - {{{ from64Expr('wasmTable.length') }}};
657-
if (tableGrowthNeeded > 0) {
662+
if (metadata.tableSize) {
663+
#if ASSERTIONS
664+
assert({{{ from64Expr('wasmTable.length') }}} == tableBase, `unexpected table size while loading ${libName}: ${wasmTable.length}`);
665+
#endif
658666
#if DYLINK_DEBUG
659-
dbg("loadModule: growing table: " + tableGrowthNeeded);
667+
dbg("loadModule: growing table by: " + metadata.tableSize);
660668
#endif
661-
wasmTable.grow({{{ toIndexType('tableGrowthNeeded') }}});
669+
wasmTable.grow({{{ toIndexType('metadata.tableSize') }}});
662670
}
663671
#if DYLINK_DEBUG
664-
dbg("loadModule: memory[" + memoryBase + ":" + (memoryBase + metadata.memorySize) + "]" +
665-
" table[" + tableBase + ":" + (tableBase + metadata.tableSize) + "]");
672+
dbg(`loadModule: memory[${memoryBase}:${memoryBase + metadata.memorySize}]` +
673+
` table[${tableBasex}:${tableBase + metadata.tableSize}]`);
666674
#endif
667675

668676
// This is the export map that we ultimately return. We declare it here
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5914
1+
5907
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13075
1+
13061

0 commit comments

Comments
 (0)