diff mbox series

[10/12] qom: Create system containers explicitly

Message ID 20241120215703.3918445-11-peterx@redhat.com (mailing list archive)
State New
Headers show
Series QOM: container_get() removal | expand

Commit Message

Peter Xu Nov. 20, 2024, 9:57 p.m. UTC
Always explicitly create QEMU system containers upfront.

Root containers will be created when trying to fetch the root object the
1st time.  Machine sub-containers will be created only until machine is
being initialized.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 hw/core/machine.c | 19 ++++++++++++++++---
 qom/object.c      | 16 +++++++++++++++-
 2 files changed, 31 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/hw/core/machine.c b/hw/core/machine.c
index a35c4a8fae..a184dbf8f0 100644
--- a/hw/core/machine.c
+++ b/hw/core/machine.c
@@ -1193,14 +1193,27 @@  static void machine_class_base_init(ObjectClass *oc, void *data)
     }
 }
 
+const char *machine_containers[] = {
+    "unattached",
+    "peripheral",
+    "peripheral-anon"
+};
+
+static void qemu_create_machine_containers(Object *machine)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(machine_containers); i++) {
+        container_create(machine, machine_containers[i]);
+    }
+}
+
 static void machine_initfn(Object *obj)
 {
     MachineState *ms = MACHINE(obj);
     MachineClass *mc = MACHINE_GET_CLASS(obj);
 
-    container_get(obj, "/peripheral");
-    container_get(obj, "/peripheral-anon");
-
+    qemu_create_machine_containers(obj);
     ms->dump_guest_core = true;
     ms->mem_merge = (QEMU_MADV_MERGEABLE != QEMU_MADV_INVALID);
     ms->enable_graphics = true;
diff --git a/qom/object.c b/qom/object.c
index 214d6eb4c1..810e6f2bd9 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1734,12 +1734,26 @@  const char *object_property_get_type(Object *obj, const char *name, Error **errp
     return prop->type;
 }
 
+static Object *object_root_initialize(void)
+{
+    Object *root = object_new(TYPE_CONTAINER);
+
+    /*
+     * Create all QEMU system containers.  "machine" and its sub-containers
+     * are only created when machine initializes (qemu_create_machine()).
+     */
+    container_create(root, "chardevs");
+    container_create(root, "objects");
+
+    return root;
+}
+
 Object *object_get_root(void)
 {
     static Object *root;
 
     if (!root) {
-        root = object_new(TYPE_CONTAINER);
+        root = object_root_initialize();
     }
 
     return root;