LCOV - code coverage report
Current view: top level - pageserver/src/virtual_file/owned_buffers_io/aligned_buffer - buffer_mut.rs (source / functions) Coverage Total Hit
Test: b4ae4c4857f9ef3e144e982a35ee23bc84c71983.info Lines: 90.5 % 190 172
Test Date: 2024-10-22 22:13:45 Functions: 85.5 % 55 47

            Line data    Source code
       1              : use std::ops::{Deref, DerefMut};
       2              : 
       3              : use super::{
       4              :     alignment::{Alignment, ConstAlign},
       5              :     buffer::AlignedBuffer,
       6              :     raw::RawAlignedBuffer,
       7              : };
       8              : 
       9              : /// A mutable aligned buffer type.
      10              : #[derive(Debug)]
      11              : pub struct AlignedBufferMut<A: Alignment> {
      12              :     raw: RawAlignedBuffer<A>,
      13              : }
      14              : 
      15              : impl<const A: usize> AlignedBufferMut<ConstAlign<A>> {
      16              :     /// Constructs a new, empty `IoBufferMut` with at least the specified capacity and alignment.
      17              :     ///
      18              :     /// The buffer will be able to hold at most `capacity` elements and will never resize.
      19              :     ///
      20              :     ///
      21              :     /// # Panics
      22              :     ///
      23              :     /// Panics if the new capacity exceeds `isize::MAX` _bytes_, or if the following alignment requirement is not met:
      24              :     /// * `align` must not be zero,
      25              :     ///
      26              :     /// * `align` must be a power of two,
      27              :     ///
      28              :     /// * `capacity`, when rounded up to the nearest multiple of `align`,
      29              :     ///    must not overflow isize (i.e., the rounded value must be
      30              :     ///    less than or equal to `isize::MAX`).
      31       969277 :     pub fn with_capacity(capacity: usize) -> Self {
      32       969277 :         AlignedBufferMut {
      33       969277 :             raw: RawAlignedBuffer::with_capacity(capacity),
      34       969277 :         }
      35       969277 :     }
      36              : 
      37              :     /// Constructs a new `IoBufferMut` with at least the specified capacity and alignment, filled with zeros.
      38          296 :     pub fn with_capacity_zeroed(capacity: usize) -> Self {
      39              :         use bytes::BufMut;
      40          296 :         let mut buf = Self::with_capacity(capacity);
      41          296 :         buf.put_bytes(0, capacity);
      42          296 :         // SAFETY: `put_bytes` filled the entire buffer.
      43          296 :         unsafe { buf.set_len(capacity) };
      44          296 :         buf
      45          296 :     }
      46              : }
      47              : 
      48              : impl<A: Alignment> AlignedBufferMut<A> {
      49              :     /// Returns the total number of bytes the buffer can hold.
      50              :     #[inline]
      51    111806028 :     pub fn capacity(&self) -> usize {
      52    111806028 :         self.raw.capacity()
      53    111806028 :     }
      54              : 
      55              :     /// Returns the alignment of the buffer.
      56              :     #[inline]
      57           18 :     pub fn align(&self) -> usize {
      58           18 :         self.raw.align()
      59           18 :     }
      60              : 
      61              :     /// Returns the number of bytes in the buffer, also referred to as its 'length'.
      62              :     #[inline]
      63    154893137 :     pub fn len(&self) -> usize {
      64    154893137 :         self.raw.len()
      65    154893137 :     }
      66              : 
      67              :     /// Force the length of the buffer to `new_len`.
      68              :     #[inline]
      69     36941730 :     unsafe fn set_len(&mut self, new_len: usize) {
      70     36941730 :         self.raw.set_len(new_len)
      71     36941730 :     }
      72              : 
      73              :     #[inline]
      74      4167199 :     fn as_ptr(&self) -> *const u8 {
      75      4167199 :         self.raw.as_ptr()
      76      4167199 :     }
      77              : 
      78              :     #[inline]
      79     37923298 :     fn as_mut_ptr(&mut self) -> *mut u8 {
      80     37923298 :         self.raw.as_mut_ptr()
      81     37923298 :     }
      82              : 
      83              :     /// Extracts a slice containing the entire buffer.
      84              :     ///
      85              :     /// Equivalent to `&s[..]`.
      86              :     #[inline]
      87      3784716 :     fn as_slice(&self) -> &[u8] {
      88      3784716 :         self.raw.as_slice()
      89      3784716 :     }
      90              : 
      91              :     /// Extracts a mutable slice of the entire buffer.
      92              :     ///
      93              :     /// Equivalent to `&mut s[..]`.
      94            0 :     fn as_mut_slice(&mut self) -> &mut [u8] {
      95            0 :         self.raw.as_mut_slice()
      96            0 :     }
      97              : 
      98              :     /// Drops the all the contents of the buffer, setting its length to `0`.
      99              :     #[inline]
     100           26 :     pub fn clear(&mut self) {
     101           26 :         self.raw.clear()
     102           26 :     }
     103              : 
     104              :     /// Reserves capacity for at least `additional` more bytes to be inserted
     105              :     /// in the given `IoBufferMut`. The collection may reserve more space to
     106              :     /// speculatively avoid frequent reallocations. After calling `reserve`,
     107              :     /// capacity will be greater than or equal to `self.len() + additional`.
     108              :     /// Does nothing if capacity is already sufficient.
     109              :     ///
     110              :     /// # Panics
     111              :     ///
     112              :     /// Panics if the new capacity exceeds `isize::MAX` _bytes_.
     113           22 :     pub fn reserve(&mut self, additional: usize) {
     114           22 :         self.raw.reserve(additional);
     115           22 :     }
     116              : 
     117              :     /// Shortens the buffer, keeping the first len bytes.
     118           10 :     pub fn truncate(&mut self, len: usize) {
     119           10 :         self.raw.truncate(len);
     120           10 :     }
     121              : 
     122              :     /// Consumes and leaks the `IoBufferMut`, returning a mutable reference to the contents, &'a mut [u8].
     123           84 :     pub fn leak<'a>(self) -> &'a mut [u8] {
     124           84 :         self.raw.leak()
     125           84 :     }
     126              : 
     127          968 :     pub fn freeze(self) -> AlignedBuffer<A> {
     128          968 :         let len = self.len();
     129          968 :         AlignedBuffer::from_raw(self.raw, 0..len)
     130          968 :     }
     131              : }
     132              : 
     133              : impl<A: Alignment> Deref for AlignedBufferMut<A> {
     134              :     type Target = [u8];
     135              : 
     136      3588090 :     fn deref(&self) -> &Self::Target {
     137      3588090 :         self.as_slice()
     138      3588090 :     }
     139              : }
     140              : 
     141              : impl<A: Alignment> DerefMut for AlignedBufferMut<A> {
     142            0 :     fn deref_mut(&mut self) -> &mut Self::Target {
     143            0 :         self.as_mut_slice()
     144            0 :     }
     145              : }
     146              : 
     147              : impl<A: Alignment> AsRef<[u8]> for AlignedBufferMut<A> {
     148            0 :     fn as_ref(&self) -> &[u8] {
     149            0 :         self.as_slice()
     150            0 :     }
     151              : }
     152              : 
     153              : impl<A: Alignment> AsMut<[u8]> for AlignedBufferMut<A> {
     154            0 :     fn as_mut(&mut self) -> &mut [u8] {
     155            0 :         self.as_mut_slice()
     156            0 :     }
     157              : }
     158              : 
     159              : impl<A: Alignment> PartialEq<[u8]> for AlignedBufferMut<A> {
     160       196626 :     fn eq(&self, other: &[u8]) -> bool {
     161       196626 :         self.as_slice().eq(other)
     162       196626 :     }
     163              : }
     164              : 
     165              : /// SAFETY: When advancing the internal cursor, the caller needs to make sure the bytes advcanced past have been initialized.
     166              : unsafe impl<A: Alignment> bytes::BufMut for AlignedBufferMut<A> {
     167              :     #[inline]
     168     72106076 :     fn remaining_mut(&self) -> usize {
     169     72106076 :         // Although a `Vec` can have at most isize::MAX bytes, we never want to grow `IoBufferMut`.
     170     72106076 :         // Thus, it can have at most `self.capacity` bytes.
     171     72106076 :         self.capacity() - self.len()
     172     72106076 :     }
     173              : 
     174              :     // SAFETY: Caller needs to make sure the bytes being advanced past have been initialized.
     175              :     #[inline]
     176     36053036 :     unsafe fn advance_mut(&mut self, cnt: usize) {
     177     36053036 :         let len = self.len();
     178     36053036 :         let remaining = self.remaining_mut();
     179     36053036 : 
     180     36053036 :         if remaining < cnt {
     181            0 :             panic_advance(cnt, remaining);
     182     36053036 :         }
     183     36053036 : 
     184     36053036 :         // Addition will not overflow since the sum is at most the capacity.
     185     36053036 :         self.set_len(len + cnt);
     186     36053036 :     }
     187              : 
     188              :     #[inline]
     189     36053036 :     fn chunk_mut(&mut self) -> &mut bytes::buf::UninitSlice {
     190     36053036 :         let cap = self.capacity();
     191     36053036 :         let len = self.len();
     192     36053036 : 
     193     36053036 :         // SAFETY: Since `self.ptr` is valid for `cap` bytes, `self.ptr.add(len)` must be
     194     36053036 :         // valid for `cap - len` bytes. The subtraction will not underflow since
     195     36053036 :         // `len <= cap`.
     196     36053036 :         unsafe {
     197     36053036 :             bytes::buf::UninitSlice::from_raw_parts_mut(self.as_mut_ptr().add(len), cap - len)
     198     36053036 :         }
     199     36053036 :     }
     200              : }
     201              : 
     202              : /// Panic with a nice error message.
     203              : #[cold]
     204            0 : fn panic_advance(idx: usize, len: usize) -> ! {
     205            0 :     panic!(
     206            0 :         "advance out of bounds: the len is {} but advancing by {}",
     207            0 :         len, idx
     208            0 :     );
     209              : }
     210              : 
     211              : /// Safety: [`AlignedBufferMut`] has exclusive ownership of the io buffer,
     212              : /// and the underlying pointer remains stable while io-uring is owning the buffer.
     213              : /// The tokio-epoll-uring crate itself will not resize the buffer and will respect
     214              : /// [`tokio_epoll_uring::IoBuf::bytes_total`].
     215              : unsafe impl<A: Alignment> tokio_epoll_uring::IoBuf for AlignedBufferMut<A> {
     216      4167181 :     fn stable_ptr(&self) -> *const u8 {
     217      4167181 :         self.as_ptr()
     218      4167181 :     }
     219              : 
     220      9066477 :     fn bytes_init(&self) -> usize {
     221      9066477 :         self.len()
     222      9066477 :     }
     223              : 
     224      3447646 :     fn bytes_total(&self) -> usize {
     225      3447646 :         self.capacity()
     226      3447646 :     }
     227              : }
     228              : 
     229              : // SAFETY: See above.
     230              : unsafe impl<A: Alignment> tokio_epoll_uring::IoBufMut for AlignedBufferMut<A> {
     231      1870262 :     fn stable_mut_ptr(&mut self) -> *mut u8 {
     232      1870262 :         self.as_mut_ptr()
     233      1870262 :     }
     234              : 
     235      1115127 :     unsafe fn set_init(&mut self, init_len: usize) {
     236      1115127 :         if self.len() < init_len {
     237       888398 :             self.set_len(init_len);
     238       888398 :         }
     239      1115127 :     }
     240              : }
     241              : 
     242              : #[cfg(test)]
     243              : mod tests {
     244              : 
     245              :     use super::*;
     246              : 
     247              :     const ALIGN: usize = 4 * 1024;
     248              :     type TestIoBufferMut = AlignedBufferMut<ConstAlign<ALIGN>>;
     249              : 
     250              :     #[test]
     251            2 :     fn test_with_capacity() {
     252            2 :         let v = TestIoBufferMut::with_capacity(ALIGN * 4);
     253            2 :         assert_eq!(v.len(), 0);
     254            2 :         assert_eq!(v.capacity(), ALIGN * 4);
     255            2 :         assert_eq!(v.align(), ALIGN);
     256            2 :         assert_eq!(v.as_ptr().align_offset(ALIGN), 0);
     257              : 
     258            2 :         let v = TestIoBufferMut::with_capacity(ALIGN / 2);
     259            2 :         assert_eq!(v.len(), 0);
     260            2 :         assert_eq!(v.capacity(), ALIGN / 2);
     261            2 :         assert_eq!(v.align(), ALIGN);
     262            2 :         assert_eq!(v.as_ptr().align_offset(ALIGN), 0);
     263            2 :     }
     264              : 
     265              :     #[test]
     266            2 :     fn test_with_capacity_zeroed() {
     267            2 :         let v = TestIoBufferMut::with_capacity_zeroed(ALIGN);
     268            2 :         assert_eq!(v.len(), ALIGN);
     269            2 :         assert_eq!(v.capacity(), ALIGN);
     270            2 :         assert_eq!(v.align(), ALIGN);
     271            2 :         assert_eq!(v.as_ptr().align_offset(ALIGN), 0);
     272            2 :         assert_eq!(&v[..], &[0; ALIGN])
     273            2 :     }
     274              : 
     275              :     #[test]
     276            2 :     fn test_reserve() {
     277              :         use bytes::BufMut;
     278            2 :         let mut v = TestIoBufferMut::with_capacity(ALIGN);
     279            2 :         let capacity = v.capacity();
     280            2 :         v.reserve(capacity);
     281            2 :         assert_eq!(v.capacity(), capacity);
     282            2 :         let data = [b'a'; ALIGN];
     283            2 :         v.put(&data[..]);
     284            2 :         v.reserve(capacity);
     285            2 :         assert!(v.capacity() >= capacity * 2);
     286            2 :         assert_eq!(&v[..], &data[..]);
     287            2 :         let capacity = v.capacity();
     288            2 :         v.clear();
     289            2 :         v.reserve(capacity);
     290            2 :         assert_eq!(capacity, v.capacity());
     291            2 :     }
     292              : 
     293              :     #[test]
     294            2 :     fn test_bytes_put() {
     295              :         use bytes::BufMut;
     296            2 :         let mut v = TestIoBufferMut::with_capacity(ALIGN * 4);
     297            2 :         let x = [b'a'; ALIGN];
     298              : 
     299            6 :         for _ in 0..2 {
     300           20 :             for _ in 0..4 {
     301           16 :                 v.put(&x[..]);
     302           16 :             }
     303            4 :             assert_eq!(v.len(), ALIGN * 4);
     304            4 :             assert_eq!(v.capacity(), ALIGN * 4);
     305            4 :             assert_eq!(v.align(), ALIGN);
     306            4 :             assert_eq!(v.as_ptr().align_offset(ALIGN), 0);
     307            4 :             v.clear()
     308              :         }
     309            2 :         assert_eq!(v.len(), 0);
     310            2 :         assert_eq!(v.capacity(), ALIGN * 4);
     311            2 :         assert_eq!(v.align(), ALIGN);
     312            2 :         assert_eq!(v.as_ptr().align_offset(ALIGN), 0);
     313            2 :     }
     314              : 
     315              :     #[test]
     316              :     #[should_panic]
     317            2 :     fn test_bytes_put_panic() {
     318              :         use bytes::BufMut;
     319              :         const ALIGN: usize = 4 * 1024;
     320            2 :         let mut v = TestIoBufferMut::with_capacity(ALIGN * 4);
     321            2 :         let x = [b'a'; ALIGN];
     322           12 :         for _ in 0..5 {
     323           10 :             v.put_slice(&x[..]);
     324           10 :         }
     325            2 :     }
     326              : 
     327              :     #[test]
     328            2 :     fn test_io_buf_put_slice() {
     329              :         use tokio_epoll_uring::BoundedBufMut;
     330              :         const ALIGN: usize = 4 * 1024;
     331            2 :         let mut v = TestIoBufferMut::with_capacity(ALIGN);
     332            2 :         let x = [b'a'; ALIGN];
     333              : 
     334            6 :         for _ in 0..2 {
     335            4 :             v.put_slice(&x[..]);
     336            4 :             assert_eq!(v.len(), ALIGN);
     337            4 :             assert_eq!(v.capacity(), ALIGN);
     338            4 :             assert_eq!(v.align(), ALIGN);
     339            4 :             assert_eq!(v.as_ptr().align_offset(ALIGN), 0);
     340            4 :             v.clear()
     341              :         }
     342            2 :         assert_eq!(v.len(), 0);
     343            2 :         assert_eq!(v.capacity(), ALIGN);
     344            2 :         assert_eq!(v.align(), ALIGN);
     345            2 :         assert_eq!(v.as_ptr().align_offset(ALIGN), 0);
     346            2 :     }
     347              : }
        

Generated by: LCOV version 2.1-beta