@@ -411,7 +411,9 @@ pub fn munlockall() -> Result<()> {
411
411
unsafe { Errno :: result ( libc:: munlockall ( ) ) } . map ( drop)
412
412
}
413
413
414
- /// allocate memory, or map files or devices into memory
414
+ /// Allocate memory, or map files or devices into memory
415
+ ///
416
+ /// For anonymous mappings (`MAP_ANON`/`MAP_ANONYMOUS`), see [mmap_anonymous].
415
417
///
416
418
/// # Safety
417
419
///
@@ -423,13 +425,12 @@ pub unsafe fn mmap<F: AsFd>(
423
425
length : NonZeroUsize ,
424
426
prot : ProtFlags ,
425
427
flags : MapFlags ,
426
- f : Option < F > ,
428
+ f : F ,
427
429
offset : off_t ,
428
430
) -> Result < * mut c_void > {
429
- let ptr =
430
- addr. map_or ( std:: ptr:: null_mut ( ) , |a| usize:: from ( a) as * mut c_void ) ;
431
+ let ptr = addr. map_or ( std:: ptr:: null_mut ( ) , |a| a. get ( ) as * mut c_void ) ;
431
432
432
- let fd = f. map ( |f| f . as_fd ( ) . as_raw_fd ( ) ) . unwrap_or ( - 1 ) ;
433
+ let fd = f. as_fd ( ) . as_raw_fd ( ) ;
433
434
let ret =
434
435
libc:: mmap ( ptr, length. into ( ) , prot. bits ( ) , flags. bits ( ) , fd, offset) ;
435
436
@@ -440,6 +441,34 @@ pub unsafe fn mmap<F: AsFd>(
440
441
}
441
442
}
442
443
444
+ /// Create an anonymous memory mapping.
445
+ ///
446
+ /// This function is a wrapper around [`mmap`]:
447
+ /// `mmap(ptr, len, prot, MAP_ANONYMOUS | flags, -1, 0)`.
448
+ ///
449
+ /// # Safety
450
+ ///
451
+ /// See the [`mmap(2)`] man page for detailed requirements.
452
+ ///
453
+ /// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html
454
+ pub unsafe fn mmap_anonymous (
455
+ addr : Option < NonZeroUsize > ,
456
+ length : NonZeroUsize ,
457
+ prot : ProtFlags ,
458
+ flags : MapFlags ,
459
+ ) -> Result < * mut c_void > {
460
+ let ptr = addr. map_or ( std:: ptr:: null_mut ( ) , |a| a. get ( ) as * mut c_void ) ;
461
+
462
+ let flags = MapFlags :: MAP_ANONYMOUS | flags;
463
+ let ret = libc:: mmap ( ptr, length. into ( ) , prot. bits ( ) , flags. bits ( ) , -1 , 0 ) ;
464
+
465
+ if ret == libc:: MAP_FAILED {
466
+ Err ( Errno :: last ( ) )
467
+ } else {
468
+ Ok ( ret)
469
+ }
470
+ }
471
+
443
472
/// Expands (or shrinks) an existing memory mapping, potentially moving it at
444
473
/// the same time.
445
474
///
@@ -519,14 +548,14 @@ pub unsafe fn madvise(
519
548
///
520
549
/// ```
521
550
/// # use nix::libc::size_t;
522
- /// # use nix::sys::mman::{mmap , mprotect, MapFlags, ProtFlags};
551
+ /// # use nix::sys::mman::{mmap_anonymous , mprotect, MapFlags, ProtFlags};
523
552
/// # use std::ptr;
524
553
/// # use std::os::unix::io::BorrowedFd;
525
554
/// const ONE_K: size_t = 1024;
526
555
/// let one_k_non_zero = std::num::NonZeroUsize::new(ONE_K).unwrap();
527
556
/// let mut slice: &mut [u8] = unsafe {
528
- /// let mem = mmap::<BorrowedFd> (None, one_k_non_zero, ProtFlags::PROT_NONE,
529
- /// MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, None, 0) .unwrap();
557
+ /// let mem = mmap_anonymous (None, one_k_non_zero, ProtFlags::PROT_NONE, MapFlags::MAP_PRIVATE)
558
+ /// .unwrap();
530
559
/// mprotect(mem, ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE).unwrap();
531
560
/// std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K)
532
561
/// };
0 commit comments