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