From a6ff7989662d659aed0888670e77e68cb8c9bd81 Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Thu, 24 Sep 2020 17:27:02 +0200 Subject: block/export: Allocate BlockExport in blk_exp_add() Instead of letting the driver allocate and return the BlockExport object, allocate it already in blk_exp_add() and pass it. This allows us to initialise the generic part before calling into the driver so that the driver can just use these values instead of having to parse the options a second time. For symmetry, move freeing the BlockExport to blk_exp_unref(). Signed-off-by: Kevin Wolf Reviewed-by: Max Reitz Message-Id: <20200924152717.287415-17-kwolf@redhat.com> Acked-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf --- block/export/export.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'block/export') diff --git a/block/export/export.c b/block/export/export.c index 8635318ef1..6b2b29078b 100644 --- a/block/export/export.c +++ b/block/export/export.c @@ -39,6 +39,8 @@ static const BlockExportDriver *blk_exp_find_driver(BlockExportType type) BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) { const BlockExportDriver *drv; + BlockExport *exp; + int ret; drv = blk_exp_find_driver(export->type); if (!drv) { @@ -46,7 +48,20 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) return NULL; } - return drv->create(export, errp); + assert(drv->instance_size >= sizeof(BlockExport)); + exp = g_malloc0(drv->instance_size); + *exp = (BlockExport) { + .drv = drv, + .refcount = 1, + }; + + ret = drv->create(exp, export, errp); + if (ret < 0) { + g_free(exp); + return NULL; + } + + return exp; } /* Callers must hold exp->ctx lock */ @@ -62,6 +77,7 @@ void blk_exp_unref(BlockExport *exp) assert(exp->refcount > 0); if (--exp->refcount == 0) { exp->drv->delete(exp); + g_free(exp); } } -- cgit v1.2.3-55-g7522