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