1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 2 3 //! Extensions to the [`alloc`] crate. 3 //! Extensions to the [`alloc`] crate. 4 4 5 #[cfg(not(test))] 5 #[cfg(not(test))] 6 #[cfg(not(testlib))] 6 #[cfg(not(testlib))] 7 mod allocator; 7 mod allocator; 8 pub mod box_ext; 8 pub mod box_ext; 9 pub mod vec_ext; 9 pub mod vec_ext; 10 10 11 /// Indicates an allocation error. 11 /// Indicates an allocation error. 12 #[derive(Copy, Clone, PartialEq, Eq, Debug)] 12 #[derive(Copy, Clone, PartialEq, Eq, Debug)] 13 pub struct AllocError; 13 pub struct AllocError; 14 14 15 /// Flags to be used when allocating memory. 15 /// Flags to be used when allocating memory. 16 /// 16 /// 17 /// They can be combined with the operators `| 17 /// They can be combined with the operators `|`, `&`, and `!`. 18 /// 18 /// 19 /// Values can be used from the [`flags`] modu 19 /// Values can be used from the [`flags`] module. 20 #[derive(Clone, Copy)] 20 #[derive(Clone, Copy)] 21 pub struct Flags(u32); 21 pub struct Flags(u32); 22 22 23 impl Flags { << 24 /// Get the raw representation of this fla << 25 pub(crate) fn as_raw(self) -> u32 { << 26 self.0 << 27 } << 28 } << 29 << 30 impl core::ops::BitOr for Flags { 23 impl core::ops::BitOr for Flags { 31 type Output = Self; 24 type Output = Self; 32 fn bitor(self, rhs: Self) -> Self::Output 25 fn bitor(self, rhs: Self) -> Self::Output { 33 Self(self.0 | rhs.0) 26 Self(self.0 | rhs.0) 34 } 27 } 35 } 28 } 36 29 37 impl core::ops::BitAnd for Flags { 30 impl core::ops::BitAnd for Flags { 38 type Output = Self; 31 type Output = Self; 39 fn bitand(self, rhs: Self) -> Self::Output 32 fn bitand(self, rhs: Self) -> Self::Output { 40 Self(self.0 & rhs.0) 33 Self(self.0 & rhs.0) 41 } 34 } 42 } 35 } 43 36 44 impl core::ops::Not for Flags { 37 impl core::ops::Not for Flags { 45 type Output = Self; 38 type Output = Self; 46 fn not(self) -> Self::Output { 39 fn not(self) -> Self::Output { 47 Self(!self.0) 40 Self(!self.0) 48 } 41 } 49 } 42 } 50 43 51 /// Allocation flags. 44 /// Allocation flags. 52 /// 45 /// 53 /// These are meant to be used in functions th 46 /// These are meant to be used in functions that can allocate memory. 54 pub mod flags { 47 pub mod flags { 55 use super::Flags; 48 use super::Flags; 56 49 57 /// Zeroes out the allocated memory. 50 /// Zeroes out the allocated memory. 58 /// 51 /// 59 /// This is normally or'd with other flags 52 /// This is normally or'd with other flags. 60 pub const __GFP_ZERO: Flags = Flags(bindin 53 pub const __GFP_ZERO: Flags = Flags(bindings::__GFP_ZERO); 61 54 62 /// Allow the allocation to be in high mem << 63 /// << 64 /// Allocations in high memory may not be << 65 /// be used with `kmalloc` and other simil << 66 /// << 67 /// This is normally or'd with other flags << 68 pub const __GFP_HIGHMEM: Flags = Flags(bin << 69 << 70 /// Users can not sleep and need the alloc 55 /// Users can not sleep and need the allocation to succeed. 71 /// 56 /// 72 /// A lower watermark is applied to allow 57 /// A lower watermark is applied to allow access to "atomic reserves". The current 73 /// implementation doesn't support NMI and 58 /// implementation doesn't support NMI and few other strict non-preemptive contexts (e.g. 74 /// raw_spin_lock). The same applies to [` 59 /// raw_spin_lock). The same applies to [`GFP_NOWAIT`]. 75 pub const GFP_ATOMIC: Flags = Flags(bindin 60 pub const GFP_ATOMIC: Flags = Flags(bindings::GFP_ATOMIC); 76 61 77 /// Typical for kernel-internal allocation 62 /// Typical for kernel-internal allocations. The caller requires ZONE_NORMAL or a lower zone 78 /// for direct access but can direct recla 63 /// for direct access but can direct reclaim. 79 pub const GFP_KERNEL: Flags = Flags(bindin 64 pub const GFP_KERNEL: Flags = Flags(bindings::GFP_KERNEL); 80 65 81 /// The same as [`GFP_KERNEL`], except the 66 /// The same as [`GFP_KERNEL`], except the allocation is accounted to kmemcg. 82 pub const GFP_KERNEL_ACCOUNT: Flags = Flag 67 pub const GFP_KERNEL_ACCOUNT: Flags = Flags(bindings::GFP_KERNEL_ACCOUNT); 83 68 84 /// For kernel allocations that should not !! 69 /// Ror kernel allocations that should not stall for direct reclaim, start physical IO or 85 /// use any filesystem callback. It is ve 70 /// use any filesystem callback. It is very likely to fail to allocate memory, even for very 86 /// small allocations. 71 /// small allocations. 87 pub const GFP_NOWAIT: Flags = Flags(bindin 72 pub const GFP_NOWAIT: Flags = Flags(bindings::GFP_NOWAIT); 88 } 73 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.