This allows the superclass to set various policies on the memory region that the subclass creates.
Suggested-by: Igor Mammedov <imamm...@redhat.com> Signed-off-by: Paolo Bonzini <pbonz...@redhat.com> --- backends/hostmem-file.c | 9 ++++----- backends/hostmem-ram.c | 8 +++----- backends/hostmem.c | 12 ++++++++++-- include/sysemu/hostmem.h | 2 ++ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index 8c6ea5d..7e91665 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -30,10 +30,9 @@ struct HostMemoryBackendFile { }; static void -file_backend_memory_init(UserCreatable *uc, Error **errp) +file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) { - HostMemoryBackend *backend = MEMORY_BACKEND(uc); - HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(uc); + HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend); if (!backend->size) { error_setg(errp, "can't create backend with size 0"); @@ -58,9 +57,9 @@ file_backend_memory_init(UserCreatable *uc, Error **errp) static void file_backend_class_init(ObjectClass *oc, void *data) { - UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); + HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); - ucc->complete = file_backend_memory_init; + bc->alloc = file_backend_memory_alloc; } static char *get_mem_path(Object *o, Error **errp) diff --git a/backends/hostmem-ram.c b/backends/hostmem-ram.c index ce06fbe..e4d244a 100644 --- a/backends/hostmem-ram.c +++ b/backends/hostmem-ram.c @@ -16,10 +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); - if (!backend->size) { error_setg(errp, "can't create backend with size 0"); return; @@ -33,9 +31,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 06817dd..7d6199f 100644 --- a/backends/hostmem.c +++ b/backends/hostmem.c @@ -69,8 +69,16 @@ static void host_memory_backend_finalize(Object *obj) static void host_memory_backend_memory_init(UserCreatable *uc, Error **errp) { - error_setg(errp, "memory_init is not implemented for type [%s]", - object_get_typename(OBJECT(uc))); + HostMemoryBackend *backend = MEMORY_BACKEND(uc); + HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc); + + if (!bc->alloc) { + error_setg(errp, "memory_init is not implemented for type [%s]", + object_get_typename(OBJECT(uc))); + return; + } + + bc->alloc(backend, errp); } MemoryRegion * diff --git a/include/sysemu/hostmem.h b/include/sysemu/hostmem.h index bc3ffb3..4738107 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.8.5.3