Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Add methods for fallible allocation to `cranelift_entity::EntitySet` (#12383)

Nick Fitzgerald630ffcc31796
diff --git a/Cargo.lock b/Cargo.lock
index 95504bae1e..a87a68ff44 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -812,6 +812,7 @@ dependencies = [
"cranelift-bitset",
"serde",
"serde_derive",
+ "wasmtime-internal-error",
]
[[package]]
diff --git a/cranelift/entity/Cargo.toml b/cranelift/entity/Cargo.toml
index 35fff0c519..18965549da 100644
--- a/cranelift/entity/Cargo.toml
+++ b/cranelift/entity/Cargo.toml
@@ -19,6 +19,7 @@ workspace = true
cranelift-bitset = { workspace=true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
+wasmtime-error = { workspace = true }
[features]
enable-serde = ["serde", "serde_derive", "cranelift-bitset/enable-serde"]
diff --git a/cranelift/entity/src/set.rs b/cranelift/entity/src/set.rs
index f46fb95148..878234d185 100644
--- a/cranelift/entity/src/set.rs
+++ b/cranelift/entity/src/set.rs
@@ -5,6 +5,7 @@ use crate::keys::Keys;
use core::fmt;
use core::marker::PhantomData;
use cranelift_bitset::CompoundBitSet;
+use wasmtime_error::OutOfMemory;
/// A set of `K` for densely indexed entity references.
///
@@ -67,12 +68,25 @@ where
}
}
+ /// Like `with_capacity` but returns an error on allocation failure.
+ pub fn try_with_capacity(capacity: usize) -> Result<Self, OutOfMemory> {
+ Ok(Self {
+ bitset: CompoundBitSet::try_with_capacity(capacity)?,
+ unused: PhantomData,
+ })
+ }
+
/// Ensure that the set has enough capacity to hold `capacity` total
/// elements.
pub fn ensure_capacity(&mut self, capacity: usize) {
self.bitset.ensure_capacity(capacity);
}
+ /// Like `ensure_capacity` but returns an error on allocation failure.
+ pub fn try_ensure_capacity(&mut self, capacity: usize) -> Result<(), OutOfMemory> {
+ self.bitset.try_ensure_capacity(capacity)
+ }
+
/// Get the element at `k` if it exists.
pub fn contains(&self, k: K) -> bool {
let index = k.index();