Qt: ExtensionCopy: fix memleak
[vlc.git] / compat / poll.c
blob8c37609863b129f95b9cc0625dd06380dc953913
1 /*****************************************************************************
2 * poll.c: poll() emulation
3 *****************************************************************************
4 * Copyright © 2007-2012 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
29 #ifdef _WIN32
30 # ifdef FD_SETSIZE
31 /* Too late for #undef FD_SETSIZE to work: fd_set is already defined. */
32 # error Header inclusion order compromised!
33 # endif
34 # define FD_SETSIZE 0
35 # include <winsock2.h>
36 #else
37 # include <sys/time.h>
38 # include <sys/select.h>
39 #endif
41 int (poll) (struct pollfd *fds, unsigned nfds, int timeout)
43 #ifdef _WIN32
44 size_t setsize = sizeof (fd_set) + nfds * sizeof (SOCKET);
45 fd_set *rdset = malloc (setsize);
46 fd_set *wrset = malloc (setsize);
47 fd_set *exset = malloc (setsize);
49 if (rdset == NULL || wrset == NULL || exset == NULL)
51 free (rdset);
52 free (wrset);
53 free (exset);
54 errno = ENOMEM;
55 return -1;
57 /* Winsock FD_SET uses FD_SETSIZE in its expansion */
58 # undef FD_SETSIZE
59 # define FD_SETSIZE (nfds)
60 #else
61 fd_set rdset[1], wrset[1], exset[1];
62 #endif
63 struct timeval tv = { 0, 0 };
64 int val = -1;
66 FD_ZERO (rdset);
67 FD_ZERO (wrset);
68 FD_ZERO (exset);
69 for (unsigned i = 0; i < nfds; i++)
71 int fd = fds[i].fd;
72 if (val < fd)
73 val = fd;
75 /* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or
76 * bigger or equal than FD_SETSIZE. That is one of the reasons why VLC
77 * uses poll() rather than select(). Most POSIX systems implement
78 * fd_set has a bit field with no sanity checks. This is especially bad
79 * on systems (such as BSD) that have no process open files limit by
80 * default, such that it is quite feasible to get fd >= FD_SETSIZE.
81 * The next instructions will result in a buffer overflow if run on
82 * a POSIX system, and the later FD_ISSET would perform an undefined
83 * memory read.
85 * With Winsock, fd_set is a table of integers. This is awfully slow.
86 * However, FD_SET and FD_ISSET silently and safely discard excess
87 * entries. Here, overflow cannot happen anyway: fd_set of adequate
88 * size are allocated.
89 * Note that Vista has a much nicer WSAPoll(), but Mingw does not
90 * support it yet.
92 #ifndef _WIN32
93 if ((unsigned)fd >= FD_SETSIZE)
95 errno = EINVAL;
96 return -1;
98 #endif
99 if (fds[i].events & POLLIN)
100 FD_SET (fd, rdset);
101 if (fds[i].events & POLLOUT)
102 FD_SET (fd, wrset);
103 if (fds[i].events & POLLPRI)
104 FD_SET (fd, exset);
107 if (timeout >= 0)
109 div_t d = div (timeout, 1000);
110 tv.tv_sec = d.quot;
111 tv.tv_usec = d.rem * 1000;
114 val = select (val + 1, rdset, wrset, exset,
115 (timeout >= 0) ? &tv : NULL);
116 if (val == -1)
117 return -1;
119 for (unsigned i = 0; i < nfds; i++)
121 int fd = fds[i].fd;
122 fds[i].revents = (FD_ISSET (fd, rdset) ? POLLIN : 0)
123 | (FD_ISSET (fd, wrset) ? POLLOUT : 0)
124 | (FD_ISSET (fd, exset) ? POLLPRI : 0);
126 #ifdef _WIN32
127 free (exset);
128 free (wrset);
129 free (rdset);
130 #endif
131 return val;