[PATCH v2 02/17] virFDStreamMsgQueuePush: Clear pointer to passed message

Michal Privoznik posted 17 patches 5 years, 7 months ago
There is a newer version of this series
[PATCH v2 02/17] virFDStreamMsgQueuePush: Clear pointer to passed message
Posted by Michal Privoznik 5 years, 7 months ago
All callers of virFDStreamMsgQueuePush() have the same pattern:
they explicitly set @msg passed to NULL to avoid freeing it later
on. Well, the function can take address of the pointer and clear
it for them.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/util/virfdstream.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c
index e29c95690b..6efe6c17ad 100644
--- a/src/util/virfdstream.c
+++ b/src/util/virfdstream.c
@@ -140,7 +140,7 @@ VIR_ONCE_GLOBAL_INIT(virFDStreamData);
 
 static int
 virFDStreamMsgQueuePush(virFDStreamDataPtr fdst,
-                        virFDStreamMsgPtr msg,
+                        virFDStreamMsgPtr *msg,
                         int fd,
                         const char *fdname)
 {
@@ -150,7 +150,7 @@ virFDStreamMsgQueuePush(virFDStreamDataPtr fdst,
     while (*tmp)
         tmp = &(*tmp)->next;
 
-    *tmp = msg;
+    *tmp = g_steal_pointer(msg);
     virCondSignal(&fdst->threadCond);
 
     if (safewrite(fd, &c, sizeof(c)) != sizeof(c)) {
@@ -489,8 +489,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst,
             *dataLen -= got;
     }
 
-    virFDStreamMsgQueuePush(fdst, msg, fdout, fdoutname);
-    msg = NULL;
+    virFDStreamMsgQueuePush(fdst, &msg, fdout, fdoutname);
 
     return got;
 
@@ -814,8 +813,7 @@ static int virFDStreamWrite(virStreamPtr st, const char *bytes, size_t nbytes)
         msg->stream.data.buf = buf;
         msg->stream.data.len = nbytes;
 
-        virFDStreamMsgQueuePush(fdst, msg, fdst->fd, "pipe");
-        msg = NULL;
+        virFDStreamMsgQueuePush(fdst, &msg, fdst->fd, "pipe");
         ret = nbytes;
     } else {
      retry:
@@ -1010,8 +1008,7 @@ virFDStreamSendHole(virStreamPtr st,
 
             msg->type = VIR_FDSTREAM_MSG_TYPE_HOLE;
             msg->stream.hole.len = length;
-            virFDStreamMsgQueuePush(fdst, msg, fdst->fd, "pipe");
-            msg = NULL;
+            virFDStreamMsgQueuePush(fdst, &msg, fdst->fd, "pipe");
         }
     } else {
         off = lseek(fdst->fd, length, SEEK_CUR);
-- 
2.26.2

Re: [PATCH v2 02/17] virFDStreamMsgQueuePush: Clear pointer to passed message
Posted by Peter Krempa 5 years, 5 months ago
On Tue, Jul 07, 2020 at 21:46:20 +0200, Michal Privoznik wrote:
> All callers of virFDStreamMsgQueuePush() have the same pattern:
> they explicitly set @msg passed to NULL to avoid freeing it later
> on. Well, the function can take address of the pointer and clear
> it for them.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/util/virfdstream.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)

Reviewed-by: Peter Krempa <pkrempa@redhat.com>