Rather than use the .parent == NULL check to determine if a memory
region is contained, add a purpose specific boolean flag. This allows
for .parent to be easily converted to a link property while preserving
all the semantics of the legacy API.

Signed-off-by: Peter Crosthwaite <peter.crosthwa...@xilinx.com>
---

 include/exec/memory.h | 1 +
 memory.c              | 9 +++++----
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/include/exec/memory.h b/include/exec/memory.h
index 371c066..2bb074f 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -142,6 +142,7 @@ struct MemoryRegion {
     void *opaque;
     struct Object *owner;
     MemoryRegion *parent;
+    bool contained;
     Int128 size;
     hwaddr addr;
     void (*destructor)(MemoryRegion *mr);
diff --git a/memory.c b/memory.c
index 3c32d5a..a37fdd2 100644
--- a/memory.c
+++ b/memory.c
@@ -1451,6 +1451,7 @@ static void 
do_memory_region_add_subregion_common(MemoryRegion *subregion)
     QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
 done:
     memory_region_update_pending |= mr->enabled && subregion->enabled;
+    subregion->contained = true;
     memory_region_transaction_commit();
 }
 
@@ -1487,8 +1488,8 @@ void memory_region_del_subregion(MemoryRegion *mr,
                                  MemoryRegion *subregion)
 {
     memory_region_transaction_begin();
-    assert(subregion->parent == mr);
-    subregion->parent = NULL;
+    assert(subregion->parent == mr && subregion->contained);
+    subregion->contained = false;
     QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
     memory_region_unref(subregion);
     memory_region_update_pending |= mr->enabled && subregion->enabled;
@@ -1510,7 +1511,7 @@ void memory_region_set_address(MemoryRegion *mr, hwaddr 
addr)
 {
     MemoryRegion *parent = mr->parent;
 
-    if (addr == mr->addr || !parent) {
+    if (addr == mr->addr || !mr->contained) {
         mr->addr = addr;
         return;
     }
@@ -1518,7 +1519,7 @@ void memory_region_set_address(MemoryRegion *mr, hwaddr 
addr)
     memory_region_transaction_begin();
     memory_region_ref(mr);
     memory_region_del_subregion(parent, mr);
-    memory_region_add_subregion_common(parent, addr, mr);
+    do_memory_region_add_subregion_common(mr);
     memory_region_unref(mr);
     memory_region_transaction_commit();
 }
-- 
1.9.3.1.ga73a6ad


Reply via email to