diff options
author | Peter Crosthwaite <peter.crosthwaite@xilinx.com> | 2014-06-05 23:15:52 -0700 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2014-07-01 10:20:41 +0200 |
commit | b4fefef9d52003b6d09866501275a9a57995c6b0 (patch) | |
tree | 6d9c67e1e535afd09aa7d37609f4871d0ce3ec8b /memory.c | |
parent | b5c2c3d0c81ea97ac8443113b9a7a0c0ce25368e (diff) |
memory: MemoryRegion: QOMify
QOMify memory regions as an Object. The former init() and destroy()
routines become instance_init() and instance_finalize() resp.
memory_region_init() is re-implemented to be:
object_initialize() + set fields
memory_region_destroy() is re-implemented to call unparent().
Signed-off-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
[Add newly-created MR as child, unparent on destruction. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'memory.c')
-rw-r--r-- | memory.c | 121 |
1 files changed, 101 insertions, 20 deletions
@@ -842,40 +842,94 @@ static void memory_region_destructor_rom_device(MemoryRegion *mr) qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK); } +static bool memory_region_need_escape(char c) +{ + return c == '/' || c == '[' || c == '\\' || c == ']'; +} + +static char *memory_region_escape_name(const char *name) +{ + const char *p; + char *escaped, *q; + uint8_t c; + size_t bytes = 0; + + for (p = name; *p; p++) { + bytes += memory_region_need_escape(*p) ? 4 : 1; + } + if (bytes == p - name) { + return g_memdup(name, bytes + 1); + } + + escaped = g_malloc(bytes + 1); + for (p = name, q = escaped; *p; p++) { + c = *p; + if (unlikely(memory_region_need_escape(c))) { + *q++ = '\\'; + *q++ = 'x'; + *q++ = "0123456789abcdef"[c >> 4]; + c = "0123456789abcdef"[c & 15]; + } + *q++ = c; + } + *q = 0; + return escaped; +} + +static void object_property_add_child_array(Object *owner, + const char *name, + Object *child) +{ + int i; + char *base_name = memory_region_escape_name(name); + + for (i = 0; ; i++) { + char *full_name = g_strdup_printf("%s[%d]", base_name, i); + Error *local_err = NULL; + + object_property_add_child(owner, full_name, child, &local_err); + g_free(full_name); + if (!local_err) { + break; + } + + error_free(local_err); + } + + g_free(base_name); +} + + void memory_region_init(MemoryRegion *mr, Object *owner, const char *name, uint64_t size) { - mr->ops = &unassigned_mem_ops; - mr->opaque = NULL; + object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION); + mr->owner = owner ? owner : qdev_get_machine(); - mr->iommu_ops = NULL; - mr->container = NULL; mr->size = int128_make64(size); if (size == UINT64_MAX) { mr->size = int128_2_64(); } - mr->addr = 0; - mr->subpage = false; + mr->name = g_strdup(name); + + if (name) { + object_property_add_child_array(mr->owner, name, OBJECT(mr)); + object_unref(OBJECT(mr)); + } +} + +static void memory_region_initfn(Object *obj) +{ + MemoryRegion *mr = MEMORY_REGION(obj); + + mr->ops = &unassigned_mem_ops; mr->enabled = true; - mr->terminates = false; - mr->ram = false; mr->romd_mode = 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; - mr->flush_coalesced_mmio = false; } static uint64_t unassigned_mem_read(void *opaque, hwaddr addr, @@ -1113,8 +1167,10 @@ void memory_region_init_reservation(MemoryRegion *mr, memory_region_init_io(mr, owner, &unassigned_mem_ops, mr, name, size); } -void memory_region_destroy(MemoryRegion *mr) +static void memory_region_finalize(Object *obj) { + MemoryRegion *mr = MEMORY_REGION(obj); + assert(QTAILQ_EMPTY(&mr->subregions)); assert(memory_region_transaction_depth == 0); mr->destructor(mr); @@ -1123,6 +1179,12 @@ void memory_region_destroy(MemoryRegion *mr) g_free(mr->ioeventfds); } +void memory_region_destroy(MemoryRegion *mr) +{ + object_unparent(OBJECT(mr)); +} + + Object *memory_region_owner(MemoryRegion *mr) { return mr->owner; @@ -1132,6 +1194,8 @@ void memory_region_ref(MemoryRegion *mr) { if (mr && mr->owner) { object_ref(mr->owner); + } else { + object_ref(OBJECT(mr)); } } @@ -1139,6 +1203,8 @@ void memory_region_unref(MemoryRegion *mr) { if (mr && mr->owner) { object_unref(mr->owner); + } else { + object_unref(OBJECT(mr)); } } @@ -1946,3 +2012,18 @@ void mtree_info(fprintf_function mon_printf, void *f) g_free(ml); } } + +static const TypeInfo memory_region_info = { + .parent = TYPE_OBJECT, + .name = TYPE_MEMORY_REGION, + .instance_size = sizeof(MemoryRegion), + .instance_init = memory_region_initfn, + .instance_finalize = memory_region_finalize, +}; + +static void memory_register_types(void) +{ + type_register_static(&memory_region_info); +} + +type_init(memory_register_types) |