Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3// Set the length of the vec when the `SetLenOnDrop` value goes out of scope.
4//
5// The idea is: The length field in SetLenOnDrop is a local variable
6// that the optimizer will see does not alias with any stores through the Vec's data
7// pointer. This is a workaround for alias analysis issue #32155
8pub(super) struct SetLenOnDrop<'a> {
9 len: &'a mut usize,
10 local_len: usize,
11}
12
13impl<'a> SetLenOnDrop<'a> {
14 #[inline]
15 pub(super) fn new(len: &'a mut usize) -> Self {
16 SetLenOnDrop { local_len: *len, len }
17 }
18
19 #[inline]
20 pub(super) fn increment_len(&mut self, increment: usize) {
21 self.local_len += increment;
22 }
23}
24
25impl Drop for SetLenOnDrop<'_> {
26 #[inline]
27 fn drop(&mut self) {
28 *self.len = self.local_len;
29 }
30}