Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rbtree v4 #1081

Draft
wants to merge 7 commits into
base: rust-next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rust/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func,
}
EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key);

void rust_helper_rb_link_node(struct rb_node *node, struct rb_node *parent,
struct rb_node **rb_link)
{
rb_link_node(node, parent, rb_link);
}
EXPORT_SYMBOL_GPL(rust_helper_rb_link_node);

/*
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
* use it in contexts where Rust expects a `usize` like slice (array) indices.
Expand Down
24 changes: 23 additions & 1 deletion rust/kernel/alloc/box_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use super::{AllocError, Flags};
use alloc::boxed::Box;
use core::mem::MaybeUninit;
use core::{mem::MaybeUninit, ptr};

/// Extensions to [`Box`].
pub trait BoxExt<T>: Sized {
Expand All @@ -17,6 +17,20 @@ pub trait BoxExt<T>: Sized {
///
/// The allocation may fail, in which case an error is returned.
fn new_uninit(flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError>;

/// Drops the contents, but keeps the allocation.
///
/// # Examples
///
/// ```
/// use kernel::alloc::flags;
///
/// let value = Box::new([0; 32], flags::GFP_KERNEL);
/// let value = value.unwrap().drop_contents();
matthewtgilbride marked this conversation as resolved.
Show resolved Hide resolved
/// // Now we can re-use `value`:
/// Box::write(value, [1; 32]);
/// ```
fn drop_contents(self) -> Box<MaybeUninit<T>>;
}

impl<T> BoxExt<T> for Box<T> {
Expand Down Expand Up @@ -53,4 +67,12 @@ impl<T> BoxExt<T> for Box<T> {
// zero-sized types, we use `NonNull::dangling`.
Ok(unsafe { Box::from_raw(ptr) })
}

fn drop_contents(self) -> Box<MaybeUninit<T>> {
let ptr = Box::into_raw(self);
// SAFETY: `ptr` is valid, because it came from `Box::into_raw`.
unsafe { ptr::drop_in_place(ptr) };
// SAFETY: `ptr` is valid, because it came from `Box::into_raw`.
unsafe { Box::from_raw(ptr.cast()) }
}
}
1 change: 1 addition & 0 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod kunit;
pub mod net;
pub mod prelude;
pub mod print;
pub mod rbtree;
mod static_assert;
#[doc(hidden)]
pub mod std_vendor;
Expand Down
Loading