[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240924205837.78618dab.gary@garyguo.net>
Date: Tue, 24 Sep 2024 20:58:37 +0100
From: Gary Guo <gary@...yguo.net>
To: Danilo Krummrich <dakr@...nel.org>
Cc: Alice Ryhl <aliceryhl@...gle.com>, ojeda@...nel.org,
alex.gaynor@...il.com, wedsonaf@...il.com, boqun.feng@...il.com,
bjorn3_gh@...tonmail.com, benno.lossin@...ton.me, a.hindborg@...sung.com,
akpm@...ux-foundation.org, daniel.almeida@...labora.com,
faith.ekstrand@...labora.com, boris.brezillon@...labora.com,
lina@...hilina.net, mcanal@...lia.com, zhiw@...dia.com, cjia@...dia.com,
jhubbard@...dia.com, airlied@...hat.com, ajanulgu@...hat.com,
lyude@...hat.com, linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org, linux-mm@...ck.org
Subject: Re: [RFC PATCH] rust: alloc: pass `old_layout` to `Allocator`
On Tue, 24 Sep 2024 15:31:47 +0200
Danilo Krummrich <dakr@...nel.org> wrote:
> On Mon, Sep 23, 2024 at 05:13:15PM +0100, Gary Guo wrote:
> > On Mon, 23 Sep 2024 15:56:28 +0200
> > Alice Ryhl <aliceryhl@...gle.com> wrote:
> >
> > > On Sat, Sep 21, 2024 at 5:33 PM Danilo Krummrich <dakr@...nel.org> wrote:
> > > > @@ -84,11 +92,18 @@ unsafe fn call(
> > > > &self,
> > > > ptr: Option<NonNull<u8>>,
> > > > layout: Layout,
> > > > + old_layout: Layout,
> > > > flags: Flags,
> > > > ) -> Result<NonNull<[u8]>, AllocError> {
> > > > let size = aligned_size(layout);
> > > > let ptr = match ptr {
> > > > - Some(ptr) => ptr.as_ptr(),
> > > > + Some(ptr) => {
> > > > + if old_layout.size() == 0 {
> > > > + ptr::null()
> > > > + } else {
> > > > + ptr.as_ptr()
> > > > + }
> > > > + }
> > >
> > > This is making Allocator work with zero-sized types, which deviates
> > > from std. We should not do that without a reason. What is the reason?
> > >
> > > Alice
> >
> > As Benno said, this makes the API closer to Rust `allocator_api`
> > Allocator trait as opposed to deviation.
> >
> > There's one benefit of doing this (discussed with Danilo off-list),
> > which is it removes ZST special casing from caller. This RFC patch
> > simplifies `Box` handling, and if we add this line to the safety doc
> >
> > `ptr` does not need to be a pointer returned by this
> > allocator if the layout is zero-sized.
> >
> > then the `Vec` can also be simplified, removing all logics handling ZST
> > specially, except for `Vec::new()` which it forges a well-aligned
> > dangling pointer from nowhere.
>
> Partially, we still need the additional `Layout` for `Allocator::free`, which
> in `Vec::drop` and `IntoIter::drop` looks like this:
>
> `let layout = Layout::array::<T>(self.cap).unwrap();`
>
You can add an invariant to `Vec` that the size in bytes does not
exceed `isize::MAX` (which is already true, just not documented as
invariant), and this can be changed to `unwrap_unchecked`.
> I really dislike that this can potentially transform into `BUG()`, but that's
> probably unrelated to this patch series.
Powered by blists - more mailing lists