[libvirt] [PATCH 04/10] capabilities, cpu: use new array API

Eric Blake eblake at redhat.com
Thu Nov 18 04:28:56 UTC 2010


* src/conf/capabilities.h (_virCaps, _virCapsHost, _virCapsGuest)
(_virCapsGuestArch): Add additional fields.
* src/conf/cpu_conf.h (_virCPUDef): Likewise.
* src/conf/capabilities.c (virCapabilitiesFormatXML): Reflect
updated type.
(virCapabilitiesAddGuest, virCapabilitiesAddHostFeature)
(virCapabilitiesAddHostMigrateTransport)
(virCapabilitiesAddHostNUMACell, virCapabilitiesAddGuestFeature)
(virCapabilitiesAddGuestDomain): Use new array APIs.
* src/conf/cpu_conf.c (virCPUDefAddFeature, virCPUDefCopy)
(virCPUDefParseXML): Likewise.
* tests/testutilsqemu.c (testQemuCapsInit): Adjust test.
---
 src/conf/capabilities.c |   37 +++++++++++++++++--------------------
 src/conf/capabilities.h |   20 +++++++++++++-------
 src/conf/cpu_conf.c     |    7 +++++--
 src/conf/cpu_conf.h     |    5 +++--
 tests/testutilsqemu.c   |    1 +
 5 files changed, 39 insertions(+), 31 deletions(-)

diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
index 4478d85..99d5a56 100644
--- a/src/conf/capabilities.c
+++ b/src/conf/capabilities.c
@@ -1,7 +1,7 @@
 /*
  * capabilities.c: hypervisor capabilities
  *
- * Copyright (C) 2006-2008 Red Hat, Inc.
+ * Copyright (C) 2006-2008, 2010 Red Hat, Inc.
  * Copyright (C) 2006-2008 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -190,8 +190,8 @@ int
 virCapabilitiesAddHostFeature(virCapsPtr caps,
                               const char *name)
 {
-    if (VIR_REALLOC_N(caps->host.features,
-                      caps->host.nfeatures + 1) < 0)
+    if (VIR_RESIZE_N(caps->host.features, caps->host.nfeatures_max,
+                     caps->host.nfeatures, 1) < 0)
         return -1;

     if ((caps->host.features[caps->host.nfeatures] = strdup(name)) == NULL)
@@ -213,8 +213,8 @@ int
 virCapabilitiesAddHostMigrateTransport(virCapsPtr caps,
                                        const char *name)
 {
-    if (VIR_REALLOC_N(caps->host.migrateTrans,
-                      caps->host.nmigrateTrans + 1) < 0)
+    if (VIR_RESIZE_N(caps->host.migrateTrans, caps->host.nmigrateTrans_max,
+                     caps->host.nmigrateTrans, 1) < 0)
         return -1;

     if ((caps->host.migrateTrans[caps->host.nmigrateTrans] = strdup(name)) == NULL)
@@ -243,8 +243,8 @@ virCapabilitiesAddHostNUMACell(virCapsPtr caps,
 {
     virCapsHostNUMACellPtr cell;

-    if (VIR_REALLOC_N(caps->host.numaCell,
-                      caps->host.nnumaCell + 1) < 0)
+    if (VIR_RESIZE_N(caps->host.numaCell, caps->host.nnumaCell_max,
+                     caps->host.nnumaCell, 1) < 0)
         return -1;

     if (VIR_ALLOC(cell) < 0)
@@ -261,8 +261,7 @@ virCapabilitiesAddHostNUMACell(virCapsPtr caps,
     cell->ncpus = ncpus;
     cell->num = num;

-    caps->host.numaCell[caps->host.nnumaCell] = cell;
-    caps->host.nnumaCell++;
+    caps->host.numaCell[caps->host.nnumaCell++] = cell;

     return 0;
 }
@@ -380,11 +379,10 @@ virCapabilitiesAddGuest(virCapsPtr caps,
         (guest->arch.defaultInfo.loader = strdup(loader)) == NULL)
         goto no_memory;

-    if (VIR_REALLOC_N(caps->guests,
-                      caps->nguests + 1) < 0)
+    if (VIR_RESIZE_N(caps->guests, caps->nguests_max,
+                     caps->nguests, 1) < 0)
         goto no_memory;
-    caps->guests[caps->nguests] = guest;
-    caps->nguests++;
+    caps->guests[caps->nguests++] = guest;

     if (nmachines) {
         guest->arch.defaultInfo.nmachines = nmachines;
@@ -434,8 +432,8 @@ virCapabilitiesAddGuestDomain(virCapsGuestPtr guest,
         (dom->info.loader = strdup(loader)) == NULL)
         goto no_memory;

-    if (VIR_REALLOC_N(guest->arch.domains,
-                      guest->arch.ndomains + 1) < 0)
+    if (VIR_RESIZE_N(guest->arch.domains, guest->arch.ndomains_max,
+                     guest->arch.ndomains, 1) < 0)
         goto no_memory;
     guest->arch.domains[guest->arch.ndomains] = dom;
     guest->arch.ndomains++;
@@ -478,11 +476,10 @@ virCapabilitiesAddGuestFeature(virCapsGuestPtr guest,
     feature->defaultOn = defaultOn;
     feature->toggle = toggle;

-    if (VIR_REALLOC_N(guest->features,
-                      guest->nfeatures + 1) < 0)
+    if (VIR_RESIZE_N(guest->features, guest->nfeatures_max,
+                     guest->nfeatures, 1) < 0)
         goto no_memory;
-    guest->features[guest->nfeatures] = feature;
-    guest->nfeatures++;
+    guest->features[guest->nfeatures++] = feature;

     return feature;

@@ -706,7 +703,7 @@ virCapabilitiesFormatXML(virCapsPtr caps)

     if (caps->host.nnumaCell) {
         virBufferAddLit(&xml, "    <topology>\n");
-        virBufferVSprintf(&xml, "      <cells num='%d'>\n",
+        virBufferVSprintf(&xml, "      <cells num='%zu'>\n",
                           caps->host.nnumaCell);
         for (i = 0 ; i < caps->host.nnumaCell ; i++) {
             virBufferVSprintf(&xml, "        <cell id='%d'>\n",
diff --git a/src/conf/capabilities.h b/src/conf/capabilities.h
index f41be1c..759265d 100644
--- a/src/conf/capabilities.h
+++ b/src/conf/capabilities.h
@@ -1,7 +1,7 @@
 /*
  * capabilities.h: hypervisor capabilities
  *
- * Copyright (C) 2006-2008 Red Hat, Inc.
+ * Copyright (C) 2006-2008, 2010 Red Hat, Inc.
  * Copyright (C) 2006-2008 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -71,7 +71,8 @@ struct _virCapsGuestArch {
     char *name;
     int wordsize;
     virCapsGuestDomainInfo defaultInfo;
-    int ndomains;
+    size_t ndomains;
+    size_t ndomains_max;
     virCapsGuestDomainPtr *domains;
 };

@@ -80,7 +81,8 @@ typedef virCapsGuest *virCapsGuestPtr;
 struct _virCapsGuest {
     char *ostype;
     virCapsGuestArch arch;
-    int nfeatures;
+    size_t nfeatures;
+    size_t nfeatures_max;
     virCapsGuestFeaturePtr *features;
 };

@@ -102,13 +104,16 @@ typedef struct _virCapsHost virCapsHost;
 typedef virCapsHost *virCapsHostPtr;
 struct _virCapsHost {
     char *arch;
-    int nfeatures;
+    size_t nfeatures;
+    size_t nfeatures_max;
     char **features;
     int offlineMigrate;
     int liveMigrate;
-    int nmigrateTrans;
+    size_t nmigrateTrans;
+    size_t nmigrateTrans_max;
     char **migrateTrans;
-    int nnumaCell;
+    size_t nnumaCell;
+    size_t nnumaCell_max;
     virCapsHostNUMACellPtr *numaCell;
     virCapsHostSecModel secModel;
     virCPUDefPtr cpu;
@@ -134,7 +139,8 @@ typedef struct _virCaps virCaps;
 typedef virCaps* virCapsPtr;
 struct _virCaps {
     virCapsHost host;
-    int nguests;
+    size_t nguests;
+    size_t nguests_max;
     virCapsGuestPtr *guests;
     unsigned char macPrefix[VIR_MAC_PREFIX_BUFLEN];
     unsigned int emulatorRequired : 1;
diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
index f8d006d..7f03eaa 100644
--- a/src/conf/cpu_conf.c
+++ b/src/conf/cpu_conf.c
@@ -83,6 +83,7 @@ virCPUDefCopy(const virCPUDefPtr cpu)
         || (cpu->vendor && !(copy->vendor = strdup(cpu->vendor)))
         || VIR_ALLOC_N(copy->features, cpu->nfeatures) < 0)
         goto no_memory;
+    copy->nfeatures_max = cpu->nfeatures;

     copy->type = cpu->type;
     copy->match = cpu->match;
@@ -234,7 +235,8 @@ virCPUDefParseXML(const xmlNodePtr node,
             goto error;
         }

-        if (VIR_ALLOC_N(def->features, n) < 0)
+        if (VIR_RESIZE_N(def->features, def->nfeatures_max,
+                         def->nfeatures, n) < 0)
             goto no_memory;
         def->nfeatures = n;
     }
@@ -425,7 +427,8 @@ virCPUDefAddFeature(virCPUDefPtr def,
         }
     }

-    if (VIR_REALLOC_N(def->features, def->nfeatures + 1) < 0)
+    if (VIR_RESIZE_N(def->features, def->nfeatures_max,
+                     def->nfeatures, 1) < 0)
         goto no_memory;

     if (def->type == VIR_CPU_TYPE_HOST)
diff --git a/src/conf/cpu_conf.h b/src/conf/cpu_conf.h
index 6151683..055887c 100644
--- a/src/conf/cpu_conf.h
+++ b/src/conf/cpu_conf.h
@@ -1,7 +1,7 @@
 /*
  * cpu_conf.h: CPU XML handling
  *
- * Copyright (C) 2009 Red Hat, Inc.
+ * Copyright (C) 2009, 2010 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -74,7 +74,8 @@ struct _virCPUDef {
     unsigned int sockets;
     unsigned int cores;
     unsigned int threads;
-    unsigned int nfeatures;
+    size_t nfeatures;
+    size_t nfeatures_max;
     virCPUFeatureDefPtr features;
 };

diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index dd33b62..72fc8aa 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -89,6 +89,7 @@ virCapsPtr testQemuCapsInit(void) {
         2,                      /* cores */
         1,                      /* threads */
         ARRAY_CARDINALITY(host_cpu_features), /* nfeatures */
+        ARRAY_CARDINALITY(host_cpu_features), /* nfeatures_max */
         host_cpu_features       /* features */
     };

-- 
1.7.3.2




More information about the libvir-list mailing list