[RFC PATCH v2 122/137] system: Parent per-MR helper objects under their owner

Alexander Graf <[email protected]>
Newsgroups org.nongnu.qemu-arm,org.nongnu.qemu-devel,org.nongnu.qemu-riscv
Message-ID <[email protected]>
RamBlockAttributes and MemoryRegionPortioList objects are long-lived
helpers whose lifetime is already tied to a MemoryRegion or its owner.
Give each a canonical path under that owner instead of leaving them
as floating refcounted objects:

  <mr-path>/attributes           (RamBlockAttributes)
  <owner-path>/portio-list[N]    (MemoryRegionPortioList)

The destroy paths switch from object_unref() to object_unparent()
now that the parent holds the reference.

RamDiscardManager is deliberately left as-is: it is unref'd from
memory_region_finalize() itself, so making it a child<> of the MR
would introduce a use-after-free during MR teardown.  It can move
under a different owner in a follow-up once the RDM lifecycle is
untangled from MR finalize.

AI-used-for: code (refactoring)
Signed-off-by: Alexander Graf <[email protected]>
---
 system/ioport.c               | 5 +++--
 system/ram-block-attributes.c | 7 ++++---
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/system/ioport.c b/system/ioport.c
index 0415b3cd60..77ab77d7be 100644
--- a/system/ioport.c
+++ b/system/ioport.c
@@ -145,7 +145,7 @@ void portio_list_destroy(PortioList *piolist)
     for (i = 0; i < piolist->nr; ++i) {
         mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr);
         object_unparent(OBJECT(&mrpio->mr));
-        object_unref(mrpio);
+        object_unparent(OBJECT(mrpio));
     }
     g_free(piolist->regions);
 }
@@ -228,7 +228,8 @@ static void portio_list_add_1(PortioList *piolist,
 
     /* Copy the sub-list and null-terminate it.  */
     mrpio = MEMORY_REGION_PORTIO_LIST(
-                object_new(TYPE_MEMORY_REGION_PORTIO_LIST));
+                object_new_child(piolist->owner, "portio-list[*]",
+                                 TYPE_MEMORY_REGION_PORTIO_LIST));
     mrpio->portio_opaque = piolist->opaque;
     mrpio->ports = g_new0(MemoryRegionPortio, count + 1);
     memcpy(mrpio->ports, pio_init, sizeof(MemoryRegionPortio) * count);
diff --git a/system/ram-block-attributes.c b/system/ram-block-attributes.c
index 59ec7a28eb..0967bed6d3 100644
--- a/system/ram-block-attributes.c
+++ b/system/ram-block-attributes.c
@@ -182,12 +182,13 @@ RamBlockAttributes *ram_block_attributes_create(RAMBlock *ram_block)
     RamBlockAttributes *attr;
     MemoryRegion *mr = ram_block->mr;
 
-    attr = RAM_BLOCK_ATTRIBUTES(object_new(TYPE_RAM_BLOCK_ATTRIBUTES));
+    attr = RAM_BLOCK_ATTRIBUTES(object_new_child(OBJECT(mr), "attributes",
+                                                 TYPE_RAM_BLOCK_ATTRIBUTES));
 
     attr->ram_block = ram_block;
 
     if (memory_region_add_ram_discard_source(mr, RAM_DISCARD_SOURCE(attr))) {
-        object_unref(OBJECT(attr));
+        object_unparent(OBJECT(attr));
         return NULL;
     }
     attr->bitmap_size = DIV_ROUND_UP(int128_get64(mr->size), block_size);
@@ -202,7 +203,7 @@ void ram_block_attributes_destroy(RamBlockAttributes *attr)
 
     g_free(attr->bitmap);
     memory_region_del_ram_discard_source(attr->ram_block->mr, RAM_DISCARD_SOURCE(attr));
-    object_unref(OBJECT(attr));
+    object_unparent(OBJECT(attr));
 }
 
 static void ram_block_attributes_init(Object *obj)
-- 
2.47.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.