-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add fallible allocation methods to cranelift_bitset::CompoundBitSet
#12381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fitzgen
merged 7 commits into
bytecodealliance:main
from
fitzgen:fallible-alloc-for-compound-bit-set
Jan 22, 2026
+456
−137
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
998a106
Add fallible allocation methods to `cranelift_bitset::CompoundBitSet`
fitzgen 92ff7d0
Add OOM tests for `CompoundBitSet` fallible allocation methods
fitzgen 503ca3c
Create the `wasmtime-internal-core` crate
fitzgen 4cdb00d
Split `wasmtime_core::alloc` up into modules
fitzgen 3152e84
Add the `wasmtime_core::alloc::new_boxed_slice_from_iter` helper
fitzgen 5e45358
Use `new_boxed_slice_from_iter` in `cranelift_bitset::CompoundBitSet`
fitzgen cc8c197
Use `Box::assume_init`
fitzgen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [package] | ||
| authors.workspace = true | ||
| description = "INTERNAL: Wasmtime's core utilities and helpers with minimal dependencies" | ||
| edition.workspace = true | ||
| license = "Apache-2.0 WITH LLVM-exception" | ||
| name = "wasmtime-internal-core" | ||
| rust-version.workspace = true | ||
| version.workspace = true | ||
|
|
||
| [dependencies] | ||
| # This crate should have *very* minimal dependencies! Don't add new ones | ||
| # lightly. | ||
| wasmtime-error = { workspace = true } | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [features] |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //! Low-level allocation and OOM-handling utilities. | ||
|
|
||
| mod arc; | ||
| mod boxed; | ||
| mod try_new; | ||
|
|
||
| pub use boxed::{BoxedSliceFromIterError, new_boxed_slice_from_iter}; | ||
| pub use try_new::{TryNew, try_new}; | ||
|
|
||
| use core::{alloc::Layout, ptr::NonNull}; | ||
| use wasmtime_error::OutOfMemory; | ||
|
|
||
| /// Try to allocate a block of memory that fits the given layout, or return an | ||
| /// `OutOfMemory` error. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// Same as `alloc::alloc::alloc`: layout must have non-zero size. | ||
| #[inline] | ||
| unsafe fn try_alloc(layout: Layout) -> Result<NonNull<u8>, OutOfMemory> { | ||
| // Safety: same as our safety conditions. | ||
| debug_assert!(layout.size() > 0); | ||
| let ptr = unsafe { std_alloc::alloc::alloc(layout) }; | ||
|
|
||
| if let Some(ptr) = NonNull::new(ptr) { | ||
| Ok(ptr) | ||
| } else { | ||
| Err(OutOfMemory::new(layout.size())) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| use super::TryNew; | ||
| use std_alloc::sync::Arc; | ||
| use wasmtime_error::OutOfMemory; | ||
|
|
||
| /// XXX: Stable Rust doesn't actually give us any method to build fallible | ||
| /// allocation for `Arc<T>`, so this is only actually fallible when using | ||
| /// nightly Rust and setting `RUSTFLAGS="--cfg arc_try_new"`. | ||
| impl<T> TryNew for Arc<T> { | ||
| type Value = T; | ||
|
|
||
| #[inline] | ||
| fn try_new(value: T) -> Result<Self, OutOfMemory> | ||
| where | ||
| Self: Sized, | ||
| { | ||
| #[cfg(arc_try_new)] | ||
| return Arc::try_new(value).map_err(|_| { | ||
| // We don't have access to the exact size of the inner `Arc` | ||
| // allocation, but (at least at one point) it was made up of a | ||
| // strong ref count, a weak ref count, and the inner value. | ||
| let bytes = core::mem::size_of::<(usize, usize, T)>(); | ||
| OutOfMemory::new(bytes) | ||
| }); | ||
|
|
||
| #[cfg(not(arc_try_new))] | ||
| return Ok(Arc::new(value)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'd say that wasmtime-{math,error} could get folded in here too (probably in a future PR), but wanted to confirm/deny your thoughts on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm half of the mind that there is no need to eagerly merge crates if we don't have any reason to do so, but I don't feel strongly at all. Anyways, I don't think there is anything blocking that, but I don't have any plans on doing it myself.