hostmem: separate allocation from UserCreatable complete method

This allows the superclass to set various policies on the memory
region that the subclass creates. Drops hostmem-ram's complete method
accordingly.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
This commit is contained in:
Hu Tao 2014-06-10 19:15:19 +08:00 committed by Michael S. Tsirkin
parent 58f4662c6c
commit bd9262d95f
3 changed files with 25 additions and 4 deletions

View File

@ -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 = {

View File

@ -75,11 +75,31 @@ host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
return memory_region_size(&backend->mr) ? &backend->mr : NULL;
}
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 = host_memory_backend_init,
.instance_finalize = host_memory_backend_finalize,

View File

@ -34,6 +34,8 @@ typedef struct HostMemoryBackendClass HostMemoryBackendClass;
*/
struct HostMemoryBackendClass {
ObjectClass parent_class;
void (*alloc)(HostMemoryBackend *backend, Error **errp);
};
/**