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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
mod abort; mod layout; pub use self::{ abort::AbortAlloc, layout::{LayoutErr, NonZeroLayout}, }; use core::{ alloc::{AllocErr, Layout}, ptr::{self, NonNull}, }; use liballoc::alloc::{Alloc, Global}; #[cfg(feature = "std")] use std::alloc::System; pub trait BuildAlloc: Sized { type AllocRef: AllocRef<BuildAlloc = Self>; unsafe fn build_alloc_ref(&mut self, ptr: NonNull<u8>, layout: Layout) -> Self::AllocRef; } pub trait BuildDealloc: Sized { type DeallocRef: DeallocRef<BuildDealloc = Self>; unsafe fn build_dealloc_ref(&mut self, ptr: NonNull<u8>, layout: Layout) -> Self::DeallocRef; } pub trait BuildRealloc: Sized { type ReallocRef: ReallocRef<BuildRealloc = Self>; unsafe fn build_realloc_ref(&mut self, ptr: NonNull<u8>, layout: Layout) -> Self::ReallocRef; } pub trait AllocRef: Sized { type BuildAlloc: BuildAlloc<AllocRef = Self>; type Error; fn get_build_alloc(&mut self) -> Self::BuildAlloc; fn alloc(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error>; fn alloc_zeroed(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> { let size = layout.size(); let p = self.alloc(layout)?; unsafe { ptr::write_bytes(p.as_ptr(), 0, size); } Ok(p) } } pub trait DeallocRef: Sized { type BuildDealloc: BuildDealloc<DeallocRef = Self>; fn get_build_dealloc(&mut self) -> Self::BuildDealloc; unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: NonZeroLayout); } pub trait ReallocRef: Sized { type BuildRealloc: BuildRealloc<ReallocRef = Self>; type Error; fn get_build_realloc(&mut self) -> Self::BuildRealloc; unsafe fn realloc( &mut self, ptr: NonNull<u8>, layout: NonZeroLayout, new_size: usize, ) -> Result<NonNull<u8>, Self::Error>; } macro_rules! impl_alloc_zst { ($ty:tt) => { impl BuildAlloc for $ty { type AllocRef = Self; unsafe fn build_alloc_ref( &mut self, _ptr: NonNull<u8>, _layout: Layout, ) -> Self::AllocRef { Self } } impl BuildDealloc for $ty { type DeallocRef = Self; unsafe fn build_dealloc_ref( &mut self, _ptr: NonNull<u8>, _layout: Layout, ) -> Self::DeallocRef { Self } } impl BuildRealloc for $ty { type ReallocRef = Self; unsafe fn build_realloc_ref( &mut self, _ptr: NonNull<u8>, _layout: Layout, ) -> Self::ReallocRef { Self } } impl AllocRef for $ty { type BuildAlloc = Self; type Error = AllocErr; fn get_build_alloc(&mut self) -> Self::BuildAlloc { Self } fn alloc(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> { unsafe { Alloc::alloc(self, layout.into()) } } fn alloc_zeroed(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> { unsafe { Alloc::alloc_zeroed(self, layout.into()) } } } impl DeallocRef for $ty { type BuildDealloc = Self; fn get_build_dealloc(&mut self) -> Self::BuildDealloc { Self } unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: NonZeroLayout) { Alloc::dealloc(self, ptr, layout.into()) } } impl ReallocRef for $ty { type BuildRealloc = Self; type Error = AllocErr; fn get_build_realloc(&mut self) -> Self::BuildRealloc { Self } unsafe fn realloc( &mut self, ptr: NonNull<u8>, layout: NonZeroLayout, new_size: usize, ) -> Result<NonNull<u8>, Self::Error> { Alloc::realloc(self, ptr, layout.into(), new_size) } } }; } impl_alloc_zst!(Global); #[cfg(feature = "std")] impl_alloc_zst!(System);