From ed64d92beac1070eaa632f0213a738754c4b8a74 Mon Sep 17 00:00:00 2001 From: Laine Stump Date: Fri, 6 Nov 2015 13:01:29 -0500 Subject: [PATCH] conf: add virDomainDefAddController() We need a virDomainDefAddController() that doesn't check for an existing controller at the same index (since USB2 controllers must be added in sets of 4 that are all at the same index), so rather than duplicating the code in virDomainDefMaybeAddController(), split it into two functions, in the process eliminating existing duplicated code that loops through the controller list by calling virDomainControllerFind(), which does the same thing). --- src/conf/domain_conf.c | 65 ++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index a9b4249602..56d596531e 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1,7 +1,7 @@ /* * domain_conf.c: domain XML processing * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2016 Red Hat, Inc. * Copyright (C) 2006-2008 Daniel P. Berrange * Copyright (c) 2015 SUSE LINUX Products GmbH, Nuernberg, Germany. * @@ -13336,6 +13336,18 @@ virDomainControllerFind(virDomainDefPtr def, } +static int +virDomainControllerFindUnusedIndex(virDomainDefPtr def, int type) +{ + int idx = 0; + + while (virDomainControllerFind(def, type, idx) >= 0) + idx++; + + return idx; +} + + const char * virDomainControllerAliasFind(virDomainDefPtr def, int type, int idx) @@ -14255,33 +14267,44 @@ virDomainEmulatorPinDefParseXML(xmlNodePtr node) } +static virDomainControllerDefPtr +virDomainDefAddController(virDomainDefPtr def, int type, int idx, int model) +{ + virDomainControllerDefPtr cont; + + if (!(cont = virDomainControllerDefNew(type))) + return NULL; + + if (idx < 0) + idx = virDomainControllerFindUnusedIndex(def, type); + + cont->idx = idx; + cont->model = model; + + if (VIR_APPEND_ELEMENT_COPY(def->controllers, def->ncontrollers, cont) < 0) { + VIR_FREE(cont); + return NULL; + } + + return cont; +} + + int virDomainDefMaybeAddController(virDomainDefPtr def, int type, int idx, int model) { - size_t i; - virDomainControllerDefPtr cont; + /* skip if a specific index was given and it is already + * in use for that type of controller + */ + if (idx >= 0 && virDomainControllerFind(def, type, idx) >= 0) + return 0; - for (i = 0; i < def->ncontrollers; i++) { - if (def->controllers[i]->type == type && - def->controllers[i]->idx == idx) - return 0; - } - - if (!(cont = virDomainControllerDefNew(type))) - return -1; - - cont->idx = idx; - cont->model = model; - - if (VIR_APPEND_ELEMENT(def->controllers, def->ncontrollers, cont) < 0) { - VIR_FREE(cont); - return -1; - } - - return 1; + if (virDomainDefAddController(def, type, idx, model)) + return 1; + return -1; }