1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#![allow(clippy::use_self)] use crate::alloc::{ AllocRef, BuildAlloc, BuildDealloc, BuildRealloc, DeallocRef, Layout, NonZeroLayout, ReallocRef, }; use core::ptr::NonNull; use liballoc::alloc::handle_alloc_error; #[derive(Default, Copy, Clone)] pub struct AbortAlloc<A>(pub A); impl<A: BuildAlloc> BuildAlloc for AbortAlloc<A> { type AllocRef = AbortAlloc<A::AllocRef>; unsafe fn build_alloc_ref(&mut self, ptr: NonNull<u8>, layout: Layout) -> Self::AllocRef { Self(self.0.build_alloc_ref(ptr, layout)) } } impl<A: BuildDealloc> BuildDealloc for AbortAlloc<A> { type DeallocRef = AbortAlloc<A::DeallocRef>; unsafe fn build_dealloc_ref(&mut self, ptr: NonNull<u8>, layout: Layout) -> Self::DeallocRef { Self(self.0.build_dealloc_ref(ptr, layout)) } } impl<A: BuildRealloc> BuildRealloc for AbortAlloc<A> { type ReallocRef = AbortAlloc<A::ReallocRef>; unsafe fn build_realloc_ref(&mut self, ptr: NonNull<u8>, layout: Layout) -> Self::ReallocRef { Self(self.0.build_realloc_ref(ptr, layout)) } } impl<A: AllocRef> AllocRef for AbortAlloc<A> { type BuildAlloc = AbortAlloc<A::BuildAlloc>; type Error = !; fn get_build_alloc(&mut self) -> Self::BuildAlloc { Self(self.0.get_build_alloc()) } fn alloc(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> { self.0 .alloc(layout) .map_err(|_| handle_alloc_error(layout.into())) } fn alloc_zeroed(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> { self.0 .alloc_zeroed(layout) .map_err(|_| handle_alloc_error(layout.into())) } } impl<A: DeallocRef> DeallocRef for AbortAlloc<A> { type BuildDealloc = AbortAlloc<A::BuildDealloc>; fn get_build_dealloc(&mut self) -> Self::BuildDealloc { Self(self.0.get_build_dealloc()) } unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: NonZeroLayout) { self.0.dealloc(ptr, layout) } } impl<A: ReallocRef> ReallocRef for AbortAlloc<A> { type BuildRealloc = AbortAlloc<A::BuildRealloc>; type Error = !; fn get_build_realloc(&mut self) -> Self::BuildRealloc { Self(self.0.get_build_realloc()) } unsafe fn realloc( &mut self, ptr: NonNull<u8>, layout: NonZeroLayout, new_size: usize, ) -> Result<NonNull<u8>, Self::Error> { self.0 .realloc(ptr, layout, new_size) .map_err(|_| handle_alloc_error(layout.into())) } }