Skip to content

Commit 6485539

Browse files
Rick Richardsoncarllerche
Rick Richardson
authored andcommitted
added msync, madvise, and requisite constants for macos and linux
1 parent f8e3f22 commit 6485539

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/sys/mman.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ mod consts {
3939
pub const PROT_GROWSDOWN: MmapProt = 0x01000000;
4040
pub const PROT_GROWSUP: MmapProt = 0x02000000;
4141

42+
pub type MmapAdvise = c_int;
43+
44+
pub const MADV_NORMAL : MmapAdvise = 0; /* No further special treatment. */
45+
pub const MADV_RANDOM : MmapAdvise = 1; /* Expect random page references. */
46+
pub const MADV_SEQUENTIAL : MmapAdvise = 2; /* Expect sequential page references. */
47+
pub const MADV_WILLNEED : MmapAdvise = 3; /* Will need these pages. */
48+
pub const MADV_DONTNEED : MmapAdvise = 4; /* Don't need these pages. */
49+
pub const MADV_REMOVE : MmapAdvise = 9; /* Remove these pages and resources. */
50+
pub const MADV_DONTFORK : MmapAdvise = 10; /* Do not inherit across fork. */
51+
pub const MADV_DOFORK : MmapAdvise = 11; /* Do inherit across fork. */
52+
pub const MADV_MERGEABLE : MmapAdvise = 12; /* KSM may merge identical pages. */
53+
pub const MADV_UNMERGEABLE: MmapAdvise = 13; /* KSM may not merge identical pages. */
54+
pub const MADV_HUGEPAGE : MmapAdvise = 14; /* Worth backing with hugepages. */
55+
pub const MADV_NOHUGEPAGE : MmapAdvise = 15; /* Not worth backing with hugepages. */
56+
pub const MADV_DONTDUMP : MmapAdvise = 16; /* Explicity exclude from the core dump, overrides the coredump filter bits. */
57+
pub const MADV_DODUMP : MmapAdvise = 17; /* Clear the MADV_DONTDUMP flag. */
58+
pub const MADV_HWPOISON : MmapAdvise = 100; /* Poison a page for testing. */
59+
60+
pub type MmapSync = c_int;
61+
62+
pub const MS_ASYNC : MmapSync = 1;
63+
pub const MS_SYNC : MmapSync = 4;
64+
pub const MS_INVALIDATE : MmapSync = 2;
65+
4266
pub const MAP_FAILED: int = -1;
4367
}
4468

@@ -61,6 +85,27 @@ mod consts {
6185
pub const PROT_WRITE: MmapProt = 0x2;
6286
pub const PROT_EXEC: MmapProt = 0x4;
6387
pub const PROT_NONE: MmapProt = 0x0;
88+
89+
pub type MmapAdvise = c_int;
90+
91+
pub const MADV_NORMAL : MmapAdvise = 0; /* No further special treatment. */
92+
pub const MADV_RANDOM : MmapAdvise = 1; /* Expect random page references. */
93+
pub const MADV_SEQUENTIAL : MmapAdvise = 2; /* Expect sequential page references. */
94+
pub const MADV_WILLNEED : MmapAdvise = 3; /* Will need these pages. */
95+
pub const MADV_DONTNEED : MmapAdvise = 4; /* Don't need these pages. */
96+
pub const MADV_FREE : MmapAdvise = 5; /* pages unneeded, discard contents */
97+
pub const MADV_ZERO_WIRED_PAGES: MmapAdvise = 6; /* zero the wired pages that have not been unwired before the entry is deleted */
98+
pub const MADV_FREE_REUSABLE : MmapAdvise = 7; /* pages can be reused (by anyone) */
99+
pub const MADV_FREE_REUSE : MmapAdvise = 8; /* caller wants to reuse those pages */
100+
pub const MADV_CAN_REUSE : MmapAdvise = 9;
101+
102+
pub type MmapSync = c_int;
103+
104+
pub const MS_ASYNC : MmapSync = 0x0001; /* [MF|SIO] return immediately */
105+
pub const MS_INVALIDATE : MmapSync = 0x0002; /* [MF|SIO] invalidate all cached data */
106+
pub const MS_SYNC : MmapSync = 0x0010; /* [MF|SIO] msync synchronously */
107+
pub const MS_KILLPAGES : MmapSync = 0x0004; /* invalidate pages, leave mapped */
108+
pub const MS_DEACTIVATE : MmapSync = 0x0008; /* deactivate pages, leave mapped */
64109

65110
pub const MAP_FAILED: int = -1;
66111
}
@@ -76,6 +121,8 @@ mod ffi {
76121
pub fn shm_unlink(name: *const c_char) -> c_int;
77122
pub fn mlock(addr: *const c_void, len: size_t) -> c_int;
78123
pub fn munlock(addr: *const c_void, len: size_t) -> c_int;
124+
pub fn madvise (addr: *const c_void, len: size_t, advice: c_int) -> c_int;
125+
pub fn msync (addr: *const c_void, len: size_t, flags: c_int) -> c_int;
79126
}
80127
}
81128

@@ -112,6 +159,20 @@ pub fn munmap(addr: *mut c_void, len: size_t) -> SysResult<()> {
112159
}
113160
}
114161

162+
pub fn madvise(addr: *const c_void, length: size_t, advise: MmapAdvise) -> SysResult<()> {
163+
match unsafe { ffi::madvise(addr, length, advise) } {
164+
0 => Ok(()),
165+
_ => Err(SysError::last())
166+
}
167+
}
168+
169+
pub fn msync(addr: *const c_void, length: size_t, flags: MmapSync) -> SysResult<()> {
170+
match unsafe { ffi::msync(addr, length, flags) } {
171+
0 => Ok(()),
172+
_ => Err(SysError::last())
173+
}
174+
}
175+
115176
pub fn shm_open(name: &String, flag: OFlag, mode: FilePermission) -> SysResult<Fd> {
116177
let ret = unsafe { ffi::shm_open(name.to_c_str().as_ptr(), flag.bits(), mode.bits() as mode_t) };
117178

0 commit comments

Comments
 (0)