Some drivers use def-specific private data across callbacks (e.g. qemuCaps in the qemu driver). Currently it's mostly allocated in every single callback. This is rather wasteful, given that every single call to the device callback allocates it. The new callback will allocate the data (if not provided externally) and then use it for the VM, address and device post parse callbacks. --- src/conf/domain_conf.c | 63 ++++++++++++++++++++++++++++++++++++++++++-------- src/conf/domain_conf.h | 9 ++++++++ 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 7c5e6b95a..298fe9b4e 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -4672,6 +4672,31 @@ virDomainDeviceDefPostParse(virDomainDeviceDefPtr dev, return 0; } +static int +virDomainDeviceDefPostParseOne(virDomainDeviceDefPtr dev, + const virDomainDef *def, + virCapsPtr caps, + unsigned int flags, + virDomainXMLOptionPtr xmlopt) +{ + void *parseOpaque = NULL; + int ret; + + if (xmlopt->config.domainPostParseDataAlloc) { + if (xmlopt->config.domainPostParseDataAlloc(def, caps, flags, + xmlopt->config.priv, + &parseOpaque) < 0) + return -1; + } + + ret = virDomainDeviceDefPostParse(dev, def, caps, flags, xmlopt, parseOpaque); + + if (parseOpaque && xmlopt->config.domainPostParseDataFree) + xmlopt->config.domainPostParseDataFree(parseOpaque); + + return ret; +} + struct virDomainDefPostParseDeviceIteratorData { virCapsPtr caps; @@ -4818,6 +4843,7 @@ virDomainDefPostParse(virDomainDefPtr def, void *parseOpaque) { int ret; + bool localParseOpaque = false; struct virDomainDefPostParseDeviceIteratorData data = { .caps = caps, .xmlopt = xmlopt, @@ -4834,6 +4860,17 @@ virDomainDefPostParse(virDomainDefPtr def, return ret; } + if (!data.parseOpaque && + xmlopt->config.domainPostParseDataAlloc) { + ret = xmlopt->config.domainPostParseDataAlloc(def, caps, parseFlags, + xmlopt->config.priv, + &data.parseOpaque); + + if (ret < 0) + return ret; + localParseOpaque = true; + } + /* this must be done before the hypervisor-specific callback, * in case presence of a controller at a specific index is checked */ @@ -4843,9 +4880,9 @@ virDomainDefPostParse(virDomainDefPtr def, if (xmlopt->config.domainPostParseCallback) { ret = xmlopt->config.domainPostParseCallback(def, caps, parseFlags, xmlopt->config.priv, - parseOpaque); + data.parseOpaque); if (ret < 0) - return ret; + goto cleanup; } /* iterate the devices */ @@ -4853,24 +4890,30 @@ virDomainDefPostParse(virDomainDefPtr def, virDomainDefPostParseDeviceIterator, true, &data)) < 0) - return ret; + goto cleanup; if ((ret = virDomainDefPostParseInternal(def, &data)) < 0) - return ret; + goto cleanup; if (xmlopt->config.assignAddressesCallback) { ret = xmlopt->config.assignAddressesCallback(def, caps, parseFlags, xmlopt->config.priv, - parseOpaque); + data.parseOpaque); if (ret < 0) - return ret; + goto cleanup; } - if (virDomainDefPostParseCheckFeatures(def, xmlopt) < 0) - return -1; + if ((ret = virDomainDefPostParseCheckFeatures(def, xmlopt)) < 0) + goto cleanup; - return 0; + ret = 0; + + cleanup: + if (localParseOpaque && xmlopt->config.domainPostParseDataFree) + xmlopt->config.domainPostParseDataFree(data.parseOpaque); + + return ret; } @@ -14653,7 +14696,7 @@ virDomainDeviceDefParse(const char *xmlStr, } /* callback to fill driver specific device aspects */ - if (virDomainDeviceDefPostParse(dev, def, caps, flags, xmlopt, NULL) < 0) + if (virDomainDeviceDefPostParseOne(dev, def, caps, flags, xmlopt) < 0) goto error; /* validate the configuration */ diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 4daf024ea..be7298137 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -2538,6 +2538,13 @@ typedef int (*virDomainDefAssignAddressesCallback)(virDomainDef *def, void *opaque, void *parseOpaque); +typedef int (*virDomainDefPostParseDataAlloc)(const virDomainDef *def, + virCapsPtr caps, + unsigned int parseFlags, + void *opaque, + void **parseOpaque); +typedef void (*virDomainDefPostParseDataFree)(void *parseOpaque); + /* Called in appropriate places where the domain conf parser can return failure * for configurations that were previously accepted. This shall not modify the * config. */ @@ -2556,9 +2563,11 @@ typedef virDomainDefParserConfig *virDomainDefParserConfigPtr; struct _virDomainDefParserConfig { /* driver domain definition callbacks */ virDomainDefPostParseBasicCallback domainPostParseBasicCallback; + virDomainDefPostParseDataAlloc domainPostParseDataAlloc; virDomainDefPostParseCallback domainPostParseCallback; virDomainDeviceDefPostParseCallback devicesPostParseCallback; virDomainDefAssignAddressesCallback assignAddressesCallback; + virDomainDefPostParseDataFree domainPostParseDataFree; /* validation callbacks */ virDomainDefValidateCallback domainValidateCallback; -- 2.14.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list