From 9835e2ae736235810b4ea1c162ca5e65c547e770 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:50 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/bumpalo/src/alloc.rs | 2 +- vendor/bumpalo/src/boxed.rs | 15 ++++----- vendor/bumpalo/src/collections/string.rs | 18 ++++++++++ vendor/bumpalo/src/collections/vec.rs | 20 +++++++++++ vendor/bumpalo/src/lib.rs | 58 +++++++++++++++----------------- 5 files changed, 74 insertions(+), 39 deletions(-) mode change 100644 => 100755 vendor/bumpalo/src/lib.rs (limited to 'vendor/bumpalo/src') diff --git a/vendor/bumpalo/src/alloc.rs b/vendor/bumpalo/src/alloc.rs index 0bcc21f22..6947e2a6c 100644 --- a/vendor/bumpalo/src/alloc.rs +++ b/vendor/bumpalo/src/alloc.rs @@ -752,7 +752,7 @@ pub unsafe trait Alloc { match (Layout::array::(n_old), Layout::array::(n_new)) { (Ok(ref k_old), Ok(ref k_new)) if k_old.size() > 0 && k_new.size() > 0 => { debug_assert!(k_old.align() == k_new.align()); - self.realloc(ptr.cast(), k_old.clone(), k_new.size()) + self.realloc(ptr.cast(), *k_old, k_new.size()) .map(NonNull::cast) } _ => Err(AllocErr), diff --git a/vendor/bumpalo/src/boxed.rs b/vendor/bumpalo/src/boxed.rs index cf9f4d6fe..af0737cfb 100644 --- a/vendor/bumpalo/src/boxed.rs +++ b/vendor/bumpalo/src/boxed.rs @@ -130,7 +130,7 @@ use { future::Future, hash::{Hash, Hasher}, iter::FusedIterator, - mem, + mem::ManuallyDrop, ops::{Deref, DerefMut}, pin::Pin, task::{Context, Poll}, @@ -280,9 +280,8 @@ impl<'a, T: ?Sized> Box<'a, T> { /// ``` #[inline] pub fn into_raw(b: Box<'a, T>) -> *mut T { - let ptr = b.0 as *mut T; - mem::forget(b); - ptr + let mut b = ManuallyDrop::new(b); + b.deref_mut().0 as *mut T } /// Consumes and leaks the `Box`, returning a mutable reference, @@ -662,9 +661,9 @@ impl<'a, F: ?Sized + Future + Unpin> Future for Box<'a, F> { /// This impl replaces unsize coercion. impl<'a, T, const N: usize> From> for Box<'a, [T]> { - fn from(mut arr: Box<'a, [T; N]>) -> Box<'a, [T]> { + fn from(arr: Box<'a, [T; N]>) -> Box<'a, [T]> { + let mut arr = ManuallyDrop::new(arr); let ptr = core::ptr::slice_from_raw_parts_mut(arr.as_mut_ptr(), N); - mem::forget(arr); unsafe { Box::from_raw(ptr) } } } @@ -672,10 +671,10 @@ impl<'a, T, const N: usize> From> for Box<'a, [T]> { /// This impl replaces unsize coercion. impl<'a, T, const N: usize> TryFrom> for Box<'a, [T; N]> { type Error = Box<'a, [T]>; - fn try_from(mut slice: Box<'a, [T]>) -> Result, Box<'a, [T]>> { + fn try_from(slice: Box<'a, [T]>) -> Result, Box<'a, [T]>> { if slice.len() == N { + let mut slice = ManuallyDrop::new(slice); let ptr = slice.as_mut_ptr() as *mut [T; N]; - mem::forget(slice); Ok(unsafe { Box::from_raw(ptr) }) } else { Err(slice) diff --git a/vendor/bumpalo/src/collections/string.rs b/vendor/bumpalo/src/collections/string.rs index 6b7af9a4b..ffd1db92d 100644 --- a/vendor/bumpalo/src/collections/string.rs +++ b/vendor/bumpalo/src/collections/string.rs @@ -798,6 +798,24 @@ impl<'bump> String<'bump> { String { vec: bytes } } + /// Returns a shared reference to the allocator backing this `String`. + /// + /// # Examples + /// + /// ``` + /// use bumpalo::{Bump, collections::String}; + /// + /// // uses the same allocator as the provided `String` + /// fn copy_string<'bump>(s: &String<'bump>) -> &'bump str { + /// s.bump().alloc_str(s.as_str()) + /// } + /// ``` + #[inline] + #[must_use] + pub fn bump(&self) -> &'bump Bump { + self.vec.bump() + } + /// Converts a `String` into a byte vector. /// /// This consumes the `String`, so we do not need to copy its contents. diff --git a/vendor/bumpalo/src/collections/vec.rs b/vendor/bumpalo/src/collections/vec.rs index b5a60029e..312aa055b 100644 --- a/vendor/bumpalo/src/collections/vec.rs +++ b/vendor/bumpalo/src/collections/vec.rs @@ -675,6 +675,26 @@ impl<'bump, T: 'bump> Vec<'bump, T> { } } + /// Returns a shared reference to the allocator backing this `Vec`. + /// + /// # Examples + /// + /// ``` + /// use bumpalo::{Bump, collections::Vec}; + /// + /// // uses the same allocator as the provided `Vec` + /// fn add_strings<'bump>(vec: &mut Vec<'bump, &'bump str>) { + /// for string in ["foo", "bar", "baz"] { + /// vec.push(vec.bump().alloc_str(string)); + /// } + /// } + /// ``` + #[inline] + #[must_use] + pub fn bump(&self) -> &'bump Bump { + self.buf.bump() + } + /// Returns the number of elements the vector can hold without /// reallocating. /// diff --git a/vendor/bumpalo/src/lib.rs b/vendor/bumpalo/src/lib.rs old mode 100644 new mode 100755 index be68365c8..db5d41bbd --- a/vendor/bumpalo/src/lib.rs +++ b/vendor/bumpalo/src/lib.rs @@ -354,7 +354,7 @@ static EMPTY_CHUNK: EmptyChunkFooter = EmptyChunkFooter(ChunkFooter { impl EmptyChunkFooter { fn get(&'static self) -> NonNull { - unsafe { NonNull::new_unchecked(&self.0 as *const ChunkFooter as *mut ChunkFooter) } + NonNull::from(&self.0) } } @@ -463,12 +463,8 @@ struct NewChunkMemoryDetails { /// Wrapper around `Layout::from_size_align` that adds debug assertions. #[inline] -unsafe fn layout_from_size_align(size: usize, align: usize) -> Layout { - if cfg!(debug_assertions) { - Layout::from_size_align(size, align).unwrap() - } else { - Layout::from_size_align_unchecked(size, align) - } +fn layout_from_size_align(size: usize, align: usize) -> Result { + Layout::from_size_align(size, align).map_err(|_| AllocErr) } #[inline(never)] @@ -476,12 +472,6 @@ fn allocation_size_overflow() -> T { panic!("requested allocation size overflowed") } -// This can be migrated to directly use `usize::abs_diff` when the MSRV -// reaches `1.60` -fn abs_diff(a: usize, b: usize) -> usize { - usize::max(a, b) - usize::min(a, b) -} - impl Bump { /// Construct a new arena to bump allocate into. /// @@ -535,7 +525,7 @@ impl Bump { }); } - let layout = unsafe { layout_from_size_align(capacity, 1) }; + let layout = layout_from_size_align(capacity, 1)?; let chunk_footer = unsafe { Self::new_chunk( @@ -600,7 +590,7 @@ impl Bump { if allocated_bytes > allocation_limit { None } else { - Some(abs_diff(allocation_limit, allocated_bytes)) + Some(usize::abs_diff(allocation_limit, allocated_bytes)) } }) } @@ -682,7 +672,7 @@ impl Bump { size, } = new_chunk_memory_details; - let layout = layout_from_size_align(size, align); + let layout = layout_from_size_align(size, align).ok()?; debug_assert!(size >= requested_layout.size()); @@ -1470,6 +1460,7 @@ impl Bump { /// Slow path allocation for when we need to allocate a new chunk from the /// parent bump set because there isn't enough room in our current chunk. #[inline(never)] + #[cold] fn alloc_layout_slow(&self, layout: Layout) -> Option> { unsafe { let size = layout.size(); @@ -1488,21 +1479,14 @@ impl Bump { .checked_mul(2)? .max(min_new_chunk_size); let chunk_memory_details = iter::from_fn(|| { - let bypass_min_chunk_size_for_small_limits = match self.allocation_limit() { - Some(limit) - if layout.size() < limit + let bypass_min_chunk_size_for_small_limits = matches!(self.allocation_limit(), Some(limit) if layout.size() < limit && base_size >= layout.size() && limit < DEFAULT_CHUNK_SIZE_WITHOUT_FOOTER - && self.allocated_bytes() == 0 => - { - true - } - _ => false, - }; + && self.allocated_bytes() == 0); if base_size >= min_new_chunk_size || bypass_min_chunk_size_for_small_limits { let size = base_size; - base_size = base_size / 2; + base_size /= 2; Bump::new_chunk_memory_details(Some(size), layout) } else { None @@ -1678,6 +1662,10 @@ impl Bump { /// on it only counting the sum of the sizes of the things /// you've allocated in the arena. /// + /// The allocated bytes do not include the size of bumpalo's metadata, + /// so the amount of memory requested from the Rust allocator is higher + /// than the returned value. + /// /// ## Example /// /// ``` @@ -1692,6 +1680,16 @@ impl Bump { unsafe { footer.as_ref().allocated_bytes } } + /// Calculates the number of bytes requested from the Rust allocator for this `Bump`. + /// + /// This number is equal to the [`allocated_bytes()`](Self::allocated_bytes) plus + /// the size of the bump metadata. + pub fn allocated_bytes_including_metadata(&self) -> usize { + let metadata_size = + unsafe { self.iter_allocated_chunks_raw().count() * mem::size_of::() }; + self.allocated_bytes() + metadata_size + } + #[inline] unsafe fn is_last_allocation(&self, ptr: NonNull) -> bool { let footer = self.current_chunk_footer.get(); @@ -1746,9 +1744,9 @@ impl Bump { // in the `if` condition. ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr(), new_size); - return Ok(new_ptr); + Ok(new_ptr) } else { - return Ok(ptr); + Ok(ptr) } } @@ -1768,7 +1766,7 @@ impl Bump { // reuse the currently allocated space. let delta = new_size - old_size; if let Some(p) = - self.try_alloc_layout_fast(layout_from_size_align(delta, old_layout.align())) + self.try_alloc_layout_fast(layout_from_size_align(delta, old_layout.align())?) { ptr::copy(ptr.as_ptr(), p.as_ptr(), old_size); return Ok(p); @@ -1879,7 +1877,7 @@ unsafe impl<'a> alloc::Alloc for &'a Bump { return self.try_alloc_layout(layout); } - let new_layout = layout_from_size_align(new_size, layout.align()); + let new_layout = layout_from_size_align(new_size, layout.align())?; if new_size <= old_size { self.shrink(ptr, layout, new_layout) } else { -- cgit v1.2.3