util: Slightly refactor PCI list functions

In order to be able to steal PCI device by its index in the list.
This commit is contained in:
Jiri Denemark 2012-12-04 08:30:46 +01:00
parent ea1a9b5fdd
commit 5eb8a7ac4d
3 changed files with 46 additions and 28 deletions

View File

@ -1000,10 +1000,12 @@ pciDeviceListAdd;
pciDeviceListCount; pciDeviceListCount;
pciDeviceListDel; pciDeviceListDel;
pciDeviceListFind; pciDeviceListFind;
pciDeviceListFindIndex;
pciDeviceListFree; pciDeviceListFree;
pciDeviceListGet; pciDeviceListGet;
pciDeviceListNew; pciDeviceListNew;
pciDeviceListSteal; pciDeviceListSteal;
pciDeviceListStealIndex;
pciDeviceNetName; pciDeviceNetName;
pciDeviceReAttachInit; pciDeviceReAttachInit;
pciDeviceSetManaged; pciDeviceSetManaged;

View File

@ -1554,35 +1554,36 @@ pciDeviceListCount(pciDeviceList *list)
} }
pciDevice * pciDevice *
pciDeviceListSteal(pciDeviceList *list, pciDeviceListStealIndex(pciDeviceList *list,
pciDevice *dev) int idx)
{ {
pciDevice *ret = NULL; pciDevice *ret;
int i;
for (i = 0; i < list->count; i++) { if (idx < 0 || idx >= list->count)
if (list->devs[i]->domain != dev->domain || return NULL;
list->devs[i]->bus != dev->bus ||
list->devs[i]->slot != dev->slot ||
list->devs[i]->function != dev->function)
continue;
ret = list->devs[i]; ret = list->devs[idx];
if (i != --list->count) if (idx != --list->count) {
memmove(&list->devs[i], memmove(&list->devs[idx],
&list->devs[i+1], &list->devs[idx + 1],
sizeof(*list->devs) * (list->count-i)); sizeof(*list->devs) * (list->count - idx));
}
if (VIR_REALLOC_N(list->devs, list->count) < 0) { if (VIR_REALLOC_N(list->devs, list->count) < 0) {
; /* not fatal */ ; /* not fatal */
} }
break;
}
return ret; return ret;
} }
pciDevice *
pciDeviceListSteal(pciDeviceList *list,
pciDevice *dev)
{
return pciDeviceListStealIndex(list, pciDeviceListFindIndex(list, dev));
}
void void
pciDeviceListDel(pciDeviceList *list, pciDeviceListDel(pciDeviceList *list,
pciDevice *dev) pciDevice *dev)
@ -1592,8 +1593,8 @@ pciDeviceListDel(pciDeviceList *list,
pciFreeDevice(ret); pciFreeDevice(ret);
} }
pciDevice * int
pciDeviceListFind(pciDeviceList *list, pciDevice *dev) pciDeviceListFindIndex(pciDeviceList *list, pciDevice *dev)
{ {
int i; int i;
@ -1602,7 +1603,18 @@ pciDeviceListFind(pciDeviceList *list, pciDevice *dev)
list->devs[i]->bus == dev->bus && list->devs[i]->bus == dev->bus &&
list->devs[i]->slot == dev->slot && list->devs[i]->slot == dev->slot &&
list->devs[i]->function == dev->function) list->devs[i]->function == dev->function)
return i;
return -1;
}
pciDevice *
pciDeviceListFind(pciDeviceList *list, pciDevice *dev)
{
int i;
if ((i = pciDeviceListFindIndex(list, dev)) >= 0)
return list->devs[i]; return list->devs[i];
else
return NULL; return NULL;
} }

View File

@ -75,10 +75,14 @@ pciDevice * pciDeviceListGet (pciDeviceList *list,
int pciDeviceListCount (pciDeviceList *list); int pciDeviceListCount (pciDeviceList *list);
pciDevice * pciDeviceListSteal (pciDeviceList *list, pciDevice * pciDeviceListSteal (pciDeviceList *list,
pciDevice *dev); pciDevice *dev);
pciDevice * pciDeviceListStealIndex(pciDeviceList *list,
int idx);
void pciDeviceListDel (pciDeviceList *list, void pciDeviceListDel (pciDeviceList *list,
pciDevice *dev); pciDevice *dev);
pciDevice * pciDeviceListFind (pciDeviceList *list, pciDevice * pciDeviceListFind (pciDeviceList *list,
pciDevice *dev); pciDevice *dev);
int pciDeviceListFindIndex(pciDeviceList *list,
pciDevice *dev);
/* /*
* Callback that will be invoked once for each file * Callback that will be invoked once for each file