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