Patchwork [v4,2/5] convert MemoryRegion to QOM

login
register
mail settings
Submitter Wanpeng Li
Date July 18, 2012, 1:19 p.m.
Message ID <1342617545-9261-3-git-send-email-liwanp@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/171671/
State New
Headers show

Comments

Wanpeng Li - July 18, 2012, 1:19 p.m.
[CCing ML]

From: Anthony Liguori <aliguori@us.ibm.com>

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Wanpeng Li <liwanp@linux.vnet.ibm.com>
---
 memory.c |   94 ++++++++++++++++++++++++++++++++++++++++++++++----------------
 memory.h |    8 +++++
 2 files changed, 78 insertions(+), 24 deletions(-)

Patch

diff --git a/memory.c b/memory.c
index aab4a31..3674535 100644
--- a/memory.c
+++ b/memory.c
@@ -797,35 +797,26 @@  static bool memory_region_wrong_endianness(MemoryRegion *mr)
 #endif
 }
 
-void memory_region_init(MemoryRegion *mr,
-                        const char *name,
-                        uint64_t size)
+void memory_region_set_name(MemoryRegion *mr, const char *name)
+{
+    mr->name = g_strdup(name);
+}
+
+void memory_region_set_size(MemoryRegion *mr, uint64_t size)
 {
-    mr->ops = NULL;
-    mr->parent = NULL;
     mr->size = int128_make64(size);
     if (size == UINT64_MAX) {
         mr->size = int128_2_64();
     }
-    mr->addr = 0;
-    mr->subpage = false;
-    mr->enabled = true;
-    mr->terminates = false;
-    mr->ram = false;
-    mr->readable = true;
-    mr->readonly = false;
-    mr->rom_device = false;
-    mr->destructor = memory_region_destructor_none;
-    mr->priority = 0;
-    mr->may_overlap = false;
-    mr->alias = NULL;
-    QTAILQ_INIT(&mr->subregions);
-    memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
-    QTAILQ_INIT(&mr->coalesced);
-    mr->name = g_strdup(name);
-    mr->dirty_log_mask = 0;
-    mr->ioeventfd_nb = 0;
-    mr->ioeventfds = NULL;
+}
+
+void memory_region_init(MemoryRegion *mr,
+                        const char *name,
+                        uint64_t size)
+{
+    object_initialize(mr, TYPE_MEMORY_REGION);
+    memory_region_set_name(mr, name);
+    memory_region_set_size(mr, size);
 }
 
 static bool memory_region_access_valid(MemoryRegion *mr,
@@ -1645,3 +1636,58 @@  void mtree_info(fprintf_function mon_printf, void *f)
         g_free(ml);
     }
 }
+
+static void memory_region_initfn(Object *obj)
+{
+    MemoryRegion *mr = MEMORY_REGION(obj);
+    mr->ops = NULL;
+    mr->parent = NULL;
+    mr->size = int128_2_64();
+    mr->addr = 0;
+    mr->subpage = false;
+    mr->enabled = true;
+    mr->terminates = false;
+    mr->ram = false;
+    mr->readable = true;
+    mr->readonly = false;
+    mr->rom_device = false;
+    mr->destructor = memory_region_destructor_none;
+    mr->priority = 0;
+    mr->may_overlap = false;
+    mr->alias = NULL;
+    mr->name = NULL;
+    QTAILQ_INIT(&mr->subregions);
+    memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
+    QTAILQ_INIT(&mr->coalesced);
+    mr->dirty_log_mask = 0;
+    mr->ioeventfd_nb = 0;
+    mr->ioeventfds = NULL;
+}
+
+static void memory_region_finalize(Object *obj)
+{
+    MemoryRegion *mr = MEMORY_REGION(obj);
+
+    assert(QTAILQ_EMPTY(&mr->subregions));
+    mr->destructor(mr);
+    memory_region_clear_coalescing(mr);
+    if (mr->name) {
+        g_free((char *)mr->name);
+    }
+    g_free(mr->ioeventfds);
+}
+
+static TypeInfo memory_region_type = {
+    .name = TYPE_MEMORY_REGION,
+    .parent = TYPE_OBJECT,
+    .instance_size = sizeof(MemoryRegion),
+    .instance_init = memory_region_initfn,
+    .instance_finalize = memory_region_finalize,
+};
+
+static void register_devices(void)
+{
+    type_register_static(&memory_region_type);
+}
+
+type_init(register_devices)
diff --git a/memory.h b/memory.h
index 740c48e..90a53f7 100644
--- a/memory.h
+++ b/memory.h
@@ -25,6 +25,7 @@ 
 #include "iorange.h"
 #include "ioport.h"
 #include "int128.h"
+#include "qemu/object.h"
 
 typedef struct MemoryRegionOps MemoryRegionOps;
 typedef struct MemoryRegion MemoryRegion;
@@ -116,6 +117,9 @@  struct MemoryRegionOps {
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
 
+#define TYPE_MEMORY_REGION "memory-region"
+#define MEMORY_REGION(obj) OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
+
 struct MemoryRegion {
     /* All fields are private - violators will be prosecuted */
     const MemoryRegionOps *ops;
@@ -748,6 +752,10 @@  void memory_global_dirty_log_stop(void);
 
 void mtree_info(fprintf_function mon_printf, void *f);
 
+void memory_region_set_name(MemoryRegion *mr, const char *name);
+
+void memory_region_set_size(MemoryRegion *mr, uint64_t size);
+
 #endif
 
 #endif