diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-03-10 06:47:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-10 06:47:52 +0100 |
| commit | 6ad5e69b5af63f9044a4fab81450d49303ae8839 (patch) | |
| tree | 29546e55989fc8d88004ed54117d826597a5064a /src/libstd | |
| parent | 977d69fc1992b0cef8d8ff020261d5bf9ede7424 (diff) | |
| parent | f77afc8f9c63d789519c0b1a733462ca654d894a (diff) | |
Rollup merge of #69799 - TimDiekmann:zst, r=Amanieu
Allow ZSTs in `AllocRef` Allows ZSTs in all `AllocRef` methods. The implementation of `AllocRef` for `Global` and `System` were adjusted to reflect those changes. This is the second item on the roadmap to support ZSTs in `AllocRef`: https://github.com/rust-lang/wg-allocators/issues/38#issuecomment-595861542 After this has landed, I will adapt `RawVec`, but since this will be a pretty big overhaul, it makes sense to do a different PR for it. ~~Requires #69794 to land first~~ r? @Amanieu
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/alloc.rs | 47 |
1 files changed, 36 insertions, 11 deletions
diff --git a/src/libstd/alloc.rs b/src/libstd/alloc.rs index 2da18e06d99..25f3ddcbeba 100644 --- a/src/libstd/alloc.rs +++ b/src/libstd/alloc.rs @@ -133,24 +133,41 @@ pub use alloc_crate::alloc::*; #[derive(Debug, Default, Copy, Clone)] pub struct System; -// The AllocRef impl just forwards to the GlobalAlloc impl, which is in `std::sys::*::alloc`. +// The AllocRef impl checks the layout size to be non-zero and forwards to the GlobalAlloc impl, +// which is in `std::sys::*::alloc`. #[unstable(feature = "allocator_api", issue = "32838")] unsafe impl AllocRef for System { #[inline] - unsafe fn alloc(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> { - NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr).map(|p| (p, layout.size())) + fn alloc(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> { + if layout.size() == 0 { + Ok((layout.dangling(), 0)) + } else { + unsafe { + NonNull::new(GlobalAlloc::alloc(self, layout)) + .ok_or(AllocErr) + .map(|p| (p, layout.size())) + } + } } #[inline] - unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> { - NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)) - .ok_or(AllocErr) - .map(|p| (p, layout.size())) + fn alloc_zeroed(&mut self, layout: Layout) -> Result<(NonNull<u8>, usize), AllocErr> { + if layout.size() == 0 { + Ok((layout.dangling(), 0)) + } else { + unsafe { + NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)) + .ok_or(AllocErr) + .map(|p| (p, layout.size())) + } + } } #[inline] unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) { - GlobalAlloc::dealloc(self, ptr.as_ptr(), layout) + if layout.size() != 0 { + GlobalAlloc::dealloc(self, ptr.as_ptr(), layout) + } } #[inline] @@ -160,9 +177,17 @@ unsafe impl AllocRef for System { layout: Layout, new_size: usize, ) -> Result<(NonNull<u8>, usize), AllocErr> { - NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)) - .ok_or(AllocErr) - .map(|p| (p, new_size)) + match (layout.size(), new_size) { + (0, 0) => Ok((layout.dangling(), 0)), + (0, _) => self.alloc(Layout::from_size_align_unchecked(new_size, layout.align())), + (_, 0) => { + self.dealloc(ptr, layout); + Ok((layout.dangling(), 0)) + } + (_, _) => NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)) + .ok_or(AllocErr) + .map(|p| (p, new_size)), + } } } |
