Skip to content
This repository was archived by the owner on Nov 27, 2020. It is now read-only.

Commit 710ba7d

Browse files
committed
Revert "Remove AbortAlloc"
This reverts commit 708a298.
1 parent 6594a0e commit 710ba7d

File tree

8 files changed

+230
-115
lines changed

8 files changed

+230
-115
lines changed

src/alloc/abort.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#![allow(clippy::use_self)]
2+
3+
use crate::alloc::{AllocRef, BuildAllocRef, DeallocRef, NonZeroLayout, ReallocRef};
4+
use core::ptr::NonNull;
5+
use liballoc::alloc::handle_alloc_error;
6+
7+
/// An allocator, which wraps another allocator and aborts on OOM.
8+
#[derive(Debug, Default, Copy, Clone)]
9+
pub struct AbortAlloc<A>(pub A);
10+
11+
impl<A: BuildAllocRef> BuildAllocRef for AbortAlloc<A> {
12+
type Ref = AbortAlloc<A::Ref>;
13+
14+
unsafe fn build_alloc_ref(
15+
&mut self,
16+
ptr: NonNull<u8>,
17+
layout: Option<NonZeroLayout>,
18+
) -> Self::Ref {
19+
Self(self.0.build_alloc_ref(ptr, layout))
20+
}
21+
}
22+
23+
impl<A: DeallocRef> DeallocRef for AbortAlloc<A> {
24+
type BuildAlloc = AbortAlloc<A::BuildAlloc>;
25+
26+
fn get_build_alloc(&mut self) -> Self::BuildAlloc {
27+
Self(self.0.get_build_alloc())
28+
}
29+
30+
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: NonZeroLayout) {
31+
self.0.dealloc(ptr, layout)
32+
}
33+
}
34+
35+
impl<A: AllocRef> AllocRef for AbortAlloc<A> {
36+
type Error = !;
37+
38+
fn alloc(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
39+
self.0
40+
.alloc(layout)
41+
.map_err(|_| handle_alloc_error(layout.into()))
42+
}
43+
44+
fn alloc_zeroed(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
45+
self.0
46+
.alloc_zeroed(layout)
47+
.map_err(|_| handle_alloc_error(layout.into()))
48+
}
49+
}
50+
51+
impl<A: ReallocRef> ReallocRef for AbortAlloc<A> {
52+
unsafe fn realloc(
53+
&mut self,
54+
ptr: NonNull<u8>,
55+
old_layout: NonZeroLayout,
56+
new_layout: NonZeroLayout,
57+
) -> Result<NonNull<u8>, Self::Error> {
58+
self.0
59+
.realloc(ptr, old_layout, new_layout)
60+
.map_err(|_| handle_alloc_error(new_layout.into()))
61+
}
62+
}

src/alloc/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
mod abort;
12
mod layout;
23

