Re: [libvirt] [PATCH 10/17] conf: hostdev utility functions

2012-03-02 Thread Eric Blake
On 02/28/2012 01:14 PM, Laine Stump wrote:
> Three new functions useful in other files:
> 
> virDomainHostdevInsert:
> 
> Add a new hostdev at the end of the array. This would more sensibly be
> called virDomainHostdevAppend, but the existing functions for other
> types of devices are called Insert.
> 
> virDomainHostdevRemove:
> 
> Eliminates one entry from the hostdevs array, but doesn't free it;
> patterned after the code at the end of the two
> qemuDomainDetachHostXXXDevice functions (and also other pre-existing
> virDomainXXXRemove functions for other device types).
> 
> virDomainHostdevFind:
> 
> This function is patterned from the search loops at the top of
> qemuDomainDetachHostPciDevice and qemuDomainDetachHostUsbDevice, and
> will be used to re-factor those (and other detach-related) functions.
> ---
> New patch for V2.
> 
>  src/conf/domain_conf.c   |   94 
> ++
>  src/conf/domain_conf.h   |5 ++
>  src/libvirt_private.syms |3 +
>  3 files changed, 102 insertions(+), 0 deletions(-)
> 
> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index 93fd8d7..94ee634 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c
> @@ -6769,6 +6769,100 @@ virDomainChrTargetTypeToString(int deviceType,
>  }
>  
>  int
> +virDomainHostdevInsert(virDomainDefPtr def, virDomainHostdevDefPtr hostdev)
> +{
> +if (VIR_REALLOC_N(def->hostdevs, def->nhostdevs + 1) < 0)
> +return -1;
> +def->hostdevs[def->nhostdevs++]  = hostdev;

Is the double space intended?

> +return 0;
> +}
> +
> +void
> +virDomainHostdevRemove(virDomainDefPtr def, size_t i)
> +{
> +if (def->nhostdevs > 1) {
> +memmove(def->hostdevs + i,
> +def->hostdevs + i + 1,
> +sizeof(*def->hostdevs) *
> +(def->nhostdevs - (i + 1)));
> +def->nhostdevs--;
> +if (VIR_REALLOC_N(def->hostdevs, def->nhostdevs) < 0) {

I know this is copy and paste, but we could clean this pattern up
throughout the file (later) to use VIR_SHRINK_N.

ACK.

-- 
Eric Blake   ebl...@redhat.com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH 10/17] conf: hostdev utility functions

2012-02-28 Thread Laine Stump
Three new functions useful in other files:

virDomainHostdevInsert:

Add a new hostdev at the end of the array. This would more sensibly be
called virDomainHostdevAppend, but the existing functions for other
types of devices are called Insert.

virDomainHostdevRemove:

Eliminates one entry from the hostdevs array, but doesn't free it;
patterned after the code at the end of the two
qemuDomainDetachHostXXXDevice functions (and also other pre-existing
virDomainXXXRemove functions for other device types).

virDomainHostdevFind:

This function is patterned from the search loops at the top of
qemuDomainDetachHostPciDevice and qemuDomainDetachHostUsbDevice, and
will be used to re-factor those (and other detach-related) functions.
---
New patch for V2.

 src/conf/domain_conf.c   |   94 ++
 src/conf/domain_conf.h   |5 ++
 src/libvirt_private.syms |3 +
 3 files changed, 102 insertions(+), 0 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 93fd8d7..94ee634 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -6769,6 +6769,100 @@ virDomainChrTargetTypeToString(int deviceType,
 }
 
 int
+virDomainHostdevInsert(virDomainDefPtr def, virDomainHostdevDefPtr hostdev)
+{
+if (VIR_REALLOC_N(def->hostdevs, def->nhostdevs + 1) < 0)
+return -1;
+def->hostdevs[def->nhostdevs++]  = hostdev;
+return 0;
+}
+
+void
+virDomainHostdevRemove(virDomainDefPtr def, size_t i)
+{
+if (def->nhostdevs > 1) {
+memmove(def->hostdevs + i,
+def->hostdevs + i + 1,
+sizeof(*def->hostdevs) *
+(def->nhostdevs - (i + 1)));
+def->nhostdevs--;
+if (VIR_REALLOC_N(def->hostdevs, def->nhostdevs) < 0) {
+/* ignore, harmless */
+}
+} else {
+VIR_FREE(def->hostdevs);
+def->nhostdevs = 0;
+}
+}
+
+/* Find an entry in hostdevs that matches the source spec in
+ * @match. return pointer to the entry in @found (if found is
+ * non-NULL). Returns index (within hostdevs) of matched entry, or -1
+ * if no match was found.
+ */
+int
+virDomainHostdevFind(virDomainDefPtr def,
+ virDomainHostdevDefPtr match,
+ virDomainHostdevDefPtr *found)
+{
+virDomainHostdevDefPtr local_found;
+virDomainHostdevSubsysPtr m_subsys = &match->source.subsys;
+int i;
+
+if (!found)
+found = &local_found;
+*found = NULL;
+
+/* There is no code that uses _MODE_CAPABILITIES, and nothing to
+ * compare if it did, so don't allow it.
+ */
+if (match->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
+return -1;
+
+for (i = 0 ; i < def->nhostdevs ; i++) {
+virDomainHostdevDefPtr compare = def->hostdevs[i];
+virDomainHostdevSubsysPtr c_subsys = &compare->source.subsys;
+
+if ((compare->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) ||
+(c_subsys->type != m_subsys->type)) {
+continue;
+}
+
+switch (m_subsys->type)
+{
+case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
+if ((c_subsys->u.pci.domain   == m_subsys->u.pci.domain) &&
+(c_subsys->u.pci.bus  == m_subsys->u.pci.bus) &&
+(c_subsys->u.pci.slot == m_subsys->u.pci.slot) &&
+(c_subsys->u.pci.function == m_subsys->u.pci.function)) {
+*found = compare;
+}
+break;
+case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
+if (m_subsys->u.usb.bus && m_subsys->u.usb.device) {
+/* specified by bus location on host */
+if ((c_subsys->u.usb.bus== m_subsys->u.usb.bus) &&
+(c_subsys->u.usb.device == m_subsys->u.usb.device)) {
+*found = compare;
+}
+} else {
+/* specified by product & vendor id */
+if ((c_subsys->u.usb.product == m_subsys->u.usb.product) &&
+(c_subsys->u.usb.vendor  == m_subsys->u.usb.vendor)) {
+*found = compare;
+}
+}
+break;
+default:
+break;
+}
+if (*found)
+break;
+}
+return *found ? i : -1;
+}
+
+int
 virDomainDiskIndexByName(virDomainDefPtr def, const char *name,
  bool allow_ambiguous)
 {
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 1a29fdb..343e48d 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1895,6 +1895,11 @@ int virDomainNetIndexByMac(virDomainDefPtr def, const 
unsigned char *mac);
 int virDomainNetInsert(virDomainDefPtr def, virDomainNetDefPtr net);
 int virDomainNetRemoveByMac(virDomainDefPtr def, const unsigned char *mac);
 
+int virDomainHostdevInsert(virDomainDefPtr def, virDomainHostdevDefPtr 
hostdev);
+void virDomainHostdevRemove(virDomainDefPtr def, size_t i);
+int virDomainHostdevFind(vir