Skip to contentSkip to conversation

qom: Introduce object*_property_add_bool_ptr() functions

PATCH In review Eduardo Habkost 2 participants 6 messages Oct 22, 2020 – Oct 23, 2020

lore
Share & tools
lore
#0 lore raw

cover qom: Introduce object*_property_add_bool_ptr() functions

Based-on: [email protected] Git branch: https://github.com/ehabkost/qemu work/qom-bool-ptr-prop

This series introduces a helper to make it easier to register simple boolan QOM properties. It will be useful for simplifying existing property code in some types that can't use QDEV_PROP_BOOL yet (because they are not TYPE_DEVICE subtypes). As examples, some TYPE_MACHINE and TYPE_QAUTHZ_LIST_FILE properties are converted to use the new functions.

This depends on the QOM property code cleanup that was also submitted as part of:

https://lore.kernel.org/qemu-devel/[email protected]
Subject: [PATCH 00/12] qom: Make all -object types use only class properties<

Eduardo Habkost (4):
qom: object*_property_add_bool_ptr() functions autz/listfile: Use object_class_property_add_bool_ptr()
machine: Use object_class_property_add_bool_ptr() when possible
pc: Use object_class_property_add_bool_ptr()

 include/qom/object.h | 23 +++++++++++++
 authz/listfile.c     | 27 ++-------------
 hw/core/machine.c    | 78 ++++++++------------------------------------
 hw/i386/pc.c         | 57 +++++---------------------------
 qom/object.c         | 31 ++++++++++++++++++
 5 files changed, 79 insertions(+), 137 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/4 qom: object*_property_add_bool_ptr() functions

Provide helpers for registering boolean properties that simply read/write a struct field, to reduce the need to manually write property getters and setters.

---
Cc: Paolo Bonzini <[email protected]>
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: Eduardo Habkost <[email protected]>
Cc: [email protected]

---
 include/qom/object.h | 23 +++++++++++++++++++++++
 qom/object.c         | 31 +++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
include/qom/object.h +23 -0
index a124cf897d..954a26c567 100644
@@ -1815,6 +1815,29 @@ ObjectProperty *object_class_property_add_uint64_ptr(ObjectClass *klass,
                                           ptrdiff_t offset,
                                           ObjectPropertyFlags flags);
 
