@@ -426,8 +426,7 @@ pub unsafe fn mmap<F: AsFd>(
426
426
f : Option < F > ,
427
427
offset : off_t ,
428
428
) -> Result < * mut c_void > {
429
- let ptr =
430
- addr. map_or ( std:: ptr:: null_mut ( ) , |a| usize:: from ( a) as * mut c_void ) ;
429
+ let ptr = addr. map_or ( std:: ptr:: null_mut ( ) , |a| a. get ( ) as * mut c_void ) ;
431
430
432
431
let fd = f. map ( |f| f. as_fd ( ) . as_raw_fd ( ) ) . unwrap_or ( -1 ) ;
433
432
let ret =
@@ -440,6 +439,34 @@ pub unsafe fn mmap<F: AsFd>(
440
439
}
441
440
}
442
441
442
+ /// Create an anonymous memory mapping.
443
+ ///
444
+ /// This function is a wrapper around [`mmap`]:
445
+ /// `mmap(ptr, len, prot, MAP_ANONYMOUS | flags, -1, 0)`.
446
+ ///
447
+ /// # Safety
448
+ ///
449
+ /// See the [`mmap(2)`] man page for detailed requirements.
450
+ ///
451
+ /// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html
452
+ pub unsafe fn mmap_anonymous (
453
+ addr : Option < NonZeroUsize > ,
454
+ length : NonZeroUsize ,
455
+ prot : ProtFlags ,
456
+ flags : MapFlags ,
457
+ ) -> Result < * mut c_void > {
458
+ let ptr = addr. map_or ( std:: ptr:: null_mut ( ) , |a| a. get ( ) as * mut c_void ) ;
459
+
460
+ let flags = MapFlags :: MAP_ANONYMOUS | flags;
461
+ let ret = libc:: mmap ( ptr, length. into ( ) , prot. bits ( ) , flags. bits ( ) , -1 , 0 ) ;
462
+
463
+ if ret == libc:: MAP_FAILED {
464
+ Err ( Errno :: last ( ) )
465
+ } else {
466
+ Ok ( ret)
467
+ }
468
+ }
469
+
443
470
/// Expands (or shrinks) an existing memory mapping, potentially moving it at
444
471
/// the same time.
445
472
///
0 commit comments