[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAH5fLggBVNYbP1tWSFH+cmkAtXv=2Nzm1fWyF+1-NWgHBDwq6w@mail.gmail.com>
Date: Mon, 8 Apr 2024 09:59:37 +0200
From: Alice Ryhl <aliceryhl@...gle.com>
To: Benno Lossin <benno.lossin@...ton.me>
Cc: Miguel Ojeda <ojeda@...nel.org>, Andrew Morton <akpm@...ux-foundation.org>,
Alex Gaynor <alex.gaynor@...il.com>, Wedson Almeida Filho <wedsonaf@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Andreas Hindborg <a.hindborg@...sung.com>, Marco Elver <elver@...gle.com>,
Kees Cook <keescook@...omium.org>, Coly Li <colyli@...e.de>, Paolo Abeni <pabeni@...hat.com>,
Pierre Gondois <pierre.gondois@....com>, Ingo Molnar <mingo@...nel.org>,
Jakub Kicinski <kuba@...nel.org>, Wei Yang <richard.weiyang@...il.com>,
Matthew Wilcox <willy@...radead.org>, linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org
Subject: Re: [PATCH 8/9] rust: list: support heterogeneous lists
On Thu, Apr 4, 2024 at 5:35 PM Benno Lossin <benno.lossin@...ton.me> wrote:
>
> On 02.04.24 14:17, Alice Ryhl wrote:
> > @@ -180,6 +184,41 @@ unsafe fn from_fields(me: *mut ListLinksFields) -> *mut Self {
> > }
> > }
> >
> > +/// Similar to [`ListLinks`], but also contains a pointer to the full value.
> > +///
> > +/// This type can be used instead of [`ListLinks`] to support lists with trait objects.
> > +#[repr(C)]
> > +pub struct ListLinksSelfPtr<T: ?Sized, const ID: u64 = 0> {
> > + /// The `ListLinks` field inside this value.
> > + ///
> > + /// This is public so that it can be used with `impl_has_list_links!`.
> > + pub inner: ListLinks<ID>,
> > + self_ptr: UnsafeCell<MaybeUninit<*const T>>,
> > +}
> > +
> > +unsafe impl<T: ?Sized + Send, const ID: u64> Send for ListLinksSelfPtr<T, ID> {}
> > +unsafe impl<T: ?Sized + Sync, const ID: u64> Sync for ListLinksSelfPtr<T, ID> {}
>
> Missing SAFETY comments.
Will do.
> > +
> > +impl<T: ?Sized, const ID: u64> ListLinksSelfPtr<T, ID> {
> > + /// The offset from the [`ListLinks`] to the self pointer field.
> > + pub const LIST_LINKS_SELF_PTR_OFFSET: usize = core::mem::offset_of!(Self, self_ptr);
> > +
> > + /// Creates a new initializer for this type.
> > + pub fn new() -> impl PinInit<Self> {
> > + // INVARIANT: Pin-init initializers can't be used on an existing `Arc`, so this value will
> > + // not be constructed in an `Arc` that already has a `ListArc`.
> > + Self {
> > + inner: ListLinks {
> > + inner: Opaque::new(ListLinksFields {
> > + prev: ptr::null_mut(),
> > + next: ptr::null_mut(),
> > + }),
> > + },
>
> Why don't you use `inner <- ListLinks::new(),`?
Because I wasn't using the macro at all. I was just using the fact
that T implements PinInit<T>. But as discussed on another patch, I'll
replace this entire method with init::zeroed().
> > + self_ptr: UnsafeCell::new(MaybeUninit::zeroed()),
> > + }
> > + }
> > +}
> > +
> > impl<T: ?Sized + ListItem<ID>, const ID: u64> List<T, ID> {
> > /// Creates a new empty list.
> > pub const fn new() -> Self {
>
> [...]
>
> > @@ -94,5 +137,45 @@ unsafe fn post_remove(me: *mut ListLinks<$num>) -> *const Self {
> > }
> > }
> > };
> > +
> > + (
> > + impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty {
> > + using ListLinksSelfPtr;
> > + } $($rest:tt)*
> > + ) => {
> > + unsafe impl$(<$($generics)*>)? ListItem<$num> for $t {
> > + unsafe fn prepare_to_insert(me: *const Self) -> *mut ListLinks<$num> {
> > + let links_field = unsafe { Self::view_links(me) };
> > +
> > + let spoff = ListLinksSelfPtr::<Self, $num>::LIST_LINKS_SELF_PTR_OFFSET;
> > + let self_ptr = unsafe { (links_field as *const u8).add(spoff)
> > + as *const ::core::cell::UnsafeCell<*const Self> };
> > + let cell_inner = ::core::cell::UnsafeCell::raw_get(self_ptr);
> > +
> > + unsafe { ::core::ptr::write(cell_inner, me) };
> > + links_field
> > + }
> > +
> > + unsafe fn view_links(me: *const Self) -> *mut ListLinks<$num> {
> > + unsafe {
> > + <Self as HasListLinks<$num>>::raw_get_list_links(me.cast_mut())
> > + }
> > + }
> > +
> > + unsafe fn view_value(links_field: *mut ListLinks<$num>) -> *const Self {
> > + let spoff = ListLinksSelfPtr::<Self, $num>::LIST_LINKS_SELF_PTR_OFFSET;
> > + let self_ptr = unsafe { (links_field as *const u8).add(spoff)
> > + as *const ::core::cell::UnsafeCell<*const Self> };
> > + let cell_inner = ::core::cell::UnsafeCell::raw_get(self_ptr);
> > + unsafe {
> > + ::core::ptr::read(cell_inner)
> > + }
> > + }
> > +
> > + unsafe fn post_remove(me: *mut ListLinks<$num>) -> *const Self {
> > + unsafe { Self::view_value(me) }
> > + }
> > + }
> > + };
>
> The paths in this macro should use `$crate::...` to prevent import
> errors.
Will do.
Alice
Powered by blists - more mailing lists