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

[libvirt] [PATCH v2 04/35] 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 | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index 1c1eac7..aa3a86d 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -346,7 +346,6 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
     int ret = -1;
     bool end = false;
     int len = 0;
-    struct nlmsghdr *resp = NULL;
     struct nlmsghdr *msg = NULL;
 
     struct sockaddr_nl nladdr = {
@@ -361,6 +360,8 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
         goto cleanup;
 
     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)) {
@@ -374,13 +375,11 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
             if (callback(msg, opaque) < 0)
                 goto cleanup;
         }
-        VIR_FREE(resp);
     }
 
     ret = 0;
 
  cleanup:
-    VIR_FREE(resp);
     virNetlinkFree(nlhandle);
     return ret;
 }
@@ -410,7 +409,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,
@@ -418,6 +416,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;
@@ -485,12 +486,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:
@@ -524,11 +525,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);
@@ -579,7 +580,6 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
     rc = 0;
  cleanup:
     nlmsg_free(nl_msg);
-    VIR_FREE(resp);
     return rc;
 
  malformed_resp:
@@ -612,13 +612,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) {
@@ -656,13 +658,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:
@@ -768,12 +769,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);
@@ -803,7 +804,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]