Skip to content

Commit 838180f

Browse files
authored
Re-export mmap libc constants through host_env (#8214)
* Re-export mmap libc constants through host_env Move direct libc MADV_*, MAP_*, PROT_*, EOVERFLOW references in stdlib::mmap to host_env::mmap re-exports. Replace libc::c_int with core::ffi::c_int. * host_env/mmap: gate EOVERFLOW re-export on cfg(windows) Its only consumer is the cfg(windows) named-mapping overflow check in stdlib::mmap; the re-export was gated cfg(unix), so it was configured out on Windows and the reference failed to resolve. Assisted-by: Claude
1 parent ac67865 commit 838180f

2 files changed

Lines changed: 78 additions & 17 deletions

File tree

‎crates/host_env/src/mmap.rs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,63 @@
55

66
use std::io;
77

8+
#[cfg(unix)]
9+
pub use libc::{
10+
MADV_DONTNEED, MADV_NORMAL, MADV_RANDOM, MADV_SEQUENTIAL, MADV_WILLNEED, MAP_ANON,
11+
MAP_ANONYMOUS, MAP_PRIVATE, MAP_SHARED, PROT_EXEC, PROT_READ, PROT_WRITE,
12+
};
13+
14+
#[cfg(target_os = "macos")]
15+
pub use libc::{MADV_FREE_REUSABLE, MADV_FREE_REUSE};
16+
17+
#[cfg(any(
18+
target_os = "android",
19+
target_os = "dragonfly",
20+
target_os = "fuchsia",
21+
target_os = "freebsd",
22+
target_os = "linux",
23+
target_os = "netbsd",
24+
target_os = "openbsd",
25+
target_vendor = "apple"
26+
))]
27+
pub use libc::MADV_FREE;
28+
29+
#[cfg(target_os = "linux")]
30+
pub use libc::{
31+
MADV_DODUMP, MADV_DOFORK, MADV_DONTDUMP, MADV_DONTFORK, MADV_HUGEPAGE, MADV_HWPOISON,
32+
MADV_MERGEABLE, MADV_NOHUGEPAGE, MADV_REMOVE, MADV_UNMERGEABLE,
33+
};
34+
35+
#[cfg(any(
36+
target_os = "android",
37+
all(
38+
target_os = "linux",
39+
any(
40+
target_arch = "aarch64",
41+
target_arch = "arm",
42+
target_arch = "powerpc",
43+
target_arch = "powerpc64",
44+
target_arch = "s390x",
45+
target_arch = "x86",
46+
target_arch = "x86_64",
47+
target_arch = "sparc64"
48+
)
49+
)
50+
))]
51+
pub use libc::MADV_SOFT_OFFLINE;
52+
53+
#[cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))]
54+
pub use libc::{MAP_DENYWRITE, MAP_EXECUTABLE, MAP_POPULATE};
55+
56+
#[cfg(any(target_os = "linux", target_os = "openbsd", target_os = "netbsd"))]
57+
pub use libc::MAP_STACK;
58+
59+
#[cfg(target_os = "freebsd")]
60+
pub use libc::{MADV_AUTOSYNC, MADV_CORE, MADV_NOCORE, MADV_NOSYNC, MADV_PROTECT};
61+
62+
#[cfg(windows)]
63+
pub use libc::EOVERFLOW;
64+
865
#[cfg(windows)]
966
use crate::windows::{CheckWin32Bool, HandleToOwned};
1067
#[cfg(unix)]

‎crates/stdlib/src/mmap.rs‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ mod mmap {
6060

6161
#[cfg(unix)]
6262
#[pyattr]
63-
use libc::{
63+
use host_mmap::{
6464
MADV_DONTNEED, MADV_NORMAL, MADV_RANDOM, MADV_SEQUENTIAL, MADV_WILLNEED, MAP_ANON,
6565
MAP_ANONYMOUS, MAP_PRIVATE, MAP_SHARED, PROT_EXEC, PROT_READ, PROT_WRITE,
6666
};
6767

6868
#[cfg(target_os = "macos")]
6969
#[pyattr]
70-
use libc::{MADV_FREE_REUSABLE, MADV_FREE_REUSE};
70+
use host_mmap::{MADV_FREE_REUSABLE, MADV_FREE_REUSE};
7171

7272
#[cfg(any(
7373
target_os = "android",
@@ -80,11 +80,11 @@ mod mmap {
8080
target_vendor = "apple"
8181
))]
8282
#[pyattr]
83-
use libc::MADV_FREE;
83+
use host_mmap::MADV_FREE;
8484

