Merge pull request #2505 from alexcrichton/fix-reserve-bug

Fix a memory reservation bug in `reserve_modules`
This commit is contained in:
Nick Fitzgerald
2020-12-14 10:12:46 -08:00
committed by GitHub

View File

@@ -31,6 +31,10 @@ pub struct ModuleEnvironment<'data> {
/// the module linking proposal. /// the module linking proposal.
results: Vec<ModuleTranslation<'data>>, results: Vec<ModuleTranslation<'data>>,
/// How many modules that have not yet made their way into `results` which
/// are coming at some point.
modules_to_be: usize,
/// Intern'd types for this entire translation, shared by all modules. /// Intern'd types for this entire translation, shared by all modules.
types: TypeTables, types: TypeTables,
@@ -138,6 +142,7 @@ impl<'data> ModuleEnvironment<'data> {
Self { Self {
result: ModuleTranslation::default(), result: ModuleTranslation::default(),
results: Vec::with_capacity(1), results: Vec::with_capacity(1),
modules_to_be: 1,
cur: 0, cur: 0,
types: Default::default(), types: Default::default(),
target_config, target_config,
@@ -741,8 +746,8 @@ and for re-adding support for interface types you can see this issue:
fn reserve_modules(&mut self, amount: u32) { fn reserve_modules(&mut self, amount: u32) {
// Go ahead and reserve space in the final `results` array for `amount` // Go ahead and reserve space in the final `results` array for `amount`
// more modules. // more modules.
let extra = self.results.capacity() + (amount as usize) - self.results.len(); self.modules_to_be += amount as usize;
self.results.reserve(extra); self.results.reserve(self.modules_to_be);
// Then also reserve space in our own local module's metadata fields // Then also reserve space in our own local module's metadata fields
// we'll be adding to. // we'll be adding to.
@@ -796,6 +801,7 @@ and for re-adding support for interface types you can see this issue:
self.cur = index; self.cur = index;
assert_eq!(index, self.results.len()); assert_eq!(index, self.results.len());
self.results.push(prev); self.results.push(prev);
self.modules_to_be -= 1;
} }
fn module_end(&mut self, index: usize) { fn module_end(&mut self, index: usize) {