mirror of
https://gitee.com/bianbu-linux/linux-6.6
synced 2025-04-24 14:07:52 -04:00
This is a subset of the Rust standard library `alloc` crate, version 1.62.0, licensed under "Apache-2.0 OR MIT", from: https://github.com/rust-lang/rust/tree/1.62.0/library/alloc/src The file is copied as-is, with no modifications whatsoever (not even adding the SPDX identifiers). For copyright details, please see: https://github.com/rust-lang/rust/blob/1.62.0/COPYRIGHT Signed-off-by: Asahi Lina <lina@asahilina.net> Link: https://lore.kernel.org/r/20230224-rust-vec-v1-1-733b5b5a57c5@asahilina.net [ Import version 1.62.0 instead, to match the one in mainline. ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
28 lines
772 B
Rust
28 lines
772 B
Rust
// Set the length of the vec when the `SetLenOnDrop` value goes out of scope.
|
|
//
|
|
// The idea is: The length field in SetLenOnDrop is a local variable
|
|
// that the optimizer will see does not alias with any stores through the Vec's data
|
|
// pointer. This is a workaround for alias analysis issue #32155
|
|
pub(super) struct SetLenOnDrop<'a> {
|
|
len: &'a mut usize,
|
|
local_len: usize,
|
|
}
|
|
|
|
impl<'a> SetLenOnDrop<'a> {
|
|
#[inline]
|
|
pub(super) fn new(len: &'a mut usize) -> Self {
|
|
SetLenOnDrop { local_len: *len, len }
|
|
}
|
|
|
|
#[inline]
|
|
pub(super) fn increment_len(&mut self, increment: usize) {
|
|
self.local_len += increment;
|
|
}
|
|
}
|
|
|
|
impl Drop for SetLenOnDrop<'_> {
|
|
#[inline]
|
|
fn drop(&mut self) {
|
|
*self.len = self.local_len;
|
|
}
|
|
}
|