Paolo Bonzini | cde3c42 | 2024-10-18 16:30:56 +0200 | [diff] [blame] | 1 | // Copyright 2024, Linaro Limited |
| 2 | // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org> |
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | |
Paolo Bonzini | 9f7d452 | 2024-10-24 13:53:59 +0200 | [diff] [blame^] | 5 | use std::{ffi::CStr, os::raw::c_void}; |
Paolo Bonzini | cde3c42 | 2024-10-18 16:30:56 +0200 | [diff] [blame] | 6 | |
| 7 | use qemu_api::{ |
| 8 | bindings::*, |
| 9 | declare_properties, define_property, |
| 10 | definitions::{Class, ObjectImpl}, |
Manos Pitsidianakis | 0a65e41 | 2024-10-25 07:55:50 +0200 | [diff] [blame] | 11 | device_class_init, |
| 12 | zeroable::Zeroable, |
Paolo Bonzini | cde3c42 | 2024-10-18 16:30:56 +0200 | [diff] [blame] | 13 | }; |
| 14 | |
| 15 | #[test] |
| 16 | fn test_device_decl_macros() { |
| 17 | // Test that macros can compile. |
Manos Pitsidianakis | 0a65e41 | 2024-10-25 07:55:50 +0200 | [diff] [blame] | 18 | pub static VMSTATE: VMStateDescription = VMStateDescription { |
| 19 | name: c"name".as_ptr(), |
Paolo Bonzini | cde3c42 | 2024-10-18 16:30:56 +0200 | [diff] [blame] | 20 | unmigratable: true, |
Manos Pitsidianakis | 0a65e41 | 2024-10-25 07:55:50 +0200 | [diff] [blame] | 21 | ..Zeroable::ZERO |
| 22 | }; |
Paolo Bonzini | cde3c42 | 2024-10-18 16:30:56 +0200 | [diff] [blame] | 23 | |
| 24 | #[repr(C)] |
| 25 | #[derive(qemu_api_macros::Object)] |
| 26 | pub struct DummyState { |
| 27 | pub _parent: DeviceState, |
| 28 | pub migrate_clock: bool, |
| 29 | } |
| 30 | |
| 31 | #[repr(C)] |
| 32 | pub struct DummyClass { |
| 33 | pub _parent: DeviceClass, |
| 34 | } |
| 35 | |
| 36 | declare_properties! { |
| 37 | DUMMY_PROPERTIES, |
| 38 | define_property!( |
| 39 | c"migrate-clk", |
| 40 | DummyState, |
| 41 | migrate_clock, |
| 42 | unsafe { &qdev_prop_bool }, |
| 43 | bool |
| 44 | ), |
| 45 | } |
| 46 | |
| 47 | device_class_init! { |
| 48 | dummy_class_init, |
| 49 | props => DUMMY_PROPERTIES, |
| 50 | realize_fn => None, |
| 51 | legacy_reset_fn => None, |
| 52 | vmsd => VMSTATE, |
| 53 | } |
| 54 | |
| 55 | impl ObjectImpl for DummyState { |
| 56 | type Class = DummyClass; |
| 57 | const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self }; |
| 58 | const TYPE_NAME: &'static CStr = c"dummy"; |
| 59 | const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_DEVICE); |
| 60 | const ABSTRACT: bool = false; |
| 61 | const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None; |
| 62 | const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None; |
| 63 | const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None; |
| 64 | } |
| 65 | |
| 66 | impl Class for DummyClass { |
Paolo Bonzini | 9f7d452 | 2024-10-24 13:53:59 +0200 | [diff] [blame^] | 67 | const CLASS_INIT: Option<unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void)> = |
| 68 | Some(dummy_class_init); |
Paolo Bonzini | cde3c42 | 2024-10-18 16:30:56 +0200 | [diff] [blame] | 69 | const CLASS_BASE_INIT: Option< |
Paolo Bonzini | 9f7d452 | 2024-10-24 13:53:59 +0200 | [diff] [blame^] | 70 | unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void), |
Paolo Bonzini | cde3c42 | 2024-10-18 16:30:56 +0200 | [diff] [blame] | 71 | > = None; |
| 72 | } |
| 73 | |
| 74 | unsafe { |
| 75 | module_call_init(module_init_type::MODULE_INIT_QOM); |
| 76 | object_unref(object_new(DummyState::TYPE_NAME.as_ptr()) as *mut _); |
| 77 | } |
| 78 | } |