aboutsummaryrefslogtreecommitdiff
path: root/memory.c
diff options
context:
space:
mode:
authorAvi Kivity <avi@redhat.com>2011-07-26 14:26:03 +0300
committerAnthony Liguori <aliguori@us.ibm.com>2011-07-29 08:25:42 -0500
commit3d8e6bf97781a8415fd08ba1770269e1332c200c (patch)
tree2fa132f25c57a53e1034f9efef3e03f37cbe03f2 /memory.c
parent5a583347697413f964d886b074860cb888c0e306 (diff)
memory: merge adjacent segments of a single memory region
Simple implementations of memory routers, for example the Cirrus VGA memory banks or the 440FX PAM registers can generate adjacent memory regions which are contiguous. Detect these and merge them; this saves kvm memory slots and shortens lookup times. Signed-off-by: Avi Kivity <avi@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Diffstat (limited to 'memory.c')
-rw-r--r--memory.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/memory.c b/memory.c
index d858b47739..121f9e144e 100644
--- a/memory.c
+++ b/memory.c
@@ -122,6 +122,34 @@ static void flatview_destroy(FlatView *view)
qemu_free(view->ranges);
}
+static bool can_merge(FlatRange *r1, FlatRange *r2)
+{
+ return addrrange_end(r1->addr) == r2->addr.start
+ && r1->mr == r2->mr
+ && r1->offset_in_region + r1->addr.size == r2->offset_in_region
+ && r1->dirty_log_mask == r2->dirty_log_mask;
+}
+
+/* Attempt to simplify a view by merging ajacent ranges */
+static void flatview_simplify(FlatView *view)
+{
+ unsigned i, j;
+
+ i = 0;
+ while (i < view->nr) {
+ j = i + 1;
+ while (j < view->nr
+ && can_merge(&view->ranges[j-1], &view->ranges[j])) {
+ view->ranges[i].addr.size += view->ranges[j].addr.size;
+ ++j;
+ }
+ ++i;
+ memmove(&view->ranges[i], &view->ranges[j],
+ (view->nr - j) * sizeof(view->ranges[j]));
+ view->nr -= j - i;
+ }
+}
+
/* Render a memory region into the global view. Ranges in @view obscure
* ranges in @mr.
*/
@@ -209,6 +237,7 @@ static FlatView generate_memory_topology(MemoryRegion *mr)
flatview_init(&view);
render_memory_region(&view, mr, 0, addrrange_make(0, UINT64_MAX));
+ flatview_simplify(&view);
return view;
}