[Spice-devel] [PATCH spice-server 28/30] Simplify and fix constrain_iov

Frediano Ziglio fziglio at redhat.com
Mon Nov 21 12:52:15 UTC 2016


Use spice_memdup instead of manual copy.
Trim the original iov if necessary.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/websocket.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/server/websocket.c b/server/websocket.c
index 4af3748..38486b6 100644
--- a/server/websocket.c
+++ b/server/websocket.c
@@ -340,26 +340,24 @@ static void constrain_iov(struct iovec *iov, int iovcnt,
                           struct iovec **iov_out, int *iov_out_cnt,
                           guint64 maxlen)
 {
-    int i, j;
-
-    *iov_out = iov;
-    *iov_out_cnt = iovcnt;
+    int i;
 
     for (i = 0; i < iovcnt && maxlen > 0; i++) {
         if (iov[i].iov_len > maxlen) {
             /* TODO - This code has never triggered afaik... */
-            *iov_out_cnt = i + 1;
-            *iov_out = spice_malloc((*iov_out_cnt) * sizeof (**iov_out));
-            for (j = 0; j < i; j++) {
-                (*iov_out)[j].iov_base = iov[j].iov_base;
-                (*iov_out)[j].iov_len = iov[j].iov_len;
-            }
-            (*iov_out)[j].iov_base = iov[j].iov_base;
-            (*iov_out)[j].iov_len = maxlen;
-            break;
+            *iov_out_cnt = ++i;
+            *iov_out = spice_memdup(iov, i * sizeof (*iov));
+            (*iov_out)[i-1].iov_len = maxlen;
+            return;
         }
         maxlen -= iov[i].iov_len;
     }
+
+    /* we must trim the iov in case maxlen initially matches some chunks
+     * For instance if initially we had 2 chunks 256 and 128 bytes respectively
+     * and a maxlen of 256 we should jut return the first chunk */
+    *iov_out_cnt = i;
+    *iov_out = iov;
 }
 
 static int send_data_header_left(RedsWebSocket *ws)
-- 
2.7.4



More information about the Spice-devel mailing list