-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
WIP: Allocator- and fallibility-polymorphic collections #60703
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
98ebb0c
Remove the impl Alloc for System
glandium 3cbdf18
Associate an allocator to boxes
glandium 53538de
Add associated error type to allocators
Ericson2314 4454093
Add AbortAdapter allocator adapter
Ericson2314 afa5677
box, RawVec: Generalize to not neccessary abort on OOM
Ericson2314 e7f48a6
box_free_worker: Pull out version that just borrows alloc
Ericson2314 55098c0
Rc: Generalize for arbitrary allocators
Ericson2314 9289a62
Arc: Generalize for arbitrary allocators
Ericson2314 66b01b6
LinkedList: Generalize for arbitrary allocators
Ericson2314 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
//! An allocator adapter that blows up by calling `handle_alloc_error` on all errors. | ||
//! | ||
//! On one hand, concrete allocator implementations should always be written | ||
//! without panicking on user error and OOM to give users maximum | ||
//! flexibility. On the other hand, code that depends on allocation succeeding | ||
//! should depend on `Alloc<Err=!>` to avoid repetitively handling errors from | ||
//! which it cannot recover. | ||
//! | ||
//! This adapter bridges the gap, effectively allowing `Alloc<Err=!>` to be | ||
//! implemented by any allocator. | ||
|
||
#![unstable(feature = "allocator_api", | ||
reason = "the precise API and guarantees it provides may be tweaked \ | ||
slightly, especially to possibly take into account the \ | ||
types being stored to make room for a future \ | ||
tracing garbage collector", | ||
issue = "32838")] | ||
|
||
use core::usize; | ||
use core::ptr::NonNull; | ||
|
||
use crate::alloc::*; | ||
|
||
/// An allocator adapter that blows up by calling `handle_alloc_error` on all errors. | ||
/// | ||
/// See the [module-level documentation](../../std/abort_adapter/index.html) for more. | ||
#[derive(Copy, Clone, Debug, Default)] | ||
pub struct AbortAdapter<Alloc>(pub Alloc); | ||
|
||
impl<A: AllocHelper> AllocHelper for AbortAdapter<A> { | ||
type Err = !; | ||
} | ||
|
||
unsafe impl<A: Alloc> Alloc for AbortAdapter<A> { | ||
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, Self::Err> { | ||
self.0.alloc(layout).or_else(|_| handle_alloc_error(layout)) | ||
} | ||
|
||
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) { | ||
self.0.dealloc(ptr, layout) | ||
} | ||
|
||
fn usable_size(&self, layout: &Layout) -> (usize, usize) { | ||
self.0.usable_size(layout) | ||
} | ||
|
||
unsafe fn realloc(&mut self, | ||
ptr: NonNull<u8>, | ||
layout: Layout, | ||
new_size: usize) -> Result<NonNull<u8>, Self::Err> { | ||
self.0.realloc(ptr, layout, new_size).or_else(|_| { | ||
let layout = Layout::from_size_align_unchecked(new_size, layout.align()); | ||
handle_alloc_error(layout) | ||
}) | ||
} | ||
|
||
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, Self::Err> { | ||
self.0.alloc_zeroed(layout).or_else(|_| handle_alloc_error(layout)) | ||
} | ||
|
||
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, Self::Err> { | ||
self.0.alloc_excess(layout).or_else(|_| handle_alloc_error(layout)) | ||
} | ||
|
||
unsafe fn grow_in_place(&mut self, | ||
ptr: NonNull<u8>, | ||
layout: Layout, | ||
new_size: usize) -> Result<(), CannotReallocInPlace> { | ||
self.0.grow_in_place(ptr, layout, new_size) | ||
} | ||
|
||
unsafe fn shrink_in_place(&mut self, | ||
ptr: NonNull<u8>, | ||
layout: Layout, | ||
new_size: usize) -> Result<(), CannotReallocInPlace> { | ||
self.0.shrink_in_place(ptr, layout, new_size) | ||
} | ||
|
||
fn alloc_one<T>(&mut self) -> Result<NonNull<T>, Self::Err> | ||
where Self: Sized | ||
{ | ||
self.0.alloc_one().or_else(|_| handle_alloc_error(Layout::new::<T>())) | ||
} | ||
|
||
unsafe fn dealloc_one<T>(&mut self, ptr: NonNull<T>) | ||
where Self: Sized | ||
{ | ||
self.0.dealloc_one(ptr) | ||
} | ||
|
||
fn alloc_array<T>(&mut self, n: usize) -> Result<NonNull<T>, Self::Err> | ||
where Self: Sized | ||
{ | ||
self.0.alloc_array(n).or_else(|_| handle_alloc_error(Layout::new::<T>())) | ||
} | ||
|
||
unsafe fn realloc_array<T>(&mut self, | ||
ptr: NonNull<T>, | ||
n_old: usize, | ||
n_new: usize) -> Result<NonNull<T>, Self::Err> | ||
where Self: Sized | ||
{ | ||
self.0.realloc_array(ptr, n_old, n_new) | ||
.or_else(|_| handle_alloc_error(Layout::new::<T>())) | ||
} | ||
|
||
unsafe fn dealloc_array<T>(&mut self, ptr: NonNull<T>, n: usize) -> Result<(), Self::Err> | ||
where Self: Sized | ||
{ | ||
self.0.dealloc_array(ptr, n).or_else(|_| handle_alloc_error(Layout::new::<T>())) | ||
} | ||
} |
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.
I feel like we should handle this in a way that includes the actual error that occured. Maybe it'd be best to bound the
Err
associated type onDebug
orDisplay
?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.
Sure, but I figure we can hash out what
handle_alloc_error
should take 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.
handle_alloc_error
is stable, so it can't be changed. But yeah, this is less important for the moment.