From: Junjie Mao <junjie.mao@intel.com>
To: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>,
<qemu-devel@nongnu.org>
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Gustavo Romero" <gustavo.romero@linaro.org>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Zhao Liu" <zhao1.liu@intel.com>
Subject: Re: [PATCH v8 6/8] rust: add crate to expose bindings and interfaces
Date: Mon, 26 Aug 2024 13:31:55 +0800 [thread overview]
Message-ID: <841befb6-5ce1-44e5-890c-4e60fcbd4fa6@intel.com> (raw)
In-Reply-To: <20240823-rust-pl011-v8-6-b9f5746bdaf3@linaro.org>
On 8/23/2024 4:11 PM, Manos Pitsidianakis wrote:
> Add rust/qemu-api, which exposes rust-bindgen generated FFI bindings and
> provides some declaration macros for symbols visible to the rest of
> QEMU.
>
> Co-authored-by: Junjie Mao <junjie.mao@intel.com>
> Co-authored-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Junjie Mao <junjie.mao@intel.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> ---
> MAINTAINERS | 6 ++
> rust/meson.build | 1 +
> rust/qemu-api/.gitignore | 2 +
> rust/qemu-api/Cargo.lock | 7 +++
> rust/qemu-api/Cargo.toml | 26 ++++++++
> rust/qemu-api/README.md | 17 +++++
> rust/qemu-api/build.rs | 14 +++++
> rust/qemu-api/meson.build | 20 ++++++
> rust/qemu-api/rustfmt.toml | 1 +
> rust/qemu-api/src/definitions.rs | 109 ++++++++++++++++++++++++++++++++
> rust/qemu-api/src/device_class.rs | 128 ++++++++++++++++++++++++++++++++++++++
> rust/qemu-api/src/lib.rs | 102 ++++++++++++++++++++++++++++++
> rust/qemu-api/src/tests.rs | 49 +++++++++++++++
> rust/rustfmt.toml | 7 +++
> 14 files changed, 489 insertions(+)
[snip]
> diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs
> new file mode 100644
> index 0000000000..ab95d0d5f7
> --- /dev/null
> +++ b/rust/qemu-api/src/lib.rs
> @@ -0,0 +1,102 @@
> +// Copyright 2024, Linaro Limited
> +// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +#![cfg_attr(not(MESON), doc = include_str!("../README.md"))]
> +
> +#[allow(
> + dead_code,
> + improper_ctypes_definitions,
> + improper_ctypes,
> + non_camel_case_types,
> + non_snake_case,
> + non_upper_case_globals,
> + clippy::missing_const_for_fn,
> + clippy::too_many_arguments,
> + clippy::approx_constant,
> + clippy::use_self,
> + clippy::useless_transmute,
> + clippy::missing_safety_doc,
> +)]
> +#[rustfmt::skip]
> +pub mod bindings;
> +
> +unsafe impl Send for bindings::Property {}
> +unsafe impl Sync for bindings::Property {}
> +unsafe impl Sync for bindings::TypeInfo {}
> +unsafe impl Sync for bindings::VMStateDescription {}
> +
> +pub mod definitions;
> +pub mod device_class;
> +
> +#[cfg(test)]
> +mod tests;
> +
> +use std::alloc::{GlobalAlloc, Layout};
> +
> +extern "C" {
> + pub fn g_aligned_alloc0(
> + n_blocks: bindings::gsize,
> + n_block_bytes: bindings::gsize,
> + alignment: bindings::gsize,
> + ) -> bindings::gpointer;
> + pub fn g_aligned_free(mem: bindings::gpointer);
> + pub fn g_malloc0(n_bytes: bindings::gsize) -> bindings::gpointer;
> + pub fn g_free(mem: bindings::gpointer);
> +}
> +
> +/// An allocator that uses the same allocator as QEMU in C.
> +///
> +/// It is enabled by default with the `allocator` feature.
> +///
> +/// To set it up manually as a global allocator in your crate:
> +///
> +/// ```ignore
> +/// use qemu_api::QemuAllocator;
> +///
> +/// #[global_allocator]
> +/// static GLOBAL: QemuAllocator = QemuAllocator::new();
> +/// ```
> +#[derive(Clone, Copy, Debug)]
> +#[repr(C)]
> +pub struct QemuAllocator {
> + _unused: [u8; 0],
> +}
> +
> +#[cfg_attr(feature = "allocator", global_allocator)]
> +pub static GLOBAL: QemuAllocator = QemuAllocator::new();
> +
> +impl QemuAllocator {
> + pub const fn new() -> Self {
> + Self { _unused: [] }
> + }
> +}
> +
> +impl Default for QemuAllocator {
> + fn default() -> Self {
> + Self::new()
> + }
> +}
> +
> +unsafe impl GlobalAlloc for QemuAllocator {
> + unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
> + if layout.align() == 0 {
> + g_malloc0(layout.size().try_into().unwrap()).cast::<u8>()
> + } else {
> + g_aligned_alloc0(
One more thing: g_aligned_alloc0() was introduced in glib 2.72 [1] but the
current glib version check in meson is >= 2.66.0.
Glib 2.72 still supports Win 7+, so no increase to _WIN32_WINNT is needed for
this version bumping.
[1] https://docs.gtk.org/glib/func.aligned_alloc0.html
[2] https://gitlab.gnome.org/GNOME/glib/-/blob/2.72.0/meson.build?ref_type=tags#L509
---
Best Regards
Junjie Mao
> + layout.size().try_into().unwrap(),
> + 1,
> + layout.align().try_into().unwrap(),
> + )
> + .cast::<u8>()
> + }
> + }
> +
> + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
> + if layout.align() == 0 {
> + g_free(ptr.cast::<_>())
> + } else {
> + g_aligned_free(ptr.cast::<_>())
> + }
> + }
> +}
next prev parent reply other threads:[~2024-08-26 5:33 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-23 8:11 [PATCH v8 0/8] Add Rust build support, ARM PL011 device impl Manos Pitsidianakis
2024-08-23 8:11 ` [PATCH v8 1/8] Require meson version 1.5.0 Manos Pitsidianakis
2024-08-23 8:11 ` [PATCH v8 2/8] build-sys: Add rust feature option Manos Pitsidianakis
2024-08-23 8:11 ` [PATCH v8 3/8] configure, meson: detect Rust toolchain Manos Pitsidianakis
2024-08-23 8:11 ` [PATCH v8 4/8] rust: add bindgen step as a meson dependency Manos Pitsidianakis
2024-08-23 8:11 ` [PATCH v8 5/8] .gitattributes: add Rust diff and merge attributes Manos Pitsidianakis
2024-08-23 8:11 ` [PATCH v8 6/8] rust: add crate to expose bindings and interfaces Manos Pitsidianakis
2024-08-26 5:03 ` Junjie Mao
2024-08-26 6:12 ` Manos Pitsidianakis
2024-08-26 8:41 ` Junjie Mao
2024-08-26 5:31 ` Junjie Mao [this message]
2024-08-26 6:41 ` Manos Pitsidianakis
2024-08-26 7:45 ` Junjie Mao
2024-08-26 8:24 ` Thomas Huth
2024-08-26 11:29 ` Manos Pitsidianakis
2024-08-23 8:11 ` [PATCH v8 7/8] rust: add utility procedural macro crate Manos Pitsidianakis
2024-08-26 5:15 ` Junjie Mao
2024-08-26 6:02 ` Manos Pitsidianakis
2024-08-23 8:11 ` [PATCH v8 8/8] rust: add PL011 device model Manos Pitsidianakis
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=841befb6-5ce1-44e5-890c-4e60fcbd4fa6@intel.com \
--to=junjie.mao@intel.com \
--cc=alex.bennee@linaro.org \
--cc=berrange@redhat.com \
--cc=gustavo.romero@linaro.org \
--cc=manos.pitsidianakis@linaro.org \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=pierrick.bouvier@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=thuth@redhat.com \
--cc=zhao1.liu@intel.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).