diff mbox

[v4,11/29] hostmem: separate allocation from UserCreatable complete method

Message ID 452eb5fd62731cfaa9d5241f019551e912900750.1402299637.git.hutao@cn.fujitsu.com
State New
Headers show

Commit Message

Hu Tao June 9, 2014, 10:25 a.m. UTC
This allows the superclass to set various policies on the memory
region that the subclass creates. Drops hostmem-ram's complete method
accordingly.

While at file hostmem.c, s/hostmemory/host_memory/ to keep names
consistant.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
---
 backends/hostmem-ram.c   |  7 +++----
 backends/hostmem.c       | 40 ++++++++++++++++++++++++++++++----------
 include/sysemu/hostmem.h |  2 ++
 3 files changed, 35 insertions(+), 14 deletions(-)

Comments

Igor Mammedov June 9, 2014, 10:47 a.m. UTC | #1
On Mon, 9 Jun 2014 18:25:16 +0800
Hu Tao <hutao@cn.fujitsu.com> wrote:

> This allows the superclass to set various policies on the memory
> region that the subclass creates. Drops hostmem-ram's complete method
> accordingly.
> 
> While at file hostmem.c, s/hostmemory/host_memory/ to keep names
> consistant.
nitpick, it would be better to split rename s/hostmemory/host_memory/ in
separate patch

> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
> ---
>  backends/hostmem-ram.c   |  7 +++----
>  backends/hostmem.c       | 40 ++++++++++++++++++++++++++++++----------
>  include/sysemu/hostmem.h |  2 ++
>  3 files changed, 35 insertions(+), 14 deletions(-)
> 
> diff --git a/backends/hostmem-ram.c b/backends/hostmem-ram.c
> index bba2ebc..d9a8290 100644
> --- a/backends/hostmem-ram.c
> +++ b/backends/hostmem-ram.c
> @@ -16,9 +16,8 @@
>  
>  
>  static void
> -ram_backend_memory_init(UserCreatable *uc, Error **errp)
> +ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
>  {
> -    HostMemoryBackend *backend = MEMORY_BACKEND(uc);
>      char *path;
>  
>      if (!backend->size) {
> @@ -35,9 +34,9 @@ ram_backend_memory_init(UserCreatable *uc, Error **errp)
>  static void
>  ram_backend_class_init(ObjectClass *oc, void *data)
>  {
> -    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
> +    HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
>  
> -    ucc->complete = ram_backend_memory_init;
> +    bc->alloc = ram_backend_memory_alloc;
>  }
>  
>  static const TypeInfo ram_backend_info = {
> diff --git a/backends/hostmem.c b/backends/hostmem.c
> index 2f578ac..cc57c13 100644
> --- a/backends/hostmem.c
> +++ b/backends/hostmem.c
> @@ -17,7 +17,7 @@
>  #include "qom/object_interfaces.h"
>  
>  static void
> -hostmemory_backend_get_size(Object *obj, Visitor *v, void *opaque,
> +host_memory_backend_get_size(Object *obj, Visitor *v, void *opaque,
>                              const char *name, Error **errp)
>  {
>      HostMemoryBackend *backend = MEMORY_BACKEND(obj);
> @@ -27,7 +27,7 @@ hostmemory_backend_get_size(Object *obj, Visitor *v, void *opaque,
>  }
>  
>  static void
> -hostmemory_backend_set_size(Object *obj, Visitor *v, void *opaque,
> +host_memory_backend_set_size(Object *obj, Visitor *v, void *opaque,
>                              const char *name, Error **errp)
>  {
>      HostMemoryBackend *backend = MEMORY_BACKEND(obj);
> @@ -53,14 +53,14 @@ out:
>      error_propagate(errp, local_err);
>  }
>  
> -static void hostmemory_backend_init(Object *obj)
> +static void host_memory_backend_init(Object *obj)
>  {
>      object_property_add(obj, "size", "int",
> -                        hostmemory_backend_get_size,
> -                        hostmemory_backend_set_size, NULL, NULL, NULL);
> +                        host_memory_backend_get_size,
> +                        host_memory_backend_set_size, NULL, NULL, NULL);
>  }
>  
> -static void hostmemory_backend_finalize(Object *obj)
> +static void host_memory_backend_finalize(Object *obj)
>  {
>      HostMemoryBackend *backend = MEMORY_BACKEND(obj);
>  
> @@ -75,14 +75,34 @@ host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
>      return memory_region_size(&backend->mr) ? &backend->mr : NULL;
>  }
>  
> -static const TypeInfo hostmemory_backend_info = {
> +static void
> +host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
> +{
> +    HostMemoryBackend *backend = MEMORY_BACKEND(uc);
> +    HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
> +
> +    if (bc->alloc) {
> +        bc->alloc(backend, errp);
> +    }
> +}
> +
> +static void
> +host_memory_backend_class_init(ObjectClass *oc, void *data)
> +{
> +    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
> +
> +    ucc->complete = host_memory_backend_memory_complete;
> +}
> +
> +static const TypeInfo host_memory_backend_info = {
>      .name = TYPE_MEMORY_BACKEND,
>      .parent = TYPE_OBJECT,
>      .abstract = true,
>      .class_size = sizeof(HostMemoryBackendClass),
> +    .class_init = host_memory_backend_class_init,
>      .instance_size = sizeof(HostMemoryBackend),
> -    .instance_init = hostmemory_backend_init,
> -    .instance_finalize = hostmemory_backend_finalize,
> +    .instance_init = host_memory_backend_init,
> +    .instance_finalize = host_memory_backend_finalize,
>      .interfaces = (InterfaceInfo[]) {
>          { TYPE_USER_CREATABLE },
>          { }
> @@ -91,7 +111,7 @@ static const TypeInfo hostmemory_backend_info = {
>  
>  static void register_types(void)
>  {
> -    type_register_static(&hostmemory_backend_info);
> +    type_register_static(&host_memory_backend_info);
>  }
>  
>  type_init(register_types);
> diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h
> index 4fc081e..923f672 100644
> --- a/include/sysemu/hostmem.h
> +++ b/include/sysemu/hostmem.h
> @@ -34,6 +34,8 @@ typedef struct HostMemoryBackendClass HostMemoryBackendClass;
>   */
>  struct HostMemoryBackendClass {
>      ObjectClass parent_class;
> +
> +    void (*alloc)(HostMemoryBackend *backend, Error **errp);
>  };
>  
>  /**
> -- 
> 1.9.3
>
Hu Tao June 10, 2014, 1:55 a.m. UTC | #2
On Mon, Jun 09, 2014 at 12:47:19PM +0200, Igor Mammedov wrote:
> On Mon, 9 Jun 2014 18:25:16 +0800
> Hu Tao <hutao@cn.fujitsu.com> wrote:
> 
> > This allows the superclass to set various policies on the memory
> > region that the subclass creates. Drops hostmem-ram's complete method
> > accordingly.
> > 
> > While at file hostmem.c, s/hostmemory/host_memory/ to keep names
> > consistant.
> nitpick, it would be better to split rename s/hostmemory/host_memory/ in
> separate patch

OK.

Hu
diff mbox

Patch

diff --git a/backends/hostmem-ram.c b/backends/hostmem-ram.c
index bba2ebc..d9a8290 100644
--- a/backends/hostmem-ram.c
+++ b/backends/hostmem-ram.c
@@ -16,9 +16,8 @@ 
 
 
 static void
-ram_backend_memory_init(UserCreatable *uc, Error **errp)
+ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 {
-    HostMemoryBackend *backend = MEMORY_BACKEND(uc);
     char *path;
 
     if (!backend->size) {
@@ -35,9 +34,9 @@  ram_backend_memory_init(UserCreatable *uc, Error **errp)
 static void
 ram_backend_class_init(ObjectClass *oc, void *data)
 {
-    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+    HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
 
-    ucc->complete = ram_backend_memory_init;
+    bc->alloc = ram_backend_memory_alloc;
 }
 
 static const TypeInfo ram_backend_info = {
diff --git a/backends/hostmem.c b/backends/hostmem.c
index 2f578ac..cc57c13 100644
--- a/backends/hostmem.c
+++ b/backends/hostmem.c
@@ -17,7 +17,7 @@ 
 #include "qom/object_interfaces.h"
 
 static void
-hostmemory_backend_get_size(Object *obj, Visitor *v, void *opaque,
+host_memory_backend_get_size(Object *obj, Visitor *v, void *opaque,
                             const char *name, Error **errp)
 {
     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
@@ -27,7 +27,7 @@  hostmemory_backend_get_size(Object *obj, Visitor *v, void *opaque,
 }
 
 static void
-hostmemory_backend_set_size(Object *obj, Visitor *v, void *opaque,
+host_memory_backend_set_size(Object *obj, Visitor *v, void *opaque,
                             const char *name, Error **errp)
 {
     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
@@ -53,14 +53,14 @@  out:
     error_propagate(errp, local_err);
 }
 
-static void hostmemory_backend_init(Object *obj)
+static void host_memory_backend_init(Object *obj)
 {
     object_property_add(obj, "size", "int",
-                        hostmemory_backend_get_size,
-                        hostmemory_backend_set_size, NULL, NULL, NULL);
+                        host_memory_backend_get_size,
+                        host_memory_backend_set_size, NULL, NULL, NULL);
 }
 
-static void hostmemory_backend_finalize(Object *obj)
+static void host_memory_backend_finalize(Object *obj)
 {
     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 
@@ -75,14 +75,34 @@  host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
     return memory_region_size(&backend->mr) ? &backend->mr : NULL;
 }
 
-static const TypeInfo hostmemory_backend_info = {
+static void
+host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
+{
+    HostMemoryBackend *backend = MEMORY_BACKEND(uc);
+    HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
+
+    if (bc->alloc) {
+        bc->alloc(backend, errp);
+    }
+}
+
+static void
+host_memory_backend_class_init(ObjectClass *oc, void *data)
+{
+    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+
+    ucc->complete = host_memory_backend_memory_complete;
+}
+
+static const TypeInfo host_memory_backend_info = {
     .name = TYPE_MEMORY_BACKEND,
     .parent = TYPE_OBJECT,
     .abstract = true,
     .class_size = sizeof(HostMemoryBackendClass),
+    .class_init = host_memory_backend_class_init,
     .instance_size = sizeof(HostMemoryBackend),
-    .instance_init = hostmemory_backend_init,
-    .instance_finalize = hostmemory_backend_finalize,
+    .instance_init = host_memory_backend_init,
+    .instance_finalize = host_memory_backend_finalize,
     .interfaces = (InterfaceInfo[]) {
         { TYPE_USER_CREATABLE },
         { }
@@ -91,7 +111,7 @@  static const TypeInfo hostmemory_backend_info = {
 
 static void register_types(void)
 {
-    type_register_static(&hostmemory_backend_info);
+    type_register_static(&host_memory_backend_info);
 }
 
 type_init(register_types);
diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h
index 4fc081e..923f672 100644
--- a/include/sysemu/hostmem.h
+++ b/include/sysemu/hostmem.h
@@ -34,6 +34,8 @@  typedef struct HostMemoryBackendClass HostMemoryBackendClass;
  */
 struct HostMemoryBackendClass {
     ObjectClass parent_class;
+
+    void (*alloc)(HostMemoryBackend *backend, Error **errp);
 };
 
 /**