interface: Make _virInterfaceObjList struct private

Move the structs into virinterfaceobj.c, create necessary accessors, and
initializers.

This also includes reworking virInterfaceObjListClone to handle receiving
a source interfaces list pointer, creating the destination interfaces object,
and copying everything from source into dest.

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2017-04-14 15:45:27 -04:00
parent 3b6de6c0cb
commit 5374a1ca80
4 changed files with 60 additions and 48 deletions

View File

@ -39,6 +39,10 @@ struct _virInterfaceObj {
virInterfaceDefPtr def; /* The interface definition */ virInterfaceDefPtr def; /* The interface definition */
}; };
struct _virInterfaceObjList {
size_t count;
virInterfaceObjPtr *objs;
};
/* virInterfaceObj manipulation */ /* virInterfaceObj manipulation */
@ -91,6 +95,17 @@ virInterfaceObjSetActive(virInterfaceObjPtr obj,
/* virInterfaceObjList manipulation */ /* virInterfaceObjList manipulation */
virInterfaceObjListPtr
virInterfaceObjListNew(void)
{
virInterfaceObjListPtr interfaces;
if (VIR_ALLOC(interfaces) < 0)
return NULL;
return interfaces;
}
int int
virInterfaceObjFindByMACString(virInterfaceObjListPtr interfaces, virInterfaceObjFindByMACString(virInterfaceObjListPtr interfaces,
const char *mac, const char *mac,
@ -149,50 +164,50 @@ virInterfaceObjListFree(virInterfaceObjListPtr interfaces)
for (i = 0; i < interfaces->count; i++) for (i = 0; i < interfaces->count; i++)
virInterfaceObjFree(interfaces->objs[i]); virInterfaceObjFree(interfaces->objs[i]);
VIR_FREE(interfaces->objs); VIR_FREE(interfaces->objs);
interfaces->count = 0; VIR_FREE(interfaces);
} }
int virInterfaceObjListPtr
virInterfaceObjListClone(virInterfaceObjListPtr src, virInterfaceObjListClone(virInterfaceObjListPtr interfaces)
virInterfaceObjListPtr dest)
{ {
int ret = -1;
size_t i; size_t i;
unsigned int cnt; unsigned int cnt;
virInterfaceObjListPtr dest;
if (!src || !dest) if (!interfaces)
goto cleanup; return NULL;
virInterfaceObjListFree(dest); /* start with an empty list */ if (!(dest = virInterfaceObjListNew()))
cnt = src->count; return NULL;
cnt = interfaces->count;
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; i++) {
virInterfaceObjPtr srcobj = src->objs[i]; virInterfaceObjPtr srcobj = interfaces->objs[i];
virInterfaceDefPtr backup; virInterfaceDefPtr backup;
virInterfaceObjPtr obj; virInterfaceObjPtr obj;
char *xml = virInterfaceDefFormat(srcobj->def); char *xml = virInterfaceDefFormat(srcobj->def);
if (!xml) if (!xml)
goto cleanup; goto error;
if ((backup = virInterfaceDefParseString(xml)) == NULL) { if (!(backup = virInterfaceDefParseString(xml))) {
VIR_FREE(xml); VIR_FREE(xml);
goto cleanup; goto error;
} }
VIR_FREE(xml); VIR_FREE(xml);
if ((obj = virInterfaceObjAssignDef(dest, backup)) == NULL) if (!(obj = virInterfaceObjAssignDef(dest, backup)))
goto cleanup; goto error;
virInterfaceObjUnlock(obj); /* locked by virInterfaceObjAssignDef */ virInterfaceObjUnlock(obj); /* locked by virInterfaceObjAssignDef */
} }
ret = cnt; return dest;
cleanup:
if ((ret < 0) && dest) error:
virInterfaceObjListFree(dest); virInterfaceObjListFree(dest);
return ret; return NULL;
} }

View File

