Skip to contentSkip to conversation

qom: Introduce class_post_init() handler

PATCH In review Philippe Mathieu-Daudé 4 participants 7 messages Jan 25, 2025 – Jan 27, 2025 Rust

lore
Share & tools
lore
#0 lore raw

cover qom: Introduce class_post_init() handler

Add a class_post_init() handler to be called by parents *after* child class_init() handler is called. This is necessary to have parent class check children properly set some values or mandatory handlers.

Philippe Mathieu-Daudé (2):
qom: Declare class_base_init() before class_init()
qom: Introduce class_post_init() handler

 include/qom/object.h     | 17 ++++++++++-------
 qom/object.c             | 14 ++++++++++++--
 rust/qemu-api/src/qom.rs |  8 +++++++-
 3 files changed, 29 insertions(+), 10 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/2 qom: Declare class_base_init() before class_init()

Since class_base_init() is called *before* class_init(), move its declaration in TypeInfo before class_init() to help a bit. No logical change.

---
 include/qom/object.h     | 10 +++++-----
 qom/object.c             |  4 ++--
 rust/qemu-api/src/qom.rs |  2 +-
 3 files changed, 8 insertions(+), 8 deletions(-)
include/qom/object.h +5 -5
index 9192265db76..3776267e903 100644
@@ -457,14 +457,14 @@ struct Object
  *   assumed to be the size of the parent class.  This allows a type to avoid
  *   implementing an explicit class type if they are not adding additional
  *   virtual functions.
- * @class_init: This function is called after all parent class initialization
- *   has occurred to allow a class to set its default virtual method pointers.
- *   This is also the function to use to override virtual methods from a parent
- *   class.
  * @class_base_init: This function is called for all base classes after all
  *   parent class initialization has occurred, but before the class itself
  *   is initialized.  This is the function to use to undo the effects of
  *   memcpy from the parent class to the descendants.
+ * @class_init: This function is called after all parent class initialization
+ *   has occurred to allow a class to set its default virtual method pointers.
+ *   This is also the function to use to override virtual methods from a parent
+ *   class.
  * @class_data: Data to pass to the @class_init,
  *   @class_base_init. This can be useful when building dynamic
  *   classes.
@@ -486,8 +486,8 @@ struct TypeInfo
     bool abstract;
     size_t class_size;
 
-    void (*class_init)(ObjectClass *klass, void *data);
     void (*class_base_init)(ObjectClass *klass, void *data);
+    void (*class_init)(ObjectClass *klass, void *data);
     void *class_data;
 
     InterfaceInfo *interfaces;
qom/object.c +2 -2
index ec447f14a78..1a686dff5a0 100644
@@ -54,8 +54,8 @@ struct TypeImpl
     size_t instance_size;
     size_t instance_align;
 
-    void (*class_init)(ObjectClass *klass, void *data);
     void (*class_base_init)(ObjectClass *klass, void *data);
+    void (*class_init)(ObjectClass *klass, void *data);
 
     void *class_data;
 
@@ -119,8 +119,8 @@ static TypeImpl *type_new(const TypeInfo *info)
     ti->instance_size = info->instance_size;
     ti->instance_align = info->instance_align;
 
-    ti->class_init = info->class_init;
     ti->class_base_init = info->class_base_init;
+    ti->class_init = info->class_init;
     ti->class_data = info->class_data;
 
     ti->instance_init = info->instance_init;
rust/qemu-api/src/qom.rs +1 -1
index 97901fb9084..6033228e338 100644
@@ -486,8 +486,8 @@ pub trait ObjectImpl: ObjectType + ClassInitImpl<Self::Class> {
         instance_finalize: Some(drop_object::<Self>),
         abstract_: Self::ABSTRACT,
         class_size: core::mem::size_of::<Self::Class>(),
-        class_init: Some(rust_class_init::<Self>),
         class_base_init: Self::CLASS_BASE_INIT,
+        class_init: Some(rust_class_init::<Self>),
         class_data: core::ptr::null_mut(),
         interfaces: core::ptr::null_mut(),
     };
Signature · 1 line
2.47.1
#2 lore raw

PATCH 2/2 qom: Introduce class_post_init() handler

In reply to Philippe Mathieu-Daudé

Add a class_post_init() handler to be called by parents *after* child class_init() handler is called. This is necessary to have parent class check children properly set some values or mandatory handlers.

---
 include/qom/object.h     |  7 +++++--
 qom/object.c             | 10 ++++++++++
 rust/qemu-api/src/qom.rs |  6 ++++++
 3 files changed, 21 insertions(+), 2 deletions(-)
include/qom/object.h +5 -2
index 3776267e903..d1e437a81be 100644
@@ -465,8 +465,10 @@ struct Object
  *   has occurred to allow a class to set its default virtual method pointers.
  *   This is also the function to use to override virtual methods from a parent
  *   class.
- * @class_data: Data to pass to the @class_init,
- *   @class_base_init. This can be useful when building dynamic
+ * @class_post_init: This function is called to finish initialization of
+ *   a class, after all @class_init functions were called.
+ * @class_data: Data to pass to the @class_base_init, @class_init, and
+ *   @class_post_init. This can be useful when building dynamic
  *   classes.
  * @interfaces: The list of interfaces associated with this type.  This
  *   should point to a static array that's terminated with a zero filled
@@ -488,6 +490,7 @@ struct TypeInfo
 
     void (*class_base_init)(ObjectClass *klass, void *data);
     void (*class_init)(ObjectClass *klass, void *data);
+    void (*class_post_init)(ObjectClass *klass, void *data);
     void *class_data;
 
     InterfaceInfo *interfaces;
qom/object.c +10 -0
index 1a686dff5a0..694009f0c94 100644
@@ -56,6 +56,7 @@ struct TypeImpl
 
     void (*class_base_init)(ObjectClass *klass, void *data);
     void (*class_init)(ObjectClass *klass, void *data);
+    void (*class_post_init)(ObjectClass *klass, void *data);
 
     void *class_data;
 
@@ -121,6 +122,7 @@ static TypeImpl *type_new(const TypeInfo *info)
 
     ti->class_base_init = info->class_base_init;
     ti->class_init = info->class_init;
+    ti->class_post_init = info->class_post_init;
     ti->class_data = info->class_data;
 
     ti->instance_init = info->instance_init;
@@ -416,6 +418,14 @@ static void type_initialize(TypeImpl *ti)
     if (ti->class_init) {
         ti->class_init(ti->class, ti->class_data);
     }
+
+    parent = type_get_parent(ti);
+    while (parent) {
+        if (parent->class_post_init) {
+            parent->class_post_init(ti->class, ti->class_data);
+        }
+        parent = type_get_parent(parent);
+    }
 }
 
 static void object_init_with_type(Object *obj, TypeImpl *ti)
rust/qemu-api/src/qom.rs +6 -0
index 6033228e338..d95e90153a9 100644
@@ -470,6 +470,11 @@ pub trait ObjectImpl: ObjectType + ClassInitImpl<Self::Class> {
         unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
     > = None;
 
+    /// Called to finish initialization of a class.
+    const CLASS_POST_INIT: Option<
+        unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
+    > = None;
+
     const TYPE_INFO: TypeInfo = TypeInfo {
         name: Self::TYPE_NAME.as_ptr(),
         parent: Self::ParentType::TYPE_NAME.as_ptr(),
@@ -488,6 +493,7 @@ pub trait ObjectImpl: ObjectType + ClassInitImpl<Self::Class> {
         class_size: core::mem::size_of::<Self::Class>(),
         class_base_init: Self::CLASS_BASE_INIT,
         class_init: Some(rust_class_init::<Self>),
+        class_post_init: Self::CLASS_POST_INIT,
         class_data: core::ptr::null_mut(),
         interfaces: core::ptr::null_mut(),
     };
Signature · 1 line
2.47.1
#3 lore raw
Quoted 6 lines · Philippe Mathieu-Daudé

Add a class_post_init() handler to be called by parents *after* child class_init() handler is called. This is necessary to have parent class check children properly set some values or mandatory handlers.

Plausible, but I'd like to see actual use. What uses do you have in mind?

#4 lore raw
Quoted 11 lines · Markus Armbruster
Quoted 6 lines · Philippe Mathieu-Daudé

Add a class_post_init() handler to be called by parents *after* child class_init() handler is called. This is necessary to have parent class check children properly set some values or mandatory handlers.

Plausible, but I'd like to see actual use. What uses do you have in mind?

See for example:
https://lore.kernel.org/qemu-devel/[email protected]/

Where the abstract parent CPUClass checks concrete target implementation registered a handler.

(I'll cc you on another use shortly).

Reviewed-by on the v1 series
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

Add a class_post_init() handler to be called by parents *after* child class_init() handler is called. This is necessary to have parent class check children properly set some values or mandatory handlers.

Quoted 9 lines

Philippe Mathieu-Daudé (2):
qom: Declare class_base_init() before class_init()
qom: Introduce class_post_init() handler

  include/qom/object.h     | 17 ++++++++++-------
  qom/object.c             | 14 ++++++++++++--
  rust/qemu-api/src/qom.rs |  8 +++++++-
  3 files changed, 29 insertions(+), 10 deletions(-)

r~

Acked-by on the v1 series
#6 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

Add a class_post_init() handler to be called by parents *after* child class_init() handler is called. This is necessary to have parent class check children properly set some values or mandatory handlers.

Quoted 8 lines

Philippe Mathieu-Daudé (2):
qom: Declare class_base_init() before class_init()
qom: Introduce class_post_init() handler

  include/qom/object.h     | 17 ++++++++++-------
  qom/object.c             | 14 ++++++++++++--
  rust/qemu-api/src/qom.rs |  8 +++++++-
  3 files changed, 29 insertions(+), 10 deletions(-)

Paolo