+/**
+ * object_property_add_bool_ptr:
+ * @obj: the object to add a property to
+ * @name: the name of the property
+ * @v: pointer to value
+ * @flags: bitwise-or'd ObjectPropertyFlags
+ *
+ * Add an bool property in memory.  This function will add a
+ * property of type 'bool'.
+ *
+ * Returns: The newly added property on success, or %NULL on failure.
+ */
+ObjectProperty *
+object_property_add_bool_ptr(Object *obj, const char *name,
+                             bool *v,
+                             ObjectPropertyFlags flags);
+
+ObjectProperty *
+object_class_property_add_bool_ptr(ObjectClass *klass,
+                                   const char *name,
+                                   ptrdiff_t offset,
+                                   ObjectPropertyFlags flags);
+
 /**
  * object_property_add_alias:
  * @obj: the object to add a property to
qom/object.c +31 -0
index 73f27b8b7e..2abc2bda33 100644
@@ -2713,6 +2713,37 @@ object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
                                               flags, offset);
 }
 
+static void property_visit_bool_ptr(Object *obj, Visitor *v, const char *name,
+                                    void *opaque, Error **errp)
+{
+    PointerProperty *prop = opaque;
+    bool *field = pointer_property_get_ptr(obj, prop);
+    visit_type_bool(v, name, field, errp);
+}
+
+ObjectProperty *
+object_property_add_bool_ptr(Object *obj, const char *name,
+                             bool *v,
+                             ObjectPropertyFlags flags)
+{
+    return object_property_add_uint_ptr(obj, name, "bool",
+                                        property_visit_bool_ptr,
+                                        property_visit_bool_ptr,
+                                        flags,
+                                        (void *)v);
+}
+
+ObjectProperty *
+object_class_property_add_bool_ptr(ObjectClass *klass, const char *name,
+                                   ptrdiff_t offset,
+                                   ObjectPropertyFlags flags)
+{
+    return object_class_property_add_uint_ptr(klass, name, "bool",
+                                              property_visit_bool_ptr,
+                                              property_visit_bool_ptr,
+                                              flags, offset);
+}
+
 typedef struct {
     Object *target_obj;
     char *target_name;
Signature · 1 line
2.28.0
#2 lore raw

PATCH 2/4 autz/listfile: Use object_class_property_add_bool_ptr()

In reply to Eduardo Habkost

---
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: [email protected]

---
 authz/listfile.c | 27 +++------------------------
 1 file changed, 3 insertions(+), 24 deletions(-)
authz/listfile.c +3 -24
index aaf930453d..911c4e45f2 100644
@@ -184,27 +184,6 @@ qauthz_list_file_prop_get_filename(Object *obj,
 }
 
 
-static void
-qauthz_list_file_prop_set_refresh(Object *obj,
-                                  bool value,
-                                  Error **errp G_GNUC_UNUSED)
-{
-    QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
-
-    fauthz->refresh = value;
-}
-
-
-static bool
-qauthz_list_file_prop_get_refresh(Object *obj,
-                                  Error **errp G_GNUC_UNUSED)
-{
-    QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
-
-    return fauthz->refresh;
-}
-
-
 static void
 qauthz_list_file_finalize(Object *obj)
 {
@@ -227,9 +206,9 @@ qauthz_list_file_class_init(ObjectClass *oc, void *data)
     object_class_property_add_str(oc, "filename",
                                   qauthz_list_file_prop_get_filename,
                                   qauthz_list_file_prop_set_filename);
-    object_class_property_add_bool(oc, "refresh",
-                                   qauthz_list_file_prop_get_refresh,
-                                   qauthz_list_file_prop_set_refresh);
+    object_class_property_add_bool_ptr(oc, "refresh",
+                                       offsetof(QAuthZListFile, refresh),
+                                       OBJ_PROP_FLAG_READWRITE);
 
     authz->is_allowed = qauthz_list_file_is_allowed;
 }
Signature · 1 line
2.28.0
#3 lore raw

PATCH 3/4 machine: Use object_class_property_add_bool_ptr() when possible

In reply to Eduardo Habkost

Get rid if some manually written properties getters/setters.

Not all properties could be converted because they have extra logic in the property setter.

---
Cc: Eduardo Habkost <[email protected]>
Cc: Marcel Apfelbaum <[email protected]>
Cc: [email protected]

---
 hw/core/machine.c | 78 ++++++++---------------------------------------
 1 file changed, 13 insertions(+), 65 deletions(-)
hw/core/machine.c +13 -65
index d740a7e963..21cad22b9e 100644
@@ -325,34 +325,6 @@ static void machine_set_dt_compatible(Object *obj, const char *value, Error **er
     ms->dt_compatible = g_strdup(value);
 }
 
-static bool machine_get_dump_guest_core(Object *obj, Error **errp)
-{
-    MachineState *ms = MACHINE(obj);
-
-    return ms->dump_guest_core;
-}
-
-static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
-{
-    MachineState *ms = MACHINE(obj);
-
-    ms->dump_guest_core = value;
-}
-
-static bool machine_get_mem_merge(Object *obj, Error **errp)
-{
-    MachineState *ms = MACHINE(obj);
-
-    return ms->mem_merge;
-}
-
-static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
-{
-    MachineState *ms = MACHINE(obj);
-
-    ms->mem_merge = value;
-}
-
 static bool machine_get_usb(Object *obj, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
@@ -368,20 +340,6 @@ static void machine_set_usb(Object *obj, bool value, Error **errp)
     ms->usb_disabled = !value;
 }
 
-static bool machine_get_graphics(Object *obj, Error **errp)
-{
-    MachineState *ms = MACHINE(obj);
-
-    return ms->enable_graphics;
-}
-
-static void machine_set_graphics(Object *obj, bool value, Error **errp)
-{
-    MachineState *ms = MACHINE(obj);
-
-    ms->enable_graphics = value;
-}
-
 static char *machine_get_firmware(Object *obj, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
@@ -397,20 +355,6 @@ static void machine_set_firmware(Object *obj, const char *value, Error **errp)
     ms->firmware = g_strdup(value);
 }
 
-static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
-{
-    MachineState *ms = MACHINE(obj);
-
-    ms->suppress_vmdesc = value;
-}
-
-static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
-{
-    MachineState *ms = MACHINE(obj);
-
-    return ms->suppress_vmdesc;
-}
-
 static void machine_set_enforce_config_section(Object *obj, bool value,
                                              Error **errp)
 {
@@ -449,7 +393,7 @@ static void machine_set_memory_encryption(Object *obj, const char *value,
      * so there's no point in it trying to merge areas.
      */
     if (value) {
-        machine_set_mem_merge(obj, false, errp);
+        ms->mem_merge = false;
     }
 }
 
