Skip to content

Commit bcb614c

Browse files
Use NonNull<u8> in MmapRegionBuilder for saving space
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]>
1 parent b55a713 commit bcb614c

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/mmap_unix.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::error;
1414
use std::fmt;
1515
use std::io;
1616
use std::os::unix::io::AsRawFd;
17-
use std::ptr::null_mut;
17+
use std::ptr::{null_mut, NonNull};
1818
use std::result;
1919

2020
use crate::bitmap::{Bitmap, BS};
@@ -80,7 +80,7 @@ pub struct MmapRegionBuilder<B = ()> {
8080
prot: i32,
8181
flags: i32,
8282
file_offset: Option<FileOffset>,
83-
raw_ptr: Option<*mut u8>,
83+
raw_ptr: Option<NonNull<u8>>,
8484
hugetlbfs: Option<bool>,
8585
bitmap: B,
8686
}
@@ -142,7 +142,7 @@ impl<B: Bitmap> MmapRegionBuilder<B> {
142142
/// To use this safely, the caller must guarantee that `raw_addr` and `self.size` define a
143143
/// region within a valid mapping that is already present in the process.
144144
pub unsafe fn with_raw_mmap_pointer(mut self, raw_ptr: *mut u8) -> Self {
145-
self.raw_ptr = Some(raw_ptr);
145+
self.raw_ptr = Some(NonNull::new_unchecked(raw_ptr));
146146
self
147147
}
148148

@@ -199,7 +199,7 @@ impl<B: Bitmap> MmapRegionBuilder<B> {
199199
// SAFETY: Safe because this call just returns the page size and doesn't have any side
200200
// effects.
201201
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
202-
let addr = self.raw_ptr.unwrap();
202+
let addr = self.raw_ptr.unwrap().as_ptr();
203203

204204
// Check that the pointer to the mapping is page-aligned.
205205
if (addr as usize) & (page_size - 1) != 0 {

0 commit comments

Comments
 (0)