aboutsummaryrefslogtreecommitdiff
path: root/block
diff options
context:
space:
mode:
authorKevin Wolf <kwolf@redhat.com>2020-09-24 17:27:02 +0200
committerKevin Wolf <kwolf@redhat.com>2020-10-02 15:46:40 +0200
commita6ff7989662d659aed0888670e77e68cb8c9bd81 (patch)
tree67af6fa4a55dce3eb78a40f53433ffc49a38e59b /block
parentb6076afcabc7a3947d0b212b956f4575e6795c56 (diff)
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 <kwolf@redhat.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Message-Id: <20200924152717.287415-17-kwolf@redhat.com> Acked-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'block')
-rw-r--r--block/export/export.c18
1 files changed, 17 insertions, 1 deletions
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);
}
}