Skip to content

Commit 3e475d6

Browse files
FuuuOverclockingjiangliu
authored andcommitted
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 <q.for.xyz@qq.com>
1 parent 07ab853 commit 3e475d6

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
@@ -12,7 +12,7 @@
1212
1313
use std::io;
1414
use std::os::unix::io::AsRawFd;
15-
use std::ptr::null_mut;
15+
use std::ptr::{null_mut, NonNull};
1616
use std::result;
1717

1818
use crate::bitmap::{Bitmap, BS};
@@ -57,7 +57,7 @@ pub struct MmapRegionBuilder<B = ()> {
5757
prot: i32,
5858
flags: i32,
5959
file_offset: Option<FileOffset>,
60-
raw_ptr: Option<*mut u8>,
60+
raw_ptr: Option<NonNull<u8>>,
6161
hugetlbfs: Option<bool>,
6262
bitmap: B,
6363
}
@@ -119,7 +119,7 @@ impl<B: Bitmap> MmapRegionBuilder<B> {
119119
/// To use this safely, the caller must guarantee that `raw_addr` and `self.size` define a
120120
/// region within a valid mapping that is already present in the process.
121121
pub unsafe fn with_raw_mmap_pointer(mut self, raw_ptr: *mut u8) -> Self {
122-
self.raw_ptr = Some(raw_ptr);
122+
self.raw_ptr = Some(NonNull::new_unchecked(raw_ptr));
123123
self
124124
}
125125

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

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

0 commit comments

Comments
 (0)