[PATCH 01/32] bhyve: use g_auto() for all virBuffers

Laine Stump laine at redhat.com
Mon Jul 6 03:40:56 UTC 2020


In most cases this eliminates one or more calls to
virBufferClearAndReset(), but even when it doesn't it's better because:

1) it makes the code more consistent, making it more likely that new contributors who are "learning by example" will to the right thing.

2) it protects against future modifications that might have otherwise
needed to add a virBufferClearAndReset()

3) Currently some functions don't call virBufferClearAndReset() only
because they're relying on some subordinate function to call it for
them (e.g. bhyveConnectGetSysinfo() in this patch relies on
virSysinfoFormat() to clear out the buffer when there is an error). I
think this is sloppy behavior, and that the toplevel function that
defines and initializes the buffer should be the function clearing it
at the end.

Signed-off-by: Laine Stump <laine at redhat.com>
---
 src/bhyve/bhyve_command.c | 15 ++++++---------
 src/bhyve/bhyve_driver.c  |  4 ++--
 2 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c
index 5b1d80083a..9649c2d2a2 100644
--- a/src/bhyve/bhyve_command.c
+++ b/src/bhyve/bhyve_command.c
@@ -166,14 +166,15 @@ bhyveBuildAHCIControllerArgStr(const virDomainDef *def,
                                bhyveConnPtr driver,
                                virCommandPtr cmd)
 {
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
-    virBuffer device = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
     const char *disk_source;
     size_t i;
     int ret = -1;
 
     for (i = 0; i < def->ndisks; i++) {
+        g_auto(virBuffer) device = VIR_BUFFER_INITIALIZER;
         virDomainDiskDefPtr disk = def->disks[i];
+
         if (disk->bus != VIR_DOMAIN_DISK_BUS_SATA)
             continue;
 
@@ -221,7 +222,6 @@ bhyveBuildAHCIControllerArgStr(const virDomainDef *def,
             goto error;
         }
         virBufferAddBuffer(&buf, &device);
-        virBufferFreeAndReset(&device);
     }
 
     virCommandAddArg(cmd, "-s");
@@ -231,7 +231,6 @@ bhyveBuildAHCIControllerArgStr(const virDomainDef *def,
 
     ret = 0;
  error:
-    virBufferFreeAndReset(&buf);
     return ret;
 }
 
@@ -378,7 +377,7 @@ bhyveBuildGraphicsArgStr(const virDomainDef *def,
                          virCommandPtr cmd,
                          bool dryRun)
 {
-    virBuffer opt = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) opt = VIR_BUFFER_INITIALIZER;
     virDomainGraphicsListenDefPtr glisten = NULL;
     bool escapeAddr;
     unsigned short port;
@@ -478,7 +477,6 @@ bhyveBuildGraphicsArgStr(const virDomainDef *def,
     return 0;
 
  error:
-    virBufferFreeAndReset(&opt);
     return -1;
 }
 
@@ -765,7 +763,6 @@ virBhyveProcessBuildGrubbhyveCmd(virDomainDefPtr def,
                                  char **devicesmap_out)
 {
     virDomainDiskDefPtr hdd, cd, userdef, diskdef;
-    virBuffer devicemap;
     virCommandPtr cmd;
     unsigned int best_idx = UINT_MAX;
     size_t i;
@@ -773,8 +770,6 @@ virBhyveProcessBuildGrubbhyveCmd(virDomainDefPtr def,
     if (def->os.bootloaderArgs != NULL)
         return virBhyveProcessBuildCustomLoaderCmd(def);
 
-    devicemap = (virBuffer)VIR_BUFFER_INITIALIZER;
-
     /* Search disk list for CD or HDD device. We'll respect <boot order=''> if
      * present and otherwise pick the first CD or failing that HDD we come
      * across. */
@@ -809,6 +804,8 @@ virBhyveProcessBuildGrubbhyveCmd(virDomainDefPtr def,
     VIR_DEBUG("grub-bhyve with default arguments");
 
     if (devicesmap_out != NULL) {
+        g_auto(virBuffer) devicemap = VIR_BUFFER_INITIALIZER;
+
         /* Grub device.map (just for boot) */
         if (userdef != NULL) {
             virBhyveFormatGrubDevice(&devicemap, userdef);
diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c
index b6204c7fb9..daa20bad40 100644
--- a/src/bhyve/bhyve_driver.c
+++ b/src/bhyve/bhyve_driver.c
@@ -244,7 +244,7 @@ static char *
 bhyveConnectGetSysinfo(virConnectPtr conn, unsigned int flags)
 {
     bhyveConnPtr privconn = conn->privateData;
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
 
     virCheckFlags(0, NULL);
 
@@ -678,7 +678,7 @@ bhyveConnectDomainXMLToNative(virConnectPtr conn,
                               const char *xmlData,
                               unsigned int flags)
 {
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
     bhyveConnPtr privconn = conn->privateData;
     virDomainDefPtr def = NULL;
     virCommandPtr cmd = NULL, loadcmd = NULL;
-- 
2.25.4




More information about the libvir-list mailing list