8585
#[cfg(target_os = "linux")]
8686
#[pyattr]
87-
use libc::{
87+
use host_mmap::{
8888
MADV_DODUMP, MADV_DOFORK, MADV_DONTDUMP, MADV_DONTFORK, MADV_HUGEPAGE, MADV_HWPOISON,
8989
MADV_MERGEABLE, MADV_NOHUGEPAGE, MADV_REMOVE, MADV_UNMERGEABLE,
9090
};
@@ -106,21 +106,21 @@ mod mmap {
106106
)
107107
))]
108108
#[pyattr]
109-
use libc::MADV_SOFT_OFFLINE;
109+
use host_mmap::MADV_SOFT_OFFLINE;
110110

111111
#[cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))]
112112
#[pyattr]
113-
use libc::{MAP_DENYWRITE, MAP_EXECUTABLE, MAP_POPULATE};
113+
use host_mmap::{MAP_DENYWRITE, MAP_EXECUTABLE, MAP_POPULATE};
114114

115115
// MAP_STACK is available on Linux, OpenBSD, and NetBSD
116116
#[cfg(any(target_os = "linux", target_os = "openbsd", target_os = "netbsd"))]
117117
#[pyattr]
118-
use libc::MAP_STACK;
118+
use host_mmap::MAP_STACK;
119119

120120
// FreeBSD-specific MADV constants
121121
#[cfg(target_os = "freebsd")]
122122
#[pyattr]
123-
use libc::{MADV_AUTOSYNC, MADV_CORE, MADV_NOCORE, MADV_NOSYNC, MADV_PROTECT};
123+
use host_mmap::{MADV_AUTOSYNC, MADV_CORE, MADV_NOCORE, MADV_NOSYNC, MADV_PROTECT};
124124

125125
#[pyattr]
126126
const ACCESS_DEFAULT: u32 = AccessMode::Default as u32;
@@ -212,10 +212,10 @@ mod mmap {
212212
fileno: i32,
213213
#[pyarg(any)]
214214
length: isize,
215-
#[pyarg(any, default = libc::MAP_SHARED)]
216-
flags: libc::c_int,
217-
#[pyarg(any, default = libc::PROT_WRITE | libc::PROT_READ)]
218-
prot: libc::c_int,
215+
#[pyarg(any, default = host_mmap::MAP_SHARED)]
216+
flags: core::ffi::c_int,
217+
#[pyarg(any, default = host_mmap::PROT_WRITE | host_mmap::PROT_READ)]
218+
prot: core::ffi::c_int,
219219
#[pyarg(any, default = AccessMode::Default)]
220220
access: AccessMode,
221221
#[pyarg(any, default = 0)]
@@ -294,7 +294,7 @@ mod mmap {
294294
#[derive(FromArgs)]
295295
pub(super) struct AdviseOptions {
296296
#[pyarg(positional)]
297-
option: libc::c_int,
297+
option: core::ffi::c_int,
298298
#[pyarg(positional, default)]
299299
start: Option<PyIntRef>,
300300
#[pyarg(positional, default)]
@@ -303,7 +303,11 @@ mod mmap {
303303

304304
#[cfg(all(unix, not(target_os = "redox")))]
305305
impl AdviseOptions {
306-
fn values(self, len: usize, vm: &VirtualMachine) -> PyResult<(libc::c_int, usize, usize)> {
306+
fn values(
307+
self,
308+
len: usize,
309+
vm: &VirtualMachine,
310+
) -> PyResult<(core::ffi::c_int, usize, usize)> {
307311
let start = self
308312
.start
309313
.map(|s| {
@@ -342,7 +346,7 @@ mod mmap {
342346

343347
#[cfg(unix)]
344348
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
345-
use libc::{MAP_PRIVATE, MAP_SHARED, PROT_READ, PROT_WRITE};
349+
use host_mmap::{MAP_PRIVATE, MAP_SHARED, PROT_READ, PROT_WRITE};
346350

347351
let mut map_size = args.validate_new_args(vm)?;
348352
let MmapNewArgs {
@@ -552,7 +556,7 @@ mod mmap {
552556
map_size,
553557
)
554558
.map_err(|err| {
555-
if err.raw_os_error() == Some(libc::EOVERFLOW) {
559+
if err.raw_os_error() == Some(host_mmap::EOVERFLOW) {
556560
vm.new_overflow_error("mmap offset plus size would overflow")
557561
} else {
558562
err.to_pyexception(vm)
@@ -1074,7 +1078,7 @@ mod mmap {
10741078
fn seek(
10751079
&self,
10761080
dist: isize,
1077-
whence: OptionalArg<libc::c_int>,
1081+
whence: OptionalArg<core::ffi::c_int>,
10781082
vm: &VirtualMachine,
10791083
) -> PyResult<()> {
10801084
let how = whence.unwrap_or(0);

0 commit comments

Comments
 (0)