dlls/ws2_32/socket.c
/* allocate a poll array for the corresponding fd sets */
allocate a poll array for the corresponding fd sets
allocate a poll array for the corresponding fd sets
static struct pollfd *fd_sets_to_poll( const WS_fd_set *readfds, const WS_fd_set *writefds,
                                       const WS_fd_set *exceptfds, int *count_ptr )
{
    unsigned int i, j = 0, count = 0;
    struct pollfd *fds;
    if (readfds) count += readfds->fd_count;
    if (writefds) count += writefds->fd_count;
    if (exceptfds) count += exceptfds->fd_count;
    *count_ptr = count;
    if (!count)
    {
        SetLastError(WSAEINVAL);
        return NULL;
    }
    if (!(fds = HeapAlloc( GetProcessHeap(), 0, count * sizeof(fds[0]))))
    {
        SetLastError( ERROR_NOT_ENOUGH_MEMORY );
        return NULL;
    }
    if (readfds)
        for (i = 0; i < readfds->fd_count; i++, j++)
        {
            fds[j].fd = get_sock_fd( readfds->fd_array[i], FILE_READ_DATA, NULL );
            if (fds[j].fd == -1) goto failed;
            fds[j].events = POLLIN;
            fds[j].revents = 0;
        }
    if (writefds)
        for (i = 0; i < writefds->fd_count; i++, j++)
        {
            fds[j].fd = get_sock_fd( writefds->fd_array[i], FILE_WRITE_DATA, NULL );
            if (fds[j].fd == -1) goto failed;
            fds[j].events = POLLOUT;
            fds[j].revents = 0;
        }
    if (exceptfds)
        for (i = 0; i < exceptfds->fd_count; i++, j++)
        {
            fds[j].fd = get_sock_fd( exceptfds->fd_array[i], 0, NULL );
            if (fds[j].fd == -1) goto failed;
            fds[j].events = POLLHUP;
            fds[j].revents = 0;
        }
    return fds;
failed:
    count = j;
    j = 0;
    if (readfds)
        for (i = 0; i < readfds->fd_count && j < count; i++, j++)
            release_sock_fd( readfds->fd_array[i], fds[j].fd );
    if (writefds)
        for (i = 0; i < writefds->fd_count && j < count; i++, j++)
            release_sock_fd( writefds->fd_array[i], fds[j].fd );
    if (exceptfds)
        for (i = 0; i < exceptfds->fd_count && j < count; i++, j++)
            release_sock_fd( exceptfds->fd_array[i], fds[j].fd );
    HeapFree( GetProcessHeap(), 0, fds );
    return NULL;
}
static struct pollfd * fd_sets_to_poll ( const WS_fd_set * readfds , const WS_fd_set * writefds , const WS_fd_set * exceptfds , int * count_ptr ) { unsigned int i , j = 0 , count = 0 ; struct pollfd * fds ; if ( readfds ) count += readfds -> fd_count ; if ( writefds ) count += writefds -> fd_count ; if ( exceptfds ) count += exceptfds -> fd_count ; * count_ptr = count ; if ( ! count ) { SetLastError ( WSAEINVAL ) ; return NULL ; } if ( ! ( fds = HeapAlloc ( GetProcessHeap ( ) , 0 , count * sizeof ( fds [ 0 ] ) ) ) ) { SetLastError ( ERROR_NOT_ENOUGH_MEMORY ) ; return NULL ; } if ( readfds ) for ( i = 0 ; i < readfds -> fd_count ; i ++ , j ++ ) { fds [ j ] . fd = get_sock_fd ( readfds -> fd_array [ i ] , FILE_READ_DATA , NULL ) ; if ( fds [ j ] . fd == -1 ) goto failed ; fds [ j ] . events = POLLIN ; fds [ j ] . revents = 0 ; } if ( writefds ) for ( i = 0 ; i < writefds -> fd_count ; i ++ , j ++ ) { fds [ j ] . fd = get_sock_fd ( writefds -> fd_array [ i ] , FILE_WRITE_DATA , NULL ) ; if ( fds [ j ] . fd == -1 ) goto failed ; fds [ j ] . events = POLLOUT ; fds [ j ] . revents = 0 ; } if ( exceptfds ) for ( i = 0 ; i < exceptfds -> fd_count ; i ++ , j ++ ) { fds [ j ] . fd = get_sock_fd ( exceptfds -> fd_array [ i ] , 0 , NULL ) ; if ( fds [ j ] . fd == -1 ) goto failed ; fds [ j ] . events = POLLHUP ; fds [ j ] . revents = 0 ; } return fds ; failed : count = j ; j = 0 ; if ( readfds ) for ( i = 0 ; i < readfds -> fd_count && j < count ; i ++ , j ++ ) release_sock_fd ( readfds -> fd_array [ i ] , fds [ j ] . fd ) ; if ( writefds ) for ( i = 0 ; i < writefds -> fd_count && j < count ; i ++ , j ++ ) release_sock_fd ( writefds -> fd_array [ i ] , fds [ j ] . fd ) ; if ( exceptfds ) for ( i = 0 ; i < exceptfds -> fd_count && j < count ; i ++ , j ++ ) release_sock_fd ( exceptfds -> fd_array [ i ] , fds [ j ] . fd ) ; HeapFree ( GetProcessHeap ( ) , 0 , fds ) ; return NULL ; }
allocate a poll array for the corresponding fd sets
allocate a poll array for the corresponding fd sets