[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v3 03/11] util: netlink: use VIR_AUTOFREE instead of VIR_FREE for scalar types



By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar <skrtbhtngr gmail com>
---
 src/util/virnetlink.c | 43 ++++++++++++++++++++-----------------------
 1 file changed, 20 insertions(+), 23 deletions(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index ecf62c9..fcdc09d 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -342,10 +342,8 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
                       unsigned int protocol, unsigned int groups,
                       void *opaque)
 {
-    int ret = -1;
     bool end = false;
     int len = 0;
-    struct nlmsghdr *resp = NULL;
     struct nlmsghdr *msg = NULL;
 
     struct sockaddr_nl nladdr = {
@@ -357,9 +355,11 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
 
     if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
                                            protocol, groups)))
-        goto cleanup;
+        return -1;
 
     while (!end) {
+        VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
         len = nl_recv(nlhandle, &nladdr, (unsigned char **)&resp, NULL);
         VIR_WARNINGS_NO_CAST_ALIGN
         for (msg = resp; NLMSG_OK(msg, len); msg = NLMSG_NEXT(msg, len)) {
@@ -368,19 +368,14 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
                 end = true;
 
             if (virNetlinkGetErrorCode(msg, len) < 0)
-                goto cleanup;
+                return -1;
 
             if (callback(msg, opaque) < 0)
-                goto cleanup;
+                return -1;
         }
-        VIR_FREE(resp);
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(resp);
-    return ret;
+    return 0;
 }
 
 /**
@@ -408,7 +403,6 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
                    uint32_t src_pid, uint32_t dst_pid)
 {
     int rc = -1;
-    struct nlmsghdr *resp = NULL;
     struct nlmsgerr *err;
     struct ifinfomsg ifinfo = {
         .ifi_family = AF_UNSPEC,
@@ -416,6 +410,9 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
     };
     unsigned int recvbuflen;
     struct nl_msg *nl_msg;
+    VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
+    *nlData = NULL;
 
     if (ifname && ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
         return -1;
@@ -483,12 +480,12 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
     default:
         goto malformed_resp;
     }
+
+    VIR_STEAL_PTR(*nlData, resp);
     rc = 0;
+
  cleanup:
     nlmsg_free(nl_msg);
-    if (rc < 0)
-       VIR_FREE(resp);
-    *nlData = resp;
     return rc;
 
  malformed_resp:
@@ -522,11 +519,11 @@ int
 virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
 {
     int rc = -1;
-    struct nlmsghdr *resp = NULL;
     struct nlmsgerr *err;
     struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
     unsigned int recvbuflen;
     struct nl_msg *nl_msg;
+    VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
 
     nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
                                 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
@@ -577,7 +574,6 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
     rc = 0;
  cleanup:
     nlmsg_free(nl_msg);
-    VIR_FREE(resp);
     return rc;
 
  malformed_resp:
@@ -610,13 +606,15 @@ int
 virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
 {
     int rc = -1;
-    struct nlmsghdr *resp = NULL;
     struct nlmsgerr *err;
     struct ndmsg ndinfo = {
         .ndm_family = AF_UNSPEC,
     };
     unsigned int recvbuflen;
     struct nl_msg *nl_msg;
+    VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
+    *nlData = NULL;
 
     nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
     if (!nl_msg) {
@@ -654,13 +652,12 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
     default:
         goto malformed_resp;
     }
+
+    VIR_STEAL_PTR(*nlData, resp);
     rc = recvbuflen;
 
  cleanup:
     nlmsg_free(nl_msg);
-    if (rc < 0)
-       VIR_FREE(resp);
-    *nlData = resp;
     return rc;
 
  malformed_resp:
@@ -766,12 +763,12 @@ virNetlinkEventCallback(int watch,
                         void *opaque)
 {
     virNetlinkEventSrvPrivatePtr srv = opaque;
-    struct nlmsghdr *msg;
     struct sockaddr_nl peer;
     struct ucred *creds = NULL;
     size_t i;
     int length;
     bool handled = false;
+    VIR_AUTOFREE(struct nlmsghdr *) msg = NULL;
 
     length = nl_recv(srv->netlinknh, &peer,
                      (unsigned char **)&msg, &creds);
@@ -801,7 +798,7 @@ virNetlinkEventCallback(int watch,
 
     if (!handled)
         VIR_DEBUG("event not handled.");
-    VIR_FREE(msg);
+
     virNetlinkEventServerUnlock(srv);
 }
 
-- 
1.8.3.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]