[vlc-commits] en50221: avoid one memory copy (refs #12307)

Rémi Denis-Courmont git at videolan.org
Mon Dec 29 22:09:45 CET 2014


vlc/vlc-2.2 | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Mon Sep 29 21:59:47 2014 +0300| [1c260a3aa0aa51233b705311f1017422d1b5aa10] | committer: Jean-Baptiste Kempf

en50221: avoid one memory copy (refs #12307)

(cherry picked from commit 34c1ae55153af567ee6d51f53046bbbc3ef39c0c)
Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc/vlc-2.2.git/?a=commit;h=1c260a3aa0aa51233b705311f1017422d1b5aa10
---

 modules/access/dtv/en50221.c |   62 ++++++++++--------------------------------
 1 file changed, 14 insertions(+), 48 deletions(-)

diff --git a/modules/access/dtv/en50221.c b/modules/access/dtv/en50221.c
index ac9bb4c..1832f87 100644
--- a/modules/access/dtv/en50221.c
+++ b/modules/access/dtv/en50221.c
@@ -34,6 +34,7 @@
 #include <assert.h>
 #include <sys/types.h>
 #include <sys/ioctl.h>
+#include <sys/uio.h>
 #include <poll.h>
 #include <netinet/in.h>
 
@@ -243,64 +244,29 @@ static void Dump( bool b_outgoing, uint8_t *p_data, int i_size )
  * TPDUSend
  *****************************************************************************/
 static int TPDUSend( cam_t * p_cam, uint8_t i_slot, uint8_t i_tag,
-                     const uint8_t *p_content, int i_length )
+                     const uint8_t *p_content, size_t i_length )
 {
-    uint8_t i_tcid = i_slot + 1;
-    uint8_t p_data[MAX_TPDU_SIZE];
-    int i_size;
-
-    i_size = 0;
-    p_data[0] = i_slot;
-    p_data[1] = i_tcid;
-    p_data[2] = i_tag;
-
-    switch ( i_tag )
-    {
-    case T_RCV:
-    case T_CREATE_TC:
-    case T_CTC_REPLY:
-    case T_DELETE_TC:
-    case T_DTC_REPLY:
-    case T_REQUEST_TC:
-        p_data[3] = 1; /* length */
-        p_data[4] = i_tcid;
-        i_size = 5;
-        break;
-
-    case T_NEW_TC:
-    case T_TC_ERROR:
-        p_data[3] = 2; /* length */
-        p_data[4] = i_tcid;
-        p_data[5] = p_content[0];
-        i_size = 6;
-        break;
+    uint8_t p_data[9], *p = p_data;
 
-    case T_DATA_LAST:
-    case T_DATA_MORE:
-    {
-        /* i_length <= MAX_TPDU_DATA */
-        uint8_t *p = p_data + 3;
-        p = SetLength( p, i_length + 1 );
-        *p++ = i_tcid;
+    *(p++) = i_slot;
+    *(p++) = i_slot + 1; /* TCID */
+    *(p++) = i_tag;
+    p = SetLength( p, i_length + 1 );
 
-        if ( i_length )
-            memcpy( p, p_content, i_length );
-        i_size = i_length + (p - p_data);
-        break;
-    }
+    *(p++) = i_slot + 1;
+    Dump( true, p_data, p - p_data );
 
-    default:
-        break;
-    }
-    Dump( true, p_data, i_size );
+    const struct iovec iov[2] = {
+        { p_data, p - p_data },
+        { (void *)p_content, i_length },
+    };
 
-    if ( write( p_cam->fd, p_data, i_size ) != i_size )
+    if ( writev( p_cam->fd, iov, 2 ) <= 0 )
     {
         msg_Err( p_cam->obj, "cannot write to CAM device: %s",
                  vlc_strerror_c(errno) );
         return VLC_EGENERIC;
     }
-
     return VLC_SUCCESS;
 }
 



More information about the vlc-commits mailing list