From: Boqun Feng <boqun.feng@gmail.com>
To: Wedson Almeida Filho <wedsonaf@gmail.com>
Cc: rust-for-linux@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>,
linux-kernel@vger.kernel.org,
"Wedson Almeida Filho" <walmeida@microsoft.com>
Subject: Re: [PATCH 07/10] rust: alloc: update `VecExt` to take allocation flags
Date: Mon, 25 Mar 2024 13:44:23 -0700 [thread overview]
Message-ID: <ZgHiJ23TdOmnSGe9@boqun-archlinux> (raw)
In-Reply-To: <20240325195418.166013-8-wedsonaf@gmail.com>
On Mon, Mar 25, 2024 at 04:54:15PM -0300, Wedson Almeida Filho wrote:
[...]
> + fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {
> + <Self as VecExt<_>>::reserve(self, 1, flags)?;
> + let (ptr, len, cap) = destructure(self);
> + // SAFETY: ptr is valid for `cap` elements. And `cap` is greater (by at least 1) than
> + // `len` because of the call to `reserve` above. So the pointer after offsetting by `len`
> + // elements is valid for write.
> + unsafe { ptr.wrapping_add(len).write(v) };
> +
> + // SAFETY: The only difference from the values returned by `destructure` is that `length`
> + // is incremented by 1, which is fine because we have just initialised the element at
> + // offset `length`.
> + unsafe { rebuild(self, ptr, len + 1, cap) };
probably use spare_capacity_mut() here to avoid `destructure` and
`rebuild`?
https://doc.rust-lang.org/std/vec/struct.Vec.html#method.spare_capacity_mut
// .. after reserve succeed.
// there must be room for adding one more.
self.spare_capacity_mut()[0].write(v);
// or unsafe { self.spare_capacity_mut().as_mut_ptr().cast().write(v); }
unsafe {
self.set_len(self.len() + 1);
}
Thoughts?
Regards,
Boqun
> Ok(())
> }
>
[...]
next prev parent reply other threads:[~2024-03-25 20:44 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-25 19:54 [PATCH 00/10] Allocation APIs Wedson Almeida Filho
2024-03-25 19:54 ` [PATCH 01/10] rust: kernel: move `allocator` module under `alloc` Wedson Almeida Filho
2024-03-25 21:56 ` Benno Lossin
2024-03-26 0:04 ` Wedson Almeida Filho
2024-03-25 19:54 ` [PATCH 02/10] rust: alloc: introduce the `VecExt` trait Wedson Almeida Filho
2024-03-25 22:05 ` Benno Lossin
2024-03-26 0:02 ` Wedson Almeida Filho
2024-03-25 19:54 ` [PATCH 03/10] kbuild: use the upstream `alloc` crate Wedson Almeida Filho
2024-03-25 19:54 ` [PATCH 04/10] rust: alloc: remove our fork of the " Wedson Almeida Filho
2024-03-25 22:24 ` Benno Lossin
2024-03-25 19:54 ` [PATCH 05/10] rust: alloc: introduce allocation flags Wedson Almeida Filho
2024-03-25 22:26 ` Benno Lossin
2024-03-25 19:54 ` [PATCH 06/10] rust: alloc: introduce the `BoxExt` trait Wedson Almeida Filho
2024-03-25 22:37 ` Benno Lossin
2024-03-26 0:17 ` Wedson Almeida Filho
2024-03-26 13:30 ` Benno Lossin
2024-03-27 1:54 ` Wedson Almeida Filho
2024-03-27 1:55 ` Wedson Almeida Filho
2024-03-25 19:54 ` [PATCH 07/10] rust: alloc: update `VecExt` to take allocation flags Wedson Almeida Filho
2024-03-25 20:44 ` Boqun Feng [this message]
2024-03-26 0:03 ` Wedson Almeida Filho
2024-03-26 13:58 ` Benno Lossin
2024-03-25 19:54 ` [PATCH 08/10] rust: sync: update `Arc` and `UniqueArc` " Wedson Almeida Filho
2024-03-26 14:01 ` Benno Lossin
2024-03-25 19:54 ` [PATCH 09/10] rust: init: update `init` module " Wedson Almeida Filho
2024-03-26 14:08 ` Benno Lossin
2024-03-27 2:17 ` Wedson Almeida Filho
2024-03-25 19:54 ` [PATCH 10/10] rust: kernel: remove usage of `allocator_api` unstable feature Wedson Almeida Filho
2024-03-26 15:27 ` Benno Lossin
2024-03-27 2:13 ` Wedson Almeida Filho
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZgHiJ23TdOmnSGe9@boqun-archlinux \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=walmeida@microsoft.com \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).