3-
pub use self::layout::{LayoutErr, NonZeroLayout};
4+
pub use self::{
5+
abort::AbortAlloc,
6+
layout::{LayoutErr, NonZeroLayout},
7+
};
48
pub use core::alloc::GlobalAlloc;
59
use core::{
610
cmp,

src/boxed.rs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
//! [`NonZeroLayout::for_value(&*value)`]: crate::alloc::NonZeroLayout::for_value
8080
8181
use crate::{
82-
alloc::{AllocRef, BuildAllocRef, DeallocRef, Global, NonZeroLayout},
82+
alloc::{AbortAlloc, AllocRef, BuildAllocRef, DeallocRef, Global, NonZeroLayout},
8383
clone::CloneIn,
8484
collections::CollectionAllocErr,
8585
raw_vec::RawVec,
@@ -105,7 +105,7 @@ use core::{
105105
/// A pointer type for heap allocation.
106106
///
107107
/// See the [module-level documentation](index.html) for more.
108-
pub struct Box<T: ?Sized, A: DeallocRef = Global> {
108+
pub struct Box<T: ?Sized, A: DeallocRef = AbortAlloc<Global>> {
109109
ptr: Unique<T>,
110110
build_alloc: A::BuildAlloc,
111111
}
@@ -131,7 +131,7 @@ impl<T> Box<T> {
131131
#[inline(always)]
132132
#[must_use]
133133
pub fn new(x: T) -> Self {
134-
Self::new_in(x, Global)
134+
Self::new_in(x, AbortAlloc(Global))
135135
}
136136

137137
/// Constructs a new box with uninitialized contents.
@@ -156,7 +156,7 @@ impl<T> Box<T> {
156156
#[inline(always)]
157157
#[must_use]
158158
pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
159-
Self::new_uninit_in(Global)
159+
Self::new_uninit_in(AbortAlloc(Global))
160160
}
161161

162162
/// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
@@ -177,14 +177,20 @@ impl<T, A: AllocRef> Box<T, A> {
177177
/// # Example
178178
///
179179
/// ```
180-
/// use alloc_wg::{alloc::Global, boxed::Box};
180+
/// use alloc_wg::{
181+
/// alloc::{AbortAlloc, Global},
182+
/// boxed::Box,
183+
/// };
181184
///
182185
/// # #[allow(unused_variables)]
183-
/// let five = Box::new_in(5, Global);
186+
/// let five = Box::new_in(5, AbortAlloc(Global));
184187
/// ```
185188
#[allow(clippy::inline_always)]
186189
#[inline(always)]
187-
pub fn new_in(x: T, a: A) -> Self {
190+
pub fn new_in(x: T, a: A) -> Self
191+
where
192+
A: AllocRef<Error = !>,
193+
{
188194
unsafe { Self::try_new_in(x, a).unwrap_unchecked() }
189195
}
190196

@@ -219,9 +225,12 @@ impl<T, A: AllocRef> Box<T, A> {
219225
/// # Example
220226
///
221227
/// ```
222-
/// use alloc_wg::{alloc::Global, boxed::Box};
228+
/// use alloc_wg::{
229+
/// alloc::{AbortAlloc, Global},
230+
/// boxed::Box,
231+
/// };
223232
///
224-
/// let mut five = Box::<u32, _>::new_uninit_in(Global);
233+
/// let mut five = Box::<u32, _>::new_uninit_in(AbortAlloc(Global));
225234
///
226235
/// let five = unsafe {
227236
/// // Deferred initialization:
@@ -234,7 +243,10 @@ impl<T, A: AllocRef> Box<T, A> {
234243
/// ```
235244
#[allow(clippy::inline_always)]
236245
#[inline(always)]
237-
pub fn new_uninit_in(a: A) -> Box<mem::MaybeUninit<T>, A> {
246+
pub fn new_uninit_in(a: A) -> Box<mem::MaybeUninit<T>, A>
247+
where
248+
A: AllocRef<Error = !>,
249+
{
238250
unsafe { Self::try_new_uninit_in(a).unwrap_unchecked() }
239251
}
240252

@@ -271,7 +283,10 @@ impl<T, A: AllocRef> Box<T, A> {
271283
/// `Unpin`, then `x` will be pinned in memory and unable to be moved.
272284
#[allow(clippy::inline_always)]
273285
#[inline(always)]
274-
pub fn pin_in(x: T, a: A) -> Pin<Self> {
286+
pub fn pin_in(x: T, a: A) -> Pin<Self>
287+
where
288+
A: AllocRef<Error = !>,
289+
{
275290
unsafe { Self::try_pin_in(x, a).unwrap_unchecked() }
276291
}
277292

@@ -309,7 +324,7 @@ impl<T> Box<[T]> {
309324
#[inline(always)]
310325
#[must_use]
311326
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
312-
Self::new_uninit_slice_in(len, Global)
327+
Self::new_uninit_slice_in(len, AbortAlloc(Global))
313328
}
314329
}
315330

@@ -320,9 +335,12 @@ impl<T, A: AllocRef> Box<[T], A> {
320335
/// # Example
321336
///
322337
/// ```
323-
/// use alloc_wg::{alloc::Global, boxed::Box};
338+
/// use alloc_wg::{
339+
/// alloc::{AbortAlloc, Global},
340+
/// boxed::Box,
341+
/// };
324342
///
325-
/// let mut values = Box::<[u32], _>::new_uninit_slice_in(3, Global);
343+
/// let mut values = Box::<[u32], AbortAlloc<Global>>::new_uninit_slice_in(3, AbortAlloc(Global));
326344
///
327345
/// let values = unsafe {
328346
/// // Deferred initialization:
@@ -337,7 +355,10 @@ impl<T, A: AllocRef> Box<[T], A> {
337355
/// ```
338356
#[allow(clippy::inline_always)]
339357
#[inline(always)]
340-
pub fn new_uninit_slice_in(len: usize, a: A) -> Box<[mem::MaybeUninit<T>], A> {
358+
pub fn new_uninit_slice_in(len: usize, a: A) -> Box<[mem::MaybeUninit<T>], A>
359+
where
360+
A: AllocRef<Error = !>,
361+
{
341362
unsafe { Self::try_new_uninit_slice_in(len, a).unwrap_unchecked() }
342363
}
343364

@@ -503,7 +524,7 @@ impl<T: ?Sized> Box<T> {
503524
#[allow(clippy::inline_always)]
504525
#[inline(always)]
505526
pub unsafe fn from_raw(raw: *mut T) -> Self {
506-
Self::from_raw_in(raw, Global)
527+
Self::from_raw_in(raw, AbortAlloc(Global))
507528
}
508529
}
509530

@@ -747,7 +768,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: DeallocRef> Drop for Box<T, A> {
747768
impl<T, A> Default for Box<T, A>
748769
where
749770
T: Default,
750-
A: Default + AllocRef,
771+
A: Default + AllocRef<Error = !>,
751772
{
752773
#[must_use]
753774
fn default() -> Self {
@@ -758,7 +779,7 @@ where
758779
#[allow(clippy::use_self)]
759780
impl<T, A> Default for Box<[T], A>
760781
where
761-
A: Default + AllocRef,
782+
A: Default + AllocRef<Error = !>,
762783
{
763784
#[must_use]
764785
fn default() -> Self {
@@ -775,7 +796,7 @@ unsafe fn from_boxed_utf8_unchecked<A: DeallocRef>(v: Box<[u8], A>) -> Box<str,
775796
#[allow(clippy::use_self)]
776797
impl<A> Default for Box<str, A>
777798
where
778-
A: Default + AllocRef,
799+
A: Default + AllocRef<Error = !>,
779800
{
780801
#[must_use]
781802
fn default() -> Self {
@@ -785,7 +806,7 @@ where
785806

786807
impl<T: Clone, A> Clone for Box<T, A>
787808
where
788-
A: AllocRef,
809+
A: AllocRef<Error = !>,
789810
A::BuildAlloc: Clone,
790811
{
791812
/// Returns a new box with a `clone()` of this box's contents.
@@ -846,7 +867,10 @@ where
846867
impl<T: Clone, A: AllocRef, B: AllocRef> CloneIn<B> for Box<T, A> {
847868
type Cloned = Box<T, B>;
848869

849-
fn clone_in(&self, a: B) -> Self::Cloned {
870+
fn clone_in(&self, a: B) -> Self::Cloned
871+
where
872+
B: AllocRef<Error = !>,
873+
{
850874
Box::new_in(self.as_ref().clone(), a)
851875
}
852876

@@ -949,7 +973,7 @@ impl<T: ?Sized + Hasher, A: DeallocRef> Hasher for Box<T, A> {
949973

950974
impl<T, A> From<T> for Box<T, A>
951975
where
952-
A: Default + AllocRef,
976+
A: Default + AllocRef<Error = !>,
953977
{
954978
/// Converts a generic type `T` into a `Box<T>`
955979
///
@@ -983,7 +1007,7 @@ impl<T: ?Sized, A: DeallocRef> From<Box<T, A>> for Pin<Box<T, A>> {
9831007
#[allow(clippy::use_self)]
9841008
impl<T: Copy, A> From<&[T]> for Box<[T], A>
9851009
where
986-
A: Default + AllocRef,
1010+
A: Default + AllocRef<Error = !>,
9871011
{
9881012
/// Converts a `&[T]` into a `Box<[T], B>`
9891013
///
@@ -1012,7 +1036,7 @@ where
10121036
#[allow(clippy::use_self)]
10131037
impl<A> From<&str> for Box<str, A>
10141038
where
1015-
A: Default + AllocRef,
1039+
A: Default + AllocRef<Error = !>,
10161040
{
10171041
/// Converts a `&str` into a `Box<str>`
10181042
///
@@ -1266,13 +1290,16 @@ macro_rules! impl_dispatch_from_dyn {
12661290
}
12671291

12681292
impl_dispatch_from_dyn!(Global);
1293+
impl_dispatch_from_dyn!(AbortAlloc<Global>);
12691294
#[cfg(feature = "std")]
12701295
impl_dispatch_from_dyn!(std::alloc::System);
1296+
#[cfg(feature = "std")]
1297+
impl_dispatch_from_dyn!(AbortAlloc<std::alloc::System>);
12711298

12721299
#[allow(clippy::items_after_statements)]
12731300
impl<T: Clone, A: Clone> Clone for Box<[T], A>
12741301
where
1275-
A: AllocRef,
1302+
A: AllocRef<Error = !>,
12761303
A::BuildAlloc: Clone,
12771304
{
12781305
fn clone(&self) -> Self {

src/clone.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::alloc::AllocRef;
33
pub trait CloneIn<A: AllocRef>: Sized {
44
type Cloned;
55

6-
fn clone_in(&self, a: A) -> Self::Cloned;
6+
fn clone_in(&self, a: A) -> Self::Cloned
7+
where
8+
A: AllocRef<Error = !>;
79

810
fn try_clone_in(&self, a: A) -> Result<Self::Cloned, A::Error>;
911
}

src/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub trait TryExtend<A> {
1919
/// message.try_extend(['d', 'e', 'f'].iter())?;
2020
///
2121
/// assert_eq!(vec!['a', 'b', 'c', 'd', 'e', 'f'], message);
22-
/// # Ok::<(), alloc_wg::collections::CollectionAllocErr<alloc_wg::alloc::Global>>(())
22+
/// # Ok::<(), alloc_wg::collections::CollectionAllocErr<alloc_wg::alloc::AbortAlloc<alloc_wg::alloc::Global>>>(())
2323
/// ```
2424
fn try_extend<T: IntoIterator<Item = A>>(&mut self, iter: T) -> Result<(), Self::Err>;
2525
}

0 commit comments

Comments
 (0)