]> git.sesse.net Git - vlc/blob - include/input_iovec.h
*fixed a double malloc/free bug in DVDRead
[vlc] / include / input_iovec.h
1 /*****************************************************************************
2  * input_iovec.h: iovec structure and readv() replacement
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *          Jon Lech Johansen <jon-vl@nanocrew.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * iovec structure: vectored data entry
26  *****************************************************************************/
27 struct iovec
28 {
29     void *iov_base;     /* Pointer to data. */
30     size_t iov_len;     /* Length of data.  */
31 };
32
33 #if defined( WIN32 )
34 /*****************************************************************************
35  * readv: readv() replacement for iovec-impaired C libraries
36  *****************************************************************************/
37 static __inline int readv( int i_fd, struct iovec *p_iovec, int i_count )
38 {
39     int i_index, i_len, i_total = 0;
40     char *p_base;
41
42     for( i_index = i_count; i_index; i_index-- )
43     {
44         register signed int i_bytes;
45
46         i_len  = p_iovec->iov_len;
47         p_base = p_iovec->iov_base;
48
49         /* Loop is unrolled one time to spare the (i_bytes < 0) test */
50         if( i_len > 0 )
51         {
52             i_bytes = read( i_fd, p_base, i_len );
53
54             if( ( i_total == 0 ) && ( i_bytes < 0 ) )
55             {
56                 return -1;
57             }
58
59             if( i_bytes <= 0 )
60             {
61                 return i_total;
62             }
63
64             i_len   -= i_bytes;
65             i_total += i_bytes;
66             p_base  += i_bytes;
67
68             while( i_len > 0 )
69             {
70                 i_bytes = read( i_fd, p_base, i_len );
71
72                 if( i_bytes <= 0 )
73                 {
74                     return i_total;
75                 }
76
77                 i_len   -= i_bytes;
78                 i_total += i_bytes;
79                 p_base  += i_bytes;
80             }
81         }
82
83         p_iovec++;
84     }
85
86     return i_total;
87 }
88
89 #endif