@ -27,10 +27,6 @@ typedef virInterfaceObj *virInterfaceObjPtr;
typedef struct _virInterfaceObjList virInterfaceObjList; typedef struct _virInterfaceObjList virInterfaceObjList;
typedef virInterfaceObjList *virInterfaceObjListPtr; typedef virInterfaceObjList *virInterfaceObjListPtr;
struct _virInterfaceObjList {
size_t count;
virInterfaceObjPtr *objs;
};
virInterfaceDefPtr virInterfaceDefPtr
virInterfaceObjGetDef(virInterfaceObjPtr obj); virInterfaceObjGetDef(virInterfaceObjPtr obj);
@ -42,6 +38,9 @@ void
virInterfaceObjSetActive(virInterfaceObjPtr obj, virInterfaceObjSetActive(virInterfaceObjPtr obj,
bool active); bool active);
virInterfaceObjListPtr
virInterfaceObjListNew(void);
int int
virInterfaceObjFindByMACString(virInterfaceObjListPtr interfaces, virInterfaceObjFindByMACString(virInterfaceObjListPtr interfaces,
const char *mac, const char *mac,
@ -57,9 +56,8 @@ virInterfaceObjFree(virInterfaceObjPtr obj);
void void
virInterfaceObjListFree(virInterfaceObjListPtr vms); virInterfaceObjListFree(virInterfaceObjListPtr vms);
int virInterfaceObjListPtr
virInterfaceObjListClone(virInterfaceObjListPtr src, virInterfaceObjListClone(virInterfaceObjListPtr interfaces);
virInterfaceObjListPtr dest);
virInterfaceObjPtr virInterfaceObjPtr
virInterfaceObjAssignDef(virInterfaceObjListPtr interfaces, virInterfaceObjAssignDef(virInterfaceObjListPtr interfaces,

View File

@ -918,6 +918,7 @@ virInterfaceObjGetNames;
virInterfaceObjIsActive; virInterfaceObjIsActive;
virInterfaceObjListClone; virInterfaceObjListClone;
virInterfaceObjListFree; virInterfaceObjListFree;
virInterfaceObjListNew;
virInterfaceObjLock; virInterfaceObjLock;
virInterfaceObjNumOfInterfaces; virInterfaceObjNumOfInterfaces;
virInterfaceObjRemove; virInterfaceObjRemove;

View File

@ -97,9 +97,9 @@ struct _testDriver {
virMutex lock; virMutex lock;
virNodeInfo nodeInfo; virNodeInfo nodeInfo;
virInterfaceObjList ifaces; virInterfaceObjListPtr ifaces;
bool transaction_running; bool transaction_running;
virInterfaceObjList backupIfaces; virInterfaceObjListPtr backupIfaces;
virStoragePoolObjList pools; virStoragePoolObjList pools;
virNodeDeviceObjList devs; virNodeDeviceObjList devs;
int numCells; int numCells;
@ -154,7 +154,7 @@ testDriverFree(testDriverPtr driver)
virObjectUnref(driver->domains); virObjectUnref(driver->domains);
virNodeDeviceObjListFree(&driver->devs); virNodeDeviceObjListFree(&driver->devs);
virObjectUnref(driver->networks); virObjectUnref(driver->networks);
virInterfaceObjListFree(&driver->ifaces); virInterfaceObjListFree(driver->ifaces);
virStoragePoolObjListFree(&driver->pools); virStoragePoolObjListFree(&driver->pools);
virObjectUnref(driver->eventState); virObjectUnref(driver->eventState);
virMutexUnlock(&driver->lock); virMutexUnlock(&driver->lock);
@ -416,6 +416,7 @@ testDriverNew(void)
if (!(ret->xmlopt = virDomainXMLOptionNew(NULL, NULL, &ns)) || if (!(ret->xmlopt = virDomainXMLOptionNew(NULL, NULL, &ns)) ||
!(ret->eventState = virObjectEventStateNew()) || !(ret->eventState = virObjectEventStateNew()) ||
!(ret->ifaces = virInterfaceObjListNew()) ||
!(ret->domains = virDomainObjListNew()) || !(ret->domains = virDomainObjListNew()) ||
!(ret->networks = virNetworkObjListNew())) !(ret->networks = virNetworkObjListNew()))
goto error; goto error;
@ -1020,7 +1021,7 @@ testParseInterfaces(testDriverPtr privconn,
if (!def) if (!def)
goto error; goto error;
if (!(obj = virInterfaceObjAssignDef(&privconn->ifaces, def))) { if (!(obj = virInterfaceObjAssignDef(privconn->ifaces, def))) {
virInterfaceDefFree(def); virInterfaceDefFree(def);
goto error; goto error;
} }
@ -3630,7 +3631,7 @@ testInterfaceObjFindByName(testDriverPtr privconn,
virInterfaceObjPtr obj; virInterfaceObjPtr obj;
testDriverLock(privconn); testDriverLock(privconn);
obj = virInterfaceObjFindByName(&privconn->ifaces, name); obj = virInterfaceObjFindByName(privconn->ifaces, name);
testDriverUnlock(privconn); testDriverUnlock(privconn);
if (!obj) if (!obj)
@ -3649,7 +3650,7 @@ testConnectNumOfInterfaces(virConnectPtr conn)
int ninterfaces; int ninterfaces;
testDriverLock(privconn); testDriverLock(privconn);
ninterfaces = virInterfaceObjNumOfInterfaces(&privconn->ifaces, true); ninterfaces = virInterfaceObjNumOfInterfaces(privconn->ifaces, true);
testDriverUnlock(privconn); testDriverUnlock(privconn);
return ninterfaces; return ninterfaces;
} }
@ -3664,7 +3665,7 @@ testConnectListInterfaces(virConnectPtr conn,
int nnames; int nnames;
testDriverLock(privconn); testDriverLock(privconn);
nnames = virInterfaceObjGetNames(&privconn->ifaces, true, names, maxnames); nnames = virInterfaceObjGetNames(privconn->ifaces, true, names, maxnames);
testDriverUnlock(privconn); testDriverUnlock(privconn);
return nnames; return nnames;
@ -3678,7 +3679,7 @@ testConnectNumOfDefinedInterfaces(virConnectPtr conn)
int ninterfaces; int ninterfaces;
testDriverLock(privconn); testDriverLock(privconn);
ninterfaces = virInterfaceObjNumOfInterfaces(&privconn->ifaces, false); ninterfaces = virInterfaceObjNumOfInterfaces(privconn->ifaces, false);
testDriverUnlock(privconn); testDriverUnlock(privconn);
return ninterfaces; return ninterfaces;
} }
@ -3693,7 +3694,7 @@ testConnectListDefinedInterfaces(virConnectPtr conn,
int nnames; int nnames;
testDriverLock(privconn); testDriverLock(privconn);
nnames = virInterfaceObjGetNames(&privconn->ifaces, false, names, maxnames); nnames = virInterfaceObjGetNames(privconn->ifaces, false, names, maxnames);
testDriverUnlock(privconn); testDriverUnlock(privconn);
return nnames; return nnames;
@ -3731,7 +3732,7 @@ testInterfaceLookupByMACString(virConnectPtr conn,
virInterfacePtr ret = NULL; virInterfacePtr ret = NULL;
testDriverLock(privconn); testDriverLock(privconn);
ifacect = virInterfaceObjFindByMACString(&privconn->ifaces, mac, &obj, 1); ifacect = virInterfaceObjFindByMACString(privconn->ifaces, mac, &obj, 1);
testDriverUnlock(privconn); testDriverUnlock(privconn);
if (ifacect == 0) { if (ifacect == 0) {
@ -3789,8 +3790,7 @@ testInterfaceChangeBegin(virConnectPtr conn,
privconn->transaction_running = true; privconn->transaction_running = true;
if (virInterfaceObjListClone(&privconn->ifaces, if (!(privconn->backupIfaces = virInterfaceObjListClone(privconn->ifaces)))
&privconn->backupIfaces) < 0)
goto cleanup; goto cleanup;
ret = 0; ret = 0;
@ -3818,7 +3818,7 @@ testInterfaceChangeCommit(virConnectPtr conn,
goto cleanup; goto cleanup;
} }
virInterfaceObjListFree(&privconn->backupIfaces); virInterfaceObjListFree(privconn->backupIfaces);
privconn->transaction_running = false; privconn->transaction_running = false;
ret = 0; ret = 0;
@ -3848,11 +3848,9 @@ testInterfaceChangeRollback(virConnectPtr conn,
goto cleanup; goto cleanup;
} }
virInterfaceObjListFree(&privconn->ifaces); virInterfaceObjListFree(privconn->ifaces);
privconn->ifaces.count = privconn->backupIfaces.count; privconn->ifaces = privconn->backupIfaces;
privconn->ifaces.objs = privconn->backupIfaces.objs; privconn->backupIfaces = NULL;
privconn->backupIfaces.count = 0;
privconn->backupIfaces.objs = NULL;
privconn->transaction_running = false; privconn->transaction_running = false;
@ -3903,7 +3901,7 @@ testInterfaceDefineXML(virConnectPtr conn,
if ((def = virInterfaceDefParseString(xmlStr)) == NULL) if ((def = virInterfaceDefParseString(xmlStr)) == NULL)
goto cleanup; goto cleanup;
if ((obj = virInterfaceObjAssignDef(&privconn->ifaces, def)) == NULL) if ((obj = virInterfaceObjAssignDef(privconn->ifaces, def)) == NULL)
goto cleanup; goto cleanup;
def = NULL; def = NULL;
objdef = virInterfaceObjGetDef(obj); objdef = virInterfaceObjGetDef(obj);
@ -3928,7 +3926,7 @@ testInterfaceUndefine(virInterfacePtr iface)
if (!(obj = testInterfaceObjFindByName(privconn, iface->name))) if (!(obj = testInterfaceObjFindByName(privconn, iface->name)))
return -1; return -1;
virInterfaceObjRemove(&privconn->ifaces, obj); virInterfaceObjRemove(privconn->ifaces, obj);
return 0; return 0;
} }