@@ -827,13 +771,15 @@ static void machine_class_init(ObjectClass *oc, void *data)
     object_class_property_set_description(oc, "dt-compatible",
         "Overrides the \"compatible\" property of the dt root node");
 
-    object_class_property_add_bool(oc, "dump-guest-core",
-        machine_get_dump_guest_core, machine_set_dump_guest_core);
+    object_class_property_add_bool_ptr(oc, "dump-guest-core",
+                                       offsetof(MachineState, dump_guest_core),
+                                       OBJ_PROP_FLAG_READWRITE);
     object_class_property_set_description(oc, "dump-guest-core",
         "Include guest memory in a core dump");
 
-    object_class_property_add_bool(oc, "mem-merge",
-        machine_get_mem_merge, machine_set_mem_merge);
+    object_class_property_add_bool_ptr(oc, "mem-merge",
+                                       offsetof(MachineState, mem_merge),
+                                       OBJ_PROP_FLAG_READWRITE);
     object_class_property_set_description(oc, "mem-merge",
         "Enable/disable memory merge support");
 
@@ -842,8 +788,9 @@ static void machine_class_init(ObjectClass *oc, void *data)
     object_class_property_set_description(oc, "usb",
         "Set on/off to enable/disable usb");
 
-    object_class_property_add_bool(oc, "graphics",
-        machine_get_graphics, machine_set_graphics);
+    object_class_property_add_bool_ptr(oc, "graphics",
+                                       offsetof(MachineState, enable_graphics),
+                                       OBJ_PROP_FLAG_READWRITE);
     object_class_property_set_description(oc, "graphics",
         "Set on/off to enable/disable graphics emulation");
 
@@ -852,8 +799,9 @@ static void machine_class_init(ObjectClass *oc, void *data)
     object_class_property_set_description(oc, "firmware",
         "Firmware image");
 
-    object_class_property_add_bool(oc, "suppress-vmdesc",
-        machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
+    object_class_property_add_bool_ptr(oc, "suppress-vmdesc",
+                                       offsetof(MachineState, suppress_vmdesc),
+                                       OBJ_PROP_FLAG_READWRITE);
     object_class_property_set_description(oc, "suppress-vmdesc",
         "Set on to disable self-describing migration");
 
Signature · 1 line
2.28.0
#4 lore raw

PATCH 4/4 pc: Use object_class_property_add_bool_ptr()

In reply to Eduardo Habkost

Get rid of manually written property getters/setters.

---
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Eduardo Habkost <[email protected]>
Cc: "Michael S. Tsirkin" <[email protected]>
Cc: Marcel Apfelbaum <[email protected]>
Cc: [email protected]

---
 hw/i386/pc.c | 57 +++++++++-------------------------------------------
 1 file changed, 9 insertions(+), 48 deletions(-)
hw/i386/pc.c +9 -48
index 4e323755d0..d5a5b1b2ae 100644
@@ -1493,48 +1493,6 @@ static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
     visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
 }
 
-static bool pc_machine_get_smbus(Object *obj, Error **errp)
-{
-    PCMachineState *pcms = PC_MACHINE(obj);
-
-    return pcms->smbus_enabled;
-}
-
-static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
-{
-    PCMachineState *pcms = PC_MACHINE(obj);
-
-    pcms->smbus_enabled = value;
-}
-
-static bool pc_machine_get_sata(Object *obj, Error **errp)
-{
-    PCMachineState *pcms = PC_MACHINE(obj);
-
-    return pcms->sata_enabled;
-}
-
-static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
-{
-    PCMachineState *pcms = PC_MACHINE(obj);
-
-    pcms->sata_enabled = value;
-}
-
-static bool pc_machine_get_pit(Object *obj, Error **errp)
-{
-    PCMachineState *pcms = PC_MACHINE(obj);
-
-    return pcms->pit_enabled;
-}
-
-static void pc_machine_set_pit(Object *obj, bool value, Error **errp)
-{
-    PCMachineState *pcms = PC_MACHINE(obj);
-
-    pcms->pit_enabled = value;
-}
-
 static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
                                             const char *name, void *opaque,
                                             Error **errp)
@@ -1697,14 +1655,17 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
     object_class_property_set_description(oc, PC_MACHINE_VMPORT,
         "Enable vmport (pc & q35)");
 
-    object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
-        pc_machine_get_smbus, pc_machine_set_smbus);
+    object_class_property_add_bool_ptr(oc, PC_MACHINE_SMBUS,
+                                       offsetof(PCMachineState, smbus_enabled),
+                                       OBJ_PROP_FLAG_READWRITE);
 
-    object_class_property_add_bool(oc, PC_MACHINE_SATA,
-        pc_machine_get_sata, pc_machine_set_sata);
+    object_class_property_add_bool_ptr(oc, PC_MACHINE_SATA,
+                                       offsetof(PCMachineState, sata_enabled),
+                                       OBJ_PROP_FLAG_READWRITE);
 
-    object_class_property_add_bool(oc, PC_MACHINE_PIT,
-        pc_machine_get_pit, pc_machine_set_pit);
+    object_class_property_add_bool_ptr(oc, PC_MACHINE_PIT,
+                                       offsetof(PCMachineState, pit_enabled),
+                                       OBJ_PROP_FLAG_READWRITE);
 }
 
 static const TypeInfo pc_machine_info = {
Signature · 1 line
2.28.0
Reviewed-by on the v1 series
#5 lore raw
In reply to Eduardo Habkost
Quoted 22 lines · Eduardo Habkost

Based-on: [email protected] Git branch: https://github.com/ehabkost/qemu work/qom-bool-ptr-prop

This series introduces a helper to make it easier to register simple boolan QOM properties. It will be useful for simplifying existing property code in some types that can't use QDEV_PROP_BOOL yet (because they are not TYPE_DEVICE subtypes). As examples, some TYPE_MACHINE and TYPE_QAUTHZ_LIST_FILE properties are converted to use the new functions.

This depends on the QOM property code cleanup that was also submitted as part of:

https://lore.kernel.org/qemu-devel/[email protected]
Subject: [PATCH 00/12] qom: Make all -object types use only class properties<

Eduardo Habkost (4):
qom: object*_property_add_bool_ptr() functions autz/listfile: Use object_class_property_add_bool_ptr()
machine: Use object_class_property_add_bool_ptr() when possible
pc: Use object_class_property_add_bool_ptr()

Quoted 6 lines
  include/qom/object.h | 23 +++++++++++++
  authz/listfile.c     | 27 ++-------------
  hw/core/machine.c    | 78 ++++++++------------------------------------
  hw/i386/pc.c         | 57 +++++---------------------------
  qom/object.c         | 31 ++++++++++++++++++
  5 files changed, 79 insertions(+), 137 deletions(-)