Skip to content

Commit

Permalink
rust: alloc: implement contains for Flags
Browse files Browse the repository at this point in the history
Provide a simple helper function to check whether given flags do
contain one or multiple other flags.

This is used by a subsequent patch implementing the Cmalloc `Allocator`
to check for __GFP_ZERO.

Reviewed-by: Alice Ryhl <[email protected]>
Reviewed-by: Benno Lossin <[email protected]>
Reviewed-by: Gary Guo <[email protected]>
Signed-off-by: Danilo Krummrich <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
  • Loading branch information
Danilo Krummrich authored and ojeda committed Oct 15, 2024
1 parent a4a41a6 commit 184eb6a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion rust/kernel/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ use core::{alloc::Layout, ptr::NonNull};
/// They can be combined with the operators `|`, `&`, and `!`.
///
/// Values can be used from the [`flags`] module.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq)]
pub struct Flags(u32);

impl Flags {
/// Get the raw representation of this flag.
pub(crate) fn as_raw(self) -> u32 {
self.0
}

/// Check whether `flags` is contained in `self`.
pub fn contains(self, flags: Flags) -> bool {
(self & flags) == flags
}
}

impl core::ops::BitOr for Flags {
Expand Down

0 comments on commit 184eb6a

Please sign in to comment.