Skip to content

Commit

Permalink
Use NonNull<u8> in MmapRegionBuilder for saving space
Browse files Browse the repository at this point in the history
Updates the `raw_ptr` field in `MmapRegionBuilder` (mmap_unix.rs) from
`Option<*mut u8>` to `Option<NonNull<u8>>`. This modification saves
8 bytes on x86-64 systems, for example.

The public API and functionality remain unchanged.

Signed-off-by: Fuu <[email protected]>
  • Loading branch information
FuuuOverclocking authored and jiangliu committed Nov 2, 2023
1 parent 07ab853 commit 3e475d6
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/mmap_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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};
Expand Down Expand Up @@ -57,7 +57,7 @@ pub struct MmapRegionBuilder<B = ()> {
prot: i32,
flags: i32,
file_offset: Option<FileOffset>,
raw_ptr: Option<*mut u8>,
raw_ptr: Option<NonNull<u8>>,
hugetlbfs: Option<bool>,
bitmap: B,
}
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<B: Bitmap> MmapRegionBuilder<B> {
/// 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
}

Expand Down Expand Up @@ -189,7 +189,7 @@ impl<B: Bitmap> MmapRegionBuilder<B> {
// 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 {
Expand Down

0 comments on commit 3e475d6

Please sign in to comment.