rustix/backend/libc/mm/
types.rs

1use crate::backend::c;
2use bitflags::bitflags;
3
4bitflags! {
5    /// `PROT_*` flags for use with [`mmap`].
6    ///
7    /// For `PROT_NONE`, use `ProtFlags::empty()`.
8    ///
9    /// [`mmap`]: crate::mm::mmap
10    #[repr(transparent)]
11    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
12    pub struct ProtFlags: u32 {
13        /// `PROT_READ`
14        const READ = bitcast!(c::PROT_READ);
15        /// `PROT_WRITE`
16        const WRITE = bitcast!(c::PROT_WRITE);
17        /// `PROT_EXEC`
18        const EXEC = bitcast!(c::PROT_EXEC);
19
20        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
21        const _ = !0;
22    }
23}
24
25bitflags! {
26    /// `PROT_*` flags for use with [`mprotect`].
27    ///
28    /// For `PROT_NONE`, use `MprotectFlags::empty()`.
29    ///
30    /// [`mprotect`]: crate::mm::mprotect
31    #[repr(transparent)]
32    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
33    pub struct MprotectFlags: u32 {
34        /// `PROT_READ`
35        const READ = bitcast!(c::PROT_READ);
36        /// `PROT_WRITE`
37        const WRITE = bitcast!(c::PROT_WRITE);
38        /// `PROT_EXEC`
39        const EXEC = bitcast!(c::PROT_EXEC);
40        /// `PROT_GROWSUP`
41        #[cfg(linux_kernel)]
42        const GROWSUP = bitcast!(c::PROT_GROWSUP);
43        /// `PROT_GROWSDOWN`
44        #[cfg(linux_kernel)]
45        const GROWSDOWN = bitcast!(c::PROT_GROWSDOWN);
46        /// `PROT_SEM`
47        #[cfg(linux_kernel)]
48        const SEM = linux_raw_sys::general::PROT_SEM;
49        /// `PROT_BTI`
50        #[cfg(all(linux_kernel, target_arch = "aarch64"))]
51        const BTI = linux_raw_sys::general::PROT_BTI;
52        /// `PROT_MTE`
53        #[cfg(all(linux_kernel, target_arch = "aarch64"))]
54        const MTE = linux_raw_sys::general::PROT_MTE;
55        /// `PROT_SAO`
56        #[cfg(all(linux_kernel, any(target_arch = "powerpc", target_arch = "powerpc64")))]
57        const SAO = linux_raw_sys::general::PROT_SAO;
58        /// `PROT_ADI`
59        #[cfg(all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64")))]
60        const ADI = linux_raw_sys::general::PROT_ADI;
61
62        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
63        const _ = !0;
64    }
65}
66
67bitflags! {
68    /// `MAP_*` flags for use with [`mmap`].
69    ///
70    /// For `MAP_ANONYMOUS` (aka `MAP_ANON`), see [`mmap_anonymous`].
71    ///
72    /// [`mmap`]: crate::mm::mmap
73    /// [`mmap_anonymous`]: crates::mm::mmap_anonymous
74    #[repr(transparent)]
75    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
76    pub struct MapFlags: u32 {
77        /// `MAP_SHARED`
78        const SHARED = bitcast!(c::MAP_SHARED);
79        /// `MAP_SHARED_VALIDATE`
80        #[cfg(not(any(
81            bsd,
82            solarish,
83            target_os = "aix",
84            target_os = "android",
85            target_os = "cygwin",
86            target_os = "emscripten",
87            target_os = "fuchsia",
88            target_os = "haiku",
89            target_os = "hurd",
90            target_os = "nto",
91            target_os = "redox",
92        )))]
93        const SHARED_VALIDATE = bitcast!(c::MAP_SHARED_VALIDATE);
94        /// `MAP_PRIVATE`
95        const PRIVATE = bitcast!(c::MAP_PRIVATE);
96        /// `MAP_DENYWRITE`
97        #[cfg(not(any(
98            bsd,
99            solarish,
100            target_os = "aix",
101            target_os = "cygwin",
102            target_os = "haiku",
103            target_os = "hurd",
104            target_os = "nto",
105            target_os = "redox",
106        )))]
107        const DENYWRITE = bitcast!(c::MAP_DENYWRITE);
108        /// `MAP_FIXED`
109        const FIXED = bitcast!(c::MAP_FIXED);
110        /// `MAP_FIXED_NOREPLACE`
111        #[cfg(not(any(
112            bsd,
113            solarish,
114            target_os = "aix",
115            target_os = "android",
116            target_os = "cygwin",
117            target_os = "emscripten",
118            target_os = "fuchsia",
119            target_os = "haiku",
120            target_os = "hurd",
121            target_os = "nto",
122            target_os = "redox",
123        )))]
124        const FIXED_NOREPLACE = bitcast!(c::MAP_FIXED_NOREPLACE);
125        /// `MAP_GROWSDOWN`
126        #[cfg(not(any(
127            bsd,
128            solarish,
129            target_os = "aix",
130            target_os = "cygwin",
131            target_os = "haiku",
132            target_os = "hurd",
133            target_os = "nto",
134            target_os = "redox",
135        )))]
136        const GROWSDOWN = bitcast!(c::MAP_GROWSDOWN);
137        /// `MAP_HUGETLB`
138        #[cfg(not(any(
139            bsd,
140            solarish,
141            target_os = "aix",
142            target_os = "cygwin",
143            target_os = "haiku",
144            target_os = "hurd",
145            target_os = "nto",
146            target_os = "redox",
147        )))]
148        const HUGETLB = bitcast!(c::MAP_HUGETLB);
149        /// `MAP_HUGE_2MB`
150        #[cfg(not(any(
151            bsd,
152            solarish,
153            target_os = "aix",
154            target_os = "android",
155            target_os = "cygwin",
156            target_os = "emscripten",
157            target_os = "fuchsia",
158            target_os = "haiku",
159            target_os = "hurd",
160            target_os = "nto",
161            target_os = "redox",
162        )))]
163        const HUGE_2MB = bitcast!(c::MAP_HUGE_2MB);
164        /// `MAP_HUGE_1GB`
165        #[cfg(not(any(
166            bsd,
167            solarish,
168            target_os = "aix",
169            target_os = "android",
170            target_os = "cygwin",
171            target_os = "emscripten",
172            target_os = "fuchsia",
173            target_os = "haiku",
174            target_os = "hurd",
175            target_os = "nto",
176            target_os = "redox",
177        )))]
178        const HUGE_1GB = bitcast!(c::MAP_HUGE_1GB);
179        /// `MAP_LOCKED`
180        #[cfg(not(any(
181            bsd,
182            solarish,
183            target_os = "aix",
184            target_os = "cygwin",
185            target_os = "haiku",
186            target_os = "hurd",
187            target_os = "nto",
188            target_os = "redox",
189        )))]
190        const LOCKED = bitcast!(c::MAP_LOCKED);
191        /// `MAP_NOCORE`
192        #[cfg(freebsdlike)]
193        const NOCORE = bitcast!(c::MAP_NOCORE);
194        /// `MAP_NORESERVE`
195        #[cfg(not(any(
196            freebsdlike,
197            target_os = "aix",
198            target_os = "hurd",
199            target_os = "nto",
200            target_os = "redox",
201        )))]
202        const NORESERVE = bitcast!(c::MAP_NORESERVE);
203        /// `MAP_NOSYNC`
204        #[cfg(freebsdlike)]
205        const NOSYNC = bitcast!(c::MAP_NOSYNC);
206        /// `MAP_POPULATE`
207        #[cfg(not(any(
208            bsd,
209            solarish,
210            target_os = "aix",
211            target_os = "cygwin",
212            target_os = "haiku",
213            target_os = "hurd",
214            target_os = "nto",
215            target_os = "redox",
216        )))]
217        const POPULATE = bitcast!(c::MAP_POPULATE);
218        /// `MAP_STACK`
219        #[cfg(not(any(
220            apple,
221            solarish,
222            target_os = "aix",
223            target_os = "cygwin",
224            target_os = "haiku",
225            target_os = "hurd",
226            target_os = "redox",
227        )))]
228        const STACK = bitcast!(c::MAP_STACK);
229        /// `MAP_PREFAULT_READ`
230        #[cfg(target_os = "freebsd")]
231        const PREFAULT_READ = bitcast!(c::MAP_PREFAULT_READ);
232        /// `MAP_SYNC`
233        #[cfg(not(any(
234            bsd,
235            solarish,
236            target_os = "aix",
237            target_os = "android",
238            target_os = "cygwin",
239            target_os = "emscripten",
240            target_os = "fuchsia",
241            target_os = "haiku",
242            target_os = "hurd",
243            target_os = "nto",
244            target_os = "redox",
245            all(
246                linux_kernel,
247                any(target_arch = "mips", target_arch = "mips32r6", target_arch = "mips64", target_arch = "mips64r6"),
248            ),
249        )))]
250        const SYNC = bitcast!(c::MAP_SYNC);
251        /// `MAP_UNINITIALIZED`
252        #[cfg(any())]
253        const UNINITIALIZED = bitcast!(c::MAP_UNINITIALIZED);
254        /// `MAP_DROPPABLE`
255        #[cfg(linux_kernel)]
256        const DROPPABLE = bitcast!(c::MAP_DROPPABLE);
257
258        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
259        const _ = !0;
260    }
261}
262
263#[cfg(any(target_os = "emscripten", target_os = "linux"))]
264bitflags! {
265    /// `MREMAP_*` flags for use with [`mremap`].
266    ///
267    /// For `MREMAP_FIXED`, see [`mremap_fixed`].
268    ///
269    /// [`mremap`]: crate::mm::mremap
270    /// [`mremap_fixed`]: crate::mm::mremap_fixed
271    #[repr(transparent)]
272    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
273    pub struct MremapFlags: u32 {
274        /// `MREMAP_MAYMOVE`
275        const MAYMOVE = bitcast!(c::MREMAP_MAYMOVE);
276
277        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
278        const _ = !0;
279    }
280}
281
282bitflags! {
283    /// `MS_*` flags for use with [`msync`].
284    ///
285    /// [`msync`]: crate::mm::msync
286    #[repr(transparent)]
287    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
288    pub struct MsyncFlags: u32 {
289        /// `MS_SYNC`—Requests an update and waits for it to complete.
290        const SYNC = bitcast!(c::MS_SYNC);
291        /// `MS_ASYNC`—Specifies that an update be scheduled, but the call
292        /// returns immediately.
293        const ASYNC = bitcast!(c::MS_ASYNC);
294        /// `MS_INVALIDATE`—Asks to invalidate other mappings of the same
295        /// file (so that they can be updated with the fresh values just
296        /// written).
297        const INVALIDATE = bitcast!(c::MS_INVALIDATE);
298
299        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
300        const _ = !0;
301    }
302}
303
304#[cfg(linux_kernel)]
305bitflags! {
306    /// `MLOCK_*` flags for use with [`mlock_with`].
307    ///
308    /// [`mlock_with`]: crate::mm::mlock_with
309    #[repr(transparent)]
310    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
311    pub struct MlockFlags: u32 {
312        /// `MLOCK_ONFAULT`
313        const ONFAULT = bitcast!(c::MLOCK_ONFAULT);
314
315        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
316        const _ = !0;
317    }
318}
319
320/// `POSIX_MADV_*` constants for use with [`madvise`].
321///
322/// [`madvise`]: crate::mm::madvise
323#[cfg(not(target_os = "redox"))]
324#[derive(Debug, Copy, Clone, Eq, PartialEq)]
325#[repr(u32)]
326#[non_exhaustive]
327pub enum Advice {
328    /// `POSIX_MADV_NORMAL`
329    #[cfg(not(any(target_os = "android", target_os = "haiku")))]
330    Normal = bitcast!(c::POSIX_MADV_NORMAL),
331
332    /// `POSIX_MADV_NORMAL`
333    #[cfg(any(target_os = "android", target_os = "haiku"))]
334    Normal = bitcast!(c::MADV_NORMAL),
335
336    /// `POSIX_MADV_SEQUENTIAL`
337    #[cfg(not(any(target_os = "android", target_os = "haiku")))]
338    Sequential = bitcast!(c::POSIX_MADV_SEQUENTIAL),
339
340    /// `POSIX_MADV_SEQUENTIAL`
341    #[cfg(any(target_os = "android", target_os = "haiku"))]
342    Sequential = bitcast!(c::MADV_SEQUENTIAL),
343
344    /// `POSIX_MADV_RANDOM`
345    #[cfg(not(any(target_os = "android", target_os = "haiku")))]
346    Random = bitcast!(c::POSIX_MADV_RANDOM),
347
348    /// `POSIX_MADV_RANDOM`
349    #[cfg(any(target_os = "android", target_os = "haiku"))]
350    Random = bitcast!(c::MADV_RANDOM),
351
352    /// `POSIX_MADV_WILLNEED`
353    #[cfg(not(any(target_os = "android", target_os = "haiku")))]
354    WillNeed = bitcast!(c::POSIX_MADV_WILLNEED),
355
356    /// `POSIX_MADV_WILLNEED`
357    #[cfg(any(target_os = "android", target_os = "haiku"))]
358    WillNeed = bitcast!(c::MADV_WILLNEED),
359
360    /// `POSIX_MADV_DONTNEED`
361    #[cfg(not(any(
362        target_os = "android",
363        target_os = "emscripten",
364        target_os = "haiku",
365        target_os = "hurd",
366    )))]
367    DontNeed = bitcast!(c::POSIX_MADV_DONTNEED),
368
369    /// `POSIX_MADV_DONTNEED`
370    #[cfg(any(target_os = "android", target_os = "haiku"))]
371    DontNeed = bitcast!(i32::MAX - 1),
372
373    /// `MADV_DONTNEED`
374    // `MADV_DONTNEED` has the same value as `POSIX_MADV_DONTNEED`. We don't
375    // have a separate `posix_madvise` from `madvise`, so we expose a special
376    // value which we special-case.
377    #[cfg(target_os = "linux")]
378    LinuxDontNeed = bitcast!(i32::MAX),
379
380    /// `MADV_DONTNEED`
381    #[cfg(target_os = "android")]
382    LinuxDontNeed = bitcast!(c::MADV_DONTNEED),
383    /// `MADV_FREE`
384    #[cfg(linux_kernel)]
385    LinuxFree = bitcast!(c::MADV_FREE),
386    /// `MADV_REMOVE`
387    #[cfg(linux_kernel)]
388    LinuxRemove = bitcast!(c::MADV_REMOVE),
389    /// `MADV_DONTFORK`
390    #[cfg(linux_kernel)]
391    LinuxDontFork = bitcast!(c::MADV_DONTFORK),
392    /// `MADV_DOFORK`
393    #[cfg(linux_kernel)]
394    LinuxDoFork = bitcast!(c::MADV_DOFORK),
395    /// `MADV_HWPOISON`
396    #[cfg(linux_kernel)]
397    LinuxHwPoison = bitcast!(c::MADV_HWPOISON),
398    /// `MADV_SOFT_OFFLINE`
399    #[cfg(all(
400        linux_kernel,
401        not(any(
402            target_arch = "mips",
403            target_arch = "mips32r6",
404            target_arch = "mips64",
405            target_arch = "mips64r6"
406        ))
407    ))]
408    LinuxSoftOffline = bitcast!(c::MADV_SOFT_OFFLINE),
409    /// `MADV_MERGEABLE`
410    #[cfg(linux_kernel)]
411    LinuxMergeable = bitcast!(c::MADV_MERGEABLE),
412    /// `MADV_UNMERGEABLE`
413    #[cfg(linux_kernel)]
414    LinuxUnmergeable = bitcast!(c::MADV_UNMERGEABLE),
415    /// `MADV_HUGEPAGE`
416    #[cfg(linux_kernel)]
417    LinuxHugepage = bitcast!(c::MADV_HUGEPAGE),
418    /// `MADV_NOHUGEPAGE`
419    #[cfg(linux_kernel)]
420    LinuxNoHugepage = bitcast!(c::MADV_NOHUGEPAGE),
421    /// `MADV_DONTDUMP` (since Linux 3.4)
422    #[cfg(linux_kernel)]
423    LinuxDontDump = bitcast!(c::MADV_DONTDUMP),
424    /// `MADV_DODUMP` (since Linux 3.4)
425    #[cfg(linux_kernel)]
426    LinuxDoDump = bitcast!(c::MADV_DODUMP),
427    /// `MADV_WIPEONFORK` (since Linux 4.14)
428    #[cfg(linux_kernel)]
429    LinuxWipeOnFork = bitcast!(c::MADV_WIPEONFORK),
430    /// `MADV_KEEPONFORK` (since Linux 4.14)
431    #[cfg(linux_kernel)]
432    LinuxKeepOnFork = bitcast!(c::MADV_KEEPONFORK),
433    /// `MADV_COLD` (since Linux 5.4)
434    #[cfg(linux_kernel)]
435    LinuxCold = bitcast!(c::MADV_COLD),
436    /// `MADV_PAGEOUT` (since Linux 5.4)
437    #[cfg(linux_kernel)]
438    LinuxPageOut = bitcast!(c::MADV_PAGEOUT),
439    /// `MADV_POPULATE_READ` (since Linux 5.14)
440    #[cfg(linux_kernel)]
441    LinuxPopulateRead = bitcast!(c::MADV_POPULATE_READ),
442    /// `MADV_POPULATE_WRITE` (since Linux 5.14)
443    #[cfg(linux_kernel)]
444    LinuxPopulateWrite = bitcast!(c::MADV_POPULATE_WRITE),
445    /// `MADV_DONTNEED_LOCKED` (since Linux 5.18)
446    #[cfg(linux_kernel)]
447    LinuxDontneedLocked = bitcast!(c::MADV_DONTNEED_LOCKED),
448}
449
450#[cfg(target_os = "emscripten")]
451#[allow(non_upper_case_globals)]
452impl Advice {
453    /// `POSIX_MADV_DONTNEED`
454    pub const DontNeed: Self = Self::Normal;
455}
456
457#[cfg(linux_kernel)]
458bitflags! {
459    /// `O_*` flags for use with [`userfaultfd`].
460    ///
461    /// [`userfaultfd`]: crate::mm::userfaultfd
462    #[repr(transparent)]
463    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
464    pub struct UserfaultfdFlags: u32 {
465        /// `O_CLOEXEC`
466        const CLOEXEC = bitcast!(c::O_CLOEXEC);
467        /// `O_NONBLOCK`
468        const NONBLOCK = bitcast!(c::O_NONBLOCK);
469
470        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
471        const _ = !0;
472    }
473}
474
475#[cfg(any(linux_kernel, freebsdlike, netbsdlike))]
476bitflags! {
477    /// `MCL_*` flags for use with [`mlockall`].
478    ///
479    /// [`mlockall`]: crate::mm::mlockall
480    #[repr(transparent)]
481    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
482    pub struct MlockAllFlags: u32 {
483        /// Used together with `MCL_CURRENT`, `MCL_FUTURE`, or both. Mark all
484        /// current (with `MCL_CURRENT`) or future (with `MCL_FUTURE`) mappings
485        /// to lock pages when they are faulted in. When used with
486        /// `MCL_CURRENT`, all present pages are locked, but `mlockall` will
487        /// not fault in non-present pages. When used with `MCL_FUTURE`, all
488        /// future mappings will be marked to lock pages when they are faulted
489        /// in, but they will not be populated by the lock when the mapping is
490        /// created. `MCL_ONFAULT` must be used with either `MCL_CURRENT` or
491        /// `MCL_FUTURE` or both.
492        #[cfg(linux_kernel)]
493        const ONFAULT = bitcast!(c::MCL_ONFAULT);
494        /// Lock all pages which will become mapped into the address space of
495        /// the process in the future. These could be, for instance, new pages
496        /// required by a growing heap and stack as well as new memory-mapped
497        /// files or shared memory regions.
498        const FUTURE = bitcast!(c::MCL_FUTURE);
499        /// Lock all pages which are currently mapped into the address space of
500        /// the process.
501        const CURRENT = bitcast!(c::MCL_CURRENT);
502
503        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
504        const _ = !0;
505    }
506}