Skip to contentSkip to conversation

physmem: Simplify dirty memory type checks with loop

PATCH In review Bin Guo 3 participants 3 messages Apr 1

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

PATCH 1/1 physmem: Simplify dirty memory type checks with loop

In physical_memory_range_includes_clean(), we have three nearly identical if-statements checking different DIRTY_MEMORY types (VGA, CODE, MIGRATION). This code duplication makes maintenance harder and increases the risk of inconsistencies when adding new dirty memory types.

Replace the repetitive checks with a simple loop that iterates through all DIRTY_MEMORY_NUM types, checking only those specified in the mask. This reduces code size and makes it easier to add new dirty memory types in the future.

---
 system/physmem.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
system/physmem.c +5 -11
index 4e26f1a1d4..c58d940e80 100644
@@ -981,17 +981,11 @@ uint8_t physical_memory_range_includes_clean(ram_addr_t start,
 {
     uint8_t ret = 0;
 
-    if (mask & (1 << DIRTY_MEMORY_VGA) &&
-        !physical_memory_all_dirty(start, length, DIRTY_MEMORY_VGA)) {
-        ret |= (1 << DIRTY_MEMORY_VGA);
-    }
-    if (mask & (1 << DIRTY_MEMORY_CODE) &&
-        !physical_memory_all_dirty(start, length, DIRTY_MEMORY_CODE)) {
-        ret |= (1 << DIRTY_MEMORY_CODE);
-    }
-    if (mask & (1 << DIRTY_MEMORY_MIGRATION) &&
-        !physical_memory_all_dirty(start, length, DIRTY_MEMORY_MIGRATION)) {
-        ret |= (1 << DIRTY_MEMORY_MIGRATION);
+    for (int i = 0; i < DIRTY_MEMORY_NUM; i++) {
+        if ((mask & (1 << i)) &&
+            !physical_memory_all_dirty(start, length, i)) {
+            ret |= (1 << i);
+        }
     }
     return ret;
 }
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Bin Guo

In physical_memory_range_includes_clean(), we have three nearly identical if-statements checking different DIRTY_MEMORY types (VGA, CODE, MIGRATION). This code duplication makes maintenance harder and increases the risk of inconsistencies when adding new dirty memory types.

Replace the repetitive checks with a simple loop that iterates through all DIRTY_MEMORY_NUM types, checking only those specified in the mask. This reduces code size and makes it easier to add new dirty memory types in the future.

Quoted 4 lines
---
  system/physmem.c | 16 +++++-----------
  1 file changed, 5 insertions(+), 11 deletions(-)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Bin Guo
Quoted 11 lines · Bin Guo

In physical_memory_range_includes_clean(), we have three nearly identical if-statements checking different DIRTY_MEMORY types (VGA, CODE, MIGRATION). This code duplication makes maintenance harder and increases the risk of inconsistencies when adding new dirty memory types.

Replace the repetitive checks with a simple loop that iterates through all DIRTY_MEMORY_NUM types, checking only those specified in the mask. This reduces code size and makes it easier to add new dirty memory types in the future.

Signature · 1 line
Peter Xu