From bcb614c7701a4d144aeba5767d94ee47e9964867 Mon Sep 17 00:00:00 2001 From: Fuu Date: Tue, 18 Apr 2023 13:24:55 +0800 Subject: [PATCH] Use NonNull in MmapRegionBuilder for saving space Updates the `raw_ptr` field in `MmapRegionBuilder` (mmap_unix.rs) from `Option<*mut u8>` to `Option>`. This modification saves 8 bytes on x86-64 systems, for example. The public API and functionality remain unchanged. Signed-off-by: Fuu --- src/mmap_unix.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mmap_unix.rs b/src/mmap_unix.rs index c2eed494..fa43b28a 100644 --- a/src/mmap_unix.rs +++ b/src/mmap_unix.rs @@ -14,7 +14,7 @@ use std::error; use std::fmt; use std::io; use std::os::unix::io::AsRawFd; -use std::ptr::null_mut; +use std::ptr::{null_mut, NonNull}; use std::result; use crate::bitmap::{Bitmap, BS}; @@ -80,7 +80,7 @@ pub struct MmapRegionBuilder { prot: i32, flags: i32, file_offset: Option, - raw_ptr: Option<*mut u8>, + raw_ptr: Option>, hugetlbfs: Option, bitmap: B, } @@ -142,7 +142,7 @@ impl MmapRegionBuilder { /// To use this safely, the caller must guarantee that `raw_addr` and `self.size` define a /// region within a valid mapping that is already present in the process. pub unsafe fn with_raw_mmap_pointer(mut self, raw_ptr: *mut u8) -> Self { - self.raw_ptr = Some(raw_ptr); + self.raw_ptr = Some(NonNull::new_unchecked(raw_ptr)); self } @@ -199,7 +199,7 @@ impl MmapRegionBuilder { // SAFETY: Safe because this call just returns the page size and doesn't have any side // effects. let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize; - let addr = self.raw_ptr.unwrap(); + let addr = self.raw_ptr.unwrap().as_ptr(); // Check that the pointer to the mapping is page-aligned. if (addr as usize) & (page_size - 1) != 0 {