Patchwork [03/25] qom: add class_base_init

login
register
mail settings
Submitter Paolo Bonzini
Date April 3, 2012, 11:15 a.m.
Message ID <1333451753-3550-4-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/150410/
State New
Headers show

Comments

Paolo Bonzini - April 3, 2012, 11:15 a.m.
The class_base_init TypeInfo callback was present in one of the early
QOM versions but removed (on my request...) before committing.  We
will need it soon, add it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/qemu/object.h |   10 ++++++++--
 qom/object.c          |    9 +++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
Anthony Liguori - April 3, 2012, 8:51 p.m.
On 04/03/2012 06:15 AM, Paolo Bonzini wrote:
> The class_base_init TypeInfo callback was present in one of the early
> QOM versions but removed (on my request...) before committing.  We
> will need it soon, add it.
>
> Signed-off-by: Paolo Bonzini<pbonzini@redhat.com>

Heh, funny how that worked out :-)

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>

Regards,

Anthony Liguori

> ---
>   include/qemu/object.h |   10 ++++++++--
>   qom/object.c          |    9 +++++++++
>   2 files changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/include/qemu/object.h b/include/qemu/object.h
> index 470efe3..ccaea7d 100644
> --- a/include/qemu/object.h
> +++ b/include/qemu/object.h
> @@ -291,10 +291,15 @@ 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_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 descendents.
>    * @class_finalize: This function is called during class destruction and is
>    *   meant to release and dynamic parameters allocated by @class_init.
> - * @class_data: Data to pass to the @class_init and @class_finalize functions.
> - *   This can be useful when building dynamic classes.
> + * @class_data: Data to pass to the @class_init, @class_base_init and
> + *   @class_finalize functions.  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
>    *   element.
> @@ -312,6 +317,7 @@ struct TypeInfo
>       size_t class_size;
>
>       void (*class_init)(ObjectClass *klass, void *data);
> +    void (*class_base_init)(ObjectClass *klass, void *data);
>       void (*class_finalize)(ObjectClass *klass, void *data);
>       void *class_data;
>
> diff --git a/qom/object.c b/qom/object.c
> index 4975d93..6ff1c19 100644
> --- a/qom/object.c
> +++ b/qom/object.c
> @@ -45,6 +45,7 @@ struct TypeImpl
>       size_t instance_size;
>
>       void (*class_init)(ObjectClass *klass, void *data);
> +    void (*class_base_init)(ObjectClass *klass, void *data);
>       void (*class_finalize)(ObjectClass *klass, void *data);
>
>       void *class_data;
> @@ -112,6 +113,7 @@ TypeImpl *type_register(const TypeInfo *info)
>       ti->instance_size = info->instance_size;
>
>       ti->class_init = info->class_init;
> +    ti->class_base_init = info->class_base_init;
>       ti->class_finalize = info->class_finalize;
>       ti->class_data = info->class_data;
>
> @@ -232,6 +234,13 @@ static void type_initialize(TypeImpl *ti)
>           memcpy((void *)ti->class + sizeof(ObjectClass),
>                  (void *)parent->class + sizeof(ObjectClass),
>                  parent->class_size - sizeof(ObjectClass));
> +
> +        while (parent) {
> +            if (parent->class_base_init) {
> +                parent->class_base_init(ti->class, ti->class_data);
> +            }
> +            parent = type_get_parent(parent);
> +        }
>       }
>
>       memset((void *)ti->class + class_size, 0, ti->class_size - class_size);

Patch

diff --git a/include/qemu/object.h b/include/qemu/object.h
index 470efe3..ccaea7d 100644
--- a/include/qemu/object.h
+++ b/include/qemu/object.h
@@ -291,10 +291,15 @@  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_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 descendents.
  * @class_finalize: This function is called during class destruction and is
  *   meant to release and dynamic parameters allocated by @class_init.
- * @class_data: Data to pass to the @class_init and @class_finalize functions.
- *   This can be useful when building dynamic classes.
+ * @class_data: Data to pass to the @class_init, @class_base_init and
+ *   @class_finalize functions.  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
  *   element.
@@ -312,6 +317,7 @@  struct TypeInfo
     size_t class_size;
 
     void (*class_init)(ObjectClass *klass, void *data);
+    void (*class_base_init)(ObjectClass *klass, void *data);
     void (*class_finalize)(ObjectClass *klass, void *data);
     void *class_data;
 
diff --git a/qom/object.c b/qom/object.c
index 4975d93..6ff1c19 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -45,6 +45,7 @@  struct TypeImpl
     size_t instance_size;
 
     void (*class_init)(ObjectClass *klass, void *data);
+    void (*class_base_init)(ObjectClass *klass, void *data);
     void (*class_finalize)(ObjectClass *klass, void *data);
 
     void *class_data;
@@ -112,6 +113,7 @@  TypeImpl *type_register(const TypeInfo *info)
     ti->instance_size = info->instance_size;
 
     ti->class_init = info->class_init;
+    ti->class_base_init = info->class_base_init;
     ti->class_finalize = info->class_finalize;
     ti->class_data = info->class_data;
 
@@ -232,6 +234,13 @@  static void type_initialize(TypeImpl *ti)
         memcpy((void *)ti->class + sizeof(ObjectClass),
                (void *)parent->class + sizeof(ObjectClass),
                parent->class_size - sizeof(ObjectClass));
+
+        while (parent) {
+            if (parent->class_base_init) {
+                parent->class_base_init(ti->class, ti->class_data);
+            }
+            parent = type_get_parent(parent);
+        }
     }
 
     memset((void *)ti->class + class_size, 0, ti->class_size - class_size);