1 // SPDX-License-Identifier: GPL-2.0
3 //! Extensions to the [`alloc`] crate.
11 /// Indicates an allocation error.
12 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
13 pub struct AllocError;
15 /// Flags to be used when allocating memory.
17 /// They can be combined with the operators `|`, `&`, and `!`.
19 /// Values can be used from the [`flags`] module.
20 #[derive(Clone, Copy)]
21 pub struct Flags(u32);
24 /// Get the raw representation of this flag.
25 pub(crate) fn as_raw(self) -> u32 {
30 impl core::ops::BitOr for Flags {
32 fn bitor(self, rhs: Self) -> Self::Output {
37 impl core::ops::BitAnd for Flags {
39 fn bitand(self, rhs: Self) -> Self::Output {
44 impl core::ops::Not for Flags {
46 fn not(self) -> Self::Output {
53 /// These are meant to be used in functions that can allocate memory.
57 /// Zeroes out the allocated memory.
59 /// This is normally or'd with other flags.
60 pub const __GFP_ZERO: Flags = Flags(bindings::__GFP_ZERO);
62 /// Allow the allocation to be in high memory.
64 /// Allocations in high memory may not be mapped into the kernel's address space, so this can't
65 /// be used with `kmalloc` and other similar methods.
67 /// This is normally or'd with other flags.
68 pub const __GFP_HIGHMEM: Flags = Flags(bindings::__GFP_HIGHMEM);
70 /// Users can not sleep and need the allocation to succeed.
72 /// A lower watermark is applied to allow access to "atomic reserves". The current
73 /// implementation doesn't support NMI and few other strict non-preemptive contexts (e.g.
74 /// raw_spin_lock). The same applies to [`GFP_NOWAIT`].
75 pub const GFP_ATOMIC: Flags = Flags(bindings::GFP_ATOMIC);
77 /// Typical for kernel-internal allocations. The caller requires ZONE_NORMAL or a lower zone
78 /// for direct access but can direct reclaim.
79 pub const GFP_KERNEL: Flags = Flags(bindings::GFP_KERNEL);
81 /// The same as [`GFP_KERNEL`], except the allocation is accounted to kmemcg.
82 pub const GFP_KERNEL_ACCOUNT: Flags = Flags(bindings::GFP_KERNEL_ACCOUNT);
84 /// For kernel allocations that should not stall for direct reclaim, start physical IO or
85 /// use any filesystem callback. It is very likely to fail to allocate memory, even for very
86 /// small allocations.
87 pub const GFP_NOWAIT: Flags = Flags(bindings::GFP_NOWAIT);