xref: /qemu/rust/qemu-api/src/lib.rs (revision 4aed0296b307b6e2e3b7d38ee6c5204cf2dfe1ca)
1 // Copyright 2024, Linaro Limited
2 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 #![cfg_attr(not(MESON), doc = include_str!("../README.md"))]
6 
7 #[rustfmt::skip]
8 pub mod bindings;
9 
10 // preserve one-item-per-"use" syntax, it is clearer
11 // for prelude-like modules
12 #[rustfmt::skip]
13 pub mod prelude;
14 
15 pub mod bitops;
16 pub mod c_str;
17 pub mod cell;
18 pub mod irq;
19 pub mod module;
20 pub mod offset_of;
21 pub mod qdev;
22 pub mod qom;
23 pub mod sysbus;
24 pub mod vmstate;
25 pub mod zeroable;
26 
27 use std::{
28     alloc::{GlobalAlloc, Layout},
29     os::raw::c_void,
30 };
31 
32 #[cfg(HAVE_GLIB_WITH_ALIGNED_ALLOC)]
33 extern "C" {
34     fn g_aligned_alloc0(
35         n_blocks: bindings::gsize,
36         n_block_bytes: bindings::gsize,
37         alignment: bindings::gsize,
38     ) -> bindings::gpointer;
39     fn g_aligned_free(mem: bindings::gpointer);
40 }
41 
42 #[cfg(not(HAVE_GLIB_WITH_ALIGNED_ALLOC))]
43 extern "C" {
44     fn qemu_memalign(alignment: usize, size: usize) -> *mut c_void;
45     fn qemu_vfree(ptr: *mut c_void);
46 }
47 
48 extern "C" {
49     fn g_malloc0(n_bytes: bindings::gsize) -> bindings::gpointer;
50     fn g_free(mem: bindings::gpointer);
51 }
52 
53 /// An allocator that uses the same allocator as QEMU in C.
54 ///
55 /// It is enabled by default with the `allocator` feature.
56 ///
57 /// To set it up manually as a global allocator in your crate:
58 ///
59 /// ```ignore
60 /// use qemu_api::QemuAllocator;
61 ///
62 /// #[global_allocator]
63 /// static GLOBAL: QemuAllocator = QemuAllocator::new();
64 /// ```
65 #[derive(Clone, Copy, Debug)]
66 #[repr(C)]
67 pub struct QemuAllocator {
68     _unused: [u8; 0],
69 }
70 
71 #[cfg_attr(all(feature = "allocator", not(test)), global_allocator)]
72 pub static GLOBAL: QemuAllocator = QemuAllocator::new();
73 
74 impl QemuAllocator {
75     // From the glibc documentation, on GNU systems, malloc guarantees 16-byte
76     // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
77     // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html.
78     // This alignment guarantee also applies to Windows and Android. On Darwin
79     // and OpenBSD, the alignment is 16 bytes on both 64-bit and 32-bit systems.
80     #[cfg(all(
81         target_pointer_width = "32",
82         not(any(target_os = "macos", target_os = "openbsd"))
83     ))]
84     pub const DEFAULT_ALIGNMENT_BYTES: Option<usize> = Some(8);
85     #[cfg(all(
86         target_pointer_width = "64",
87         not(any(target_os = "macos", target_os = "openbsd"))
88     ))]
89     pub const DEFAULT_ALIGNMENT_BYTES: Option<usize> = Some(16);
90     #[cfg(all(
91         any(target_pointer_width = "32", target_pointer_width = "64"),
92         any(target_os = "macos", target_os = "openbsd")
93     ))]
94     pub const DEFAULT_ALIGNMENT_BYTES: Option<usize> = Some(16);
95     #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
96     pub const DEFAULT_ALIGNMENT_BYTES: Option<usize> = None;
97 
98     pub const fn new() -> Self {
99         Self { _unused: [] }
100     }
101 }
102 
103 impl Default for QemuAllocator {
104     fn default() -> Self {
105         Self::new()
106     }
107 }
108 
109 // Sanity check.
110 const _: [(); 8] = [(); ::core::mem::size_of::<*mut c_void>()];
111 
112 unsafe impl GlobalAlloc for QemuAllocator {
113     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
114         if matches!(Self::DEFAULT_ALIGNMENT_BYTES, Some(default) if default.checked_rem(layout.align()) == Some(0))
115         {
116             // SAFETY: g_malloc0() is safe to call.
117             unsafe { g_malloc0(layout.size().try_into().unwrap()).cast::<u8>() }
118         } else {
119             #[cfg(HAVE_GLIB_WITH_ALIGNED_ALLOC)]
120             {
121                 // SAFETY: g_aligned_alloc0() is safe to call.
122                 unsafe {
123                     g_aligned_alloc0(
124                         layout.size().try_into().unwrap(),
125                         1,
126                         layout.align().try_into().unwrap(),
127                     )
128                     .cast::<u8>()
129                 }
130             }
131             #[cfg(not(HAVE_GLIB_WITH_ALIGNED_ALLOC))]
132             {
133                 // SAFETY: qemu_memalign() is safe to call.
134                 unsafe { qemu_memalign(layout.align(), layout.size()).cast::<u8>() }
135             }
136         }
137     }
138 
139     unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
140         if matches!(Self::DEFAULT_ALIGNMENT_BYTES, Some(default) if default.checked_rem(layout.align()) == Some(0))
141         {
142             // SAFETY: `ptr` must have been allocated by Self::alloc thus a valid
143             // glib-allocated pointer, so `g_free`ing is safe.
144             unsafe { g_free(ptr.cast::<_>()) }
145         } else {
146             #[cfg(HAVE_GLIB_WITH_ALIGNED_ALLOC)]
147             {
148                 // SAFETY: `ptr` must have been allocated by Self::alloc thus a valid aligned
149                 // glib-allocated pointer, so `g_aligned_free`ing is safe.
150                 unsafe { g_aligned_free(ptr.cast::<_>()) }
151             }
152             #[cfg(not(HAVE_GLIB_WITH_ALIGNED_ALLOC))]
153             {
154                 // SAFETY: `ptr` must have been allocated by Self::alloc thus a valid aligned
155                 // glib-allocated pointer, so `qemu_vfree`ing is safe.
156                 unsafe { qemu_vfree(ptr.cast::<_>()) }
157             }
158         }
159     }
160 }
161 
162 #[cfg(has_offset_of)]
163 pub use core::mem::offset_of;
164