-
Notifications
You must be signed in to change notification settings - Fork 13k
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
alloc: add some try_* methods Rust-for-Linux needs #86938
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 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 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 |
---|---|---|
|
@@ -19,7 +19,6 @@ use crate::collections::TryReserveErrorKind::*; | |
#[cfg(test)] | ||
mod tests; | ||
|
||
#[cfg(not(no_global_oom_handling))] | ||
enum AllocInit { | ||
/// The contents of the new memory are uninitialized. | ||
Uninitialized, | ||
|
@@ -94,6 +93,16 @@ impl<T> RawVec<T, Global> { | |
Self::with_capacity_in(capacity, Global) | ||
} | ||
|
||
/// Tries to create a `RawVec` (on the system heap) with exactly the | ||
/// capacity and alignment requirements for a `[T; capacity]`. This is | ||
/// equivalent to calling `RawVec::new` when `capacity` is `0` or `T` is | ||
/// zero-sized. Note that if `T` is zero-sized this means you will | ||
/// *not* get a `RawVec` with the requested capacity. | ||
#[inline] | ||
pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> { | ||
Self::try_with_capacity_in(capacity, Global) | ||
} | ||
|
||
/// Like `with_capacity`, but guarantees the buffer is zeroed. | ||
#[cfg(not(no_global_oom_handling))] | ||
#[must_use] | ||
|
@@ -102,6 +111,12 @@ impl<T> RawVec<T, Global> { | |
Self::with_capacity_zeroed_in(capacity, Global) | ||
} | ||
|
||
/// Like `try_with_capacity`, but guarantees a successfully allocated buffer is zeroed. | ||
#[inline] | ||
pub fn try_with_capacity_zeroed(capacity: usize) -> Result<Self, TryReserveError> { | ||
Self::try_with_capacity_zeroed_in(capacity, Global) | ||
} | ||
|
||
/// Reconstitutes a `RawVec` from a pointer and capacity. | ||
/// | ||
/// # Safety | ||
|
@@ -146,6 +161,13 @@ impl<T, A: Allocator> RawVec<T, A> { | |
Self::allocate_in(capacity, AllocInit::Uninitialized, alloc) | ||
} | ||
|
||
/// Like `try_with_capacity`, but parameterized over the choice of | ||
/// allocator for the returned `RawVec`. | ||
#[inline] | ||
pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> { | ||
Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc) | ||
} | ||
|
||
/// Like `with_capacity_zeroed`, but parameterized over the choice | ||
/// of allocator for the returned `RawVec`. | ||
#[cfg(not(no_global_oom_handling))] | ||
|
@@ -154,6 +176,13 @@ impl<T, A: Allocator> RawVec<T, A> { | |
Self::allocate_in(capacity, AllocInit::Zeroed, alloc) | ||
} | ||
|
||
/// Like `with_capacity_zeroed`, but parameterized over the choice | ||
/// of allocator for the returned `RawVec`. | ||
#[inline] | ||
pub fn try_with_capacity_zeroed_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> { | ||
Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc) | ||
} | ||
|
||
/// Converts a `Box<[T]>` into a `RawVec<T>`. | ||
pub fn from_box(slice: Box<[T], A>) -> Self { | ||
unsafe { | ||
|
@@ -190,34 +219,37 @@ impl<T, A: Allocator> RawVec<T, A> { | |
|
||
#[cfg(not(no_global_oom_handling))] | ||
fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self { | ||
match Self::try_allocate_in(capacity, init, alloc) { | ||
Ok(r) => r, | ||
Err(e) => e.handle(), | ||
} | ||
Comment on lines
+222
to
+225
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw some comment about |
||
} | ||
|
||
fn try_allocate_in( | ||
capacity: usize, | ||
init: AllocInit, | ||
alloc: A, | ||
) -> Result<Self, TryReserveError> { | ||
if mem::size_of::<T>() == 0 { | ||
Self::new_in(alloc) | ||
} else { | ||
// We avoid `unwrap_or_else` here because it bloats the amount of | ||
// LLVM IR generated. | ||
let layout = match Layout::array::<T>(capacity) { | ||
Ok(layout) => layout, | ||
Err(_) => capacity_overflow(), | ||
}; | ||
match alloc_guard(layout.size()) { | ||
Ok(_) => {} | ||
Err(_) => capacity_overflow(), | ||
} | ||
let result = match init { | ||
AllocInit::Uninitialized => alloc.allocate(layout), | ||
AllocInit::Zeroed => alloc.allocate_zeroed(layout), | ||
}; | ||
let ptr = match result { | ||
Ok(ptr) => ptr, | ||
Err(_) => handle_alloc_error(layout), | ||
}; | ||
|
||
Self { | ||
ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) }, | ||
cap: Self::capacity_from_bytes(ptr.len()), | ||
alloc, | ||
} | ||
return Ok(Self::new_in(alloc)); | ||
} | ||
|
||
let layout = Layout::array::<T>(capacity)?; | ||
alloc_guard(layout.size())?; | ||
let result = match init { | ||
AllocInit::Uninitialized => alloc.allocate(layout), | ||
AllocInit::Zeroed => alloc.allocate_zeroed(layout), | ||
}; | ||
let ptr = match result { | ||
Ok(ptr) => ptr, | ||
Err(_) => return Err(TryReserveErrorKind::AllocError { layout, non_exhaustive: () }), | ||
}; | ||
|
||
Ok(Self { | ||
ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) }, | ||
cap: Self::capacity_from_bytes(ptr.len()), | ||
alloc, | ||
}) | ||
} | ||
|
||
/// Reconstitutes a `RawVec` from a pointer, capacity, and allocator. | ||
|
@@ -394,7 +426,29 @@ impl<T, A: Allocator> RawVec<T, A> { | |
/// Aborts on OOM. | ||
#[cfg(not(no_global_oom_handling))] | ||
pub fn shrink_to_fit(&mut self, amount: usize) { | ||
handle_reserve(self.shrink(amount)); | ||
handle_reserve(self.try_shrink_to_fit(amount)); | ||
} | ||
|
||
/// Tries to shrink the allocation down to the specified amount. If the given amount | ||
/// is 0, actually completely deallocates. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the given amount is *larger* than the current capacity. | ||
pub fn try_shrink_to_fit(&mut self, amount: usize) -> Result<(), TryReserveError> { | ||
assert!(amount <= self.capacity(), "Tried to shrink to a larger capacity"); | ||
|
||
let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) }; | ||
let new_size = amount * mem::size_of::<T>(); | ||
|
||
let ptr = unsafe { | ||
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align()); | ||
self.alloc | ||
.shrink(ptr, layout, new_layout) | ||
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })? | ||
}; | ||
self.set_ptr(ptr); | ||
Ok(()) | ||
} | ||
} | ||
|
||
|
@@ -466,22 +520,6 @@ impl<T, A: Allocator> RawVec<T, A> { | |
self.set_ptr(ptr); | ||
Ok(()) | ||
} | ||
|
||
fn shrink(&mut self, amount: usize) -> Result<(), TryReserveError> { | ||
assert!(amount <= self.capacity(), "Tried to shrink to a larger capacity"); | ||
|
||
let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) }; | ||
let new_size = amount * mem::size_of::<T>(); | ||
|
||
let ptr = unsafe { | ||
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align()); | ||
self.alloc | ||
.shrink(ptr, layout, new_layout) | ||
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })? | ||
}; | ||
self.set_ptr(ptr); | ||
Ok(()) | ||
} | ||
} | ||
|
||
// This function is outside `RawVec` to minimize compile times. See the comment | ||
|
@@ -558,6 +596,6 @@ fn alloc_guard(alloc_size: usize) -> Result<(), TryReserveError> { | |
// ensure that the code generation related to these panics is minimal as there's | ||
// only one location which panics rather than a bunch throughout the module. | ||
#[cfg(not(no_global_oom_handling))] | ||
fn capacity_overflow() -> ! { | ||
pub(crate) fn capacity_overflow() -> ! { | ||
panic!("capacity overflow"); | ||
} |
This file contains 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
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.
It might be useful to expose this outside the crate, but I didn't get into that for now.