incr len in with_capacity_aligned_zeroed add a test

Signed-off-by: Yuchen Liang <yuchen@neon.tech>
This commit is contained in:
Yuchen Liang
2024-09-29 23:47:00 +00:00
parent 13f1931a09
commit f04c1c230c
2 changed files with 16 additions and 3 deletions

View File

@@ -646,7 +646,8 @@ impl PageCache {
fn new(num_pages: usize, align: usize) -> Self {
assert!(num_pages > 0, "page cache size must be > 0");
let page_buffer = IoBufferMut::with_capacity_aligned(num_pages * PAGE_SZ, align).leak();
let page_buffer =
IoBufferMut::with_capacity_aligned_zeroed(num_pages * PAGE_SZ, align).leak();
let size_metrics = &crate::metrics::PAGE_CACHE_SIZE;
size_metrics.max_bytes.set_page_sz(num_pages);

View File

@@ -64,6 +64,7 @@ impl IoBufferMut {
use bytes::BufMut;
let mut buf = Self::with_capacity_aligned(capacity, align);
buf.put_bytes(0, capacity);
buf.len = capacity;
buf
}
@@ -223,7 +224,7 @@ impl DerefMut for IoBufferMut {
}
}
/// SAFETY: See [`IoBufferMut::advance_mut`]
/// SAFETY: When advancing the internal cursor, the caller needs to make sure the bytes advcanced past have been initialized.
unsafe impl bytes::BufMut for IoBufferMut {
#[inline]
fn remaining_mut(&self) -> usize {
@@ -235,7 +236,7 @@ unsafe impl bytes::BufMut for IoBufferMut {
// SAFETY: Caller needs to make sure the bytes being advanced past have been initialized.
#[inline]
unsafe fn advance_mut(&mut self, cnt: usize) {
let len = self.len();
let len: usize = self.len();
let remaining = self.remaining_mut();
if remaining < cnt {
@@ -317,6 +318,17 @@ mod tests {
assert_eq!(v.as_ptr().align_offset(ALIGN), 0);
}
#[test]
fn test_with_capacity_aligned_zeroed() {
const ALIGN: usize = 4 * 1024;
let v = IoBufferMut::with_capacity_aligned_zeroed(ALIGN, ALIGN);
assert_eq!(v.len(), ALIGN);
assert_eq!(v.capacity(), ALIGN);
assert_eq!(v.align(), ALIGN);
assert_eq!(v.as_ptr().align_offset(ALIGN), 0);
assert_eq!(&v[..], &[0; ALIGN])
}
#[test]
fn test_reserve() {
use bytes::BufMut;