--- libev/ev_select.c 2007/10/31 14:44:15 1.4 +++ libev/ev_select.c 2007/11/03 21:58:51 1.10 @@ -1,4 +1,6 @@ /* + * libev select fd activity backend + * * Copyright (c) 2007 Marc Alexander Lehmann * All rights reserved. * @@ -33,28 +35,31 @@ #include /* for unix systems */ -#include +#ifndef WIN32 +# include +#endif #include #include -static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo; -static int vec_max; - static void -select_modify (int fd, int oev, int nev) +select_modify (EV_P_ int fd, int oev, int nev) { int offs = fd >> 3; int mask = 1 << (fd & 7); if (vec_max < (fd >> 5) + 1) { - vec_max = (fd >> 5) + 1; + int new_max = (fd >> 5) + 1; - vec_ri = (unsigned char *)realloc (vec_ri, vec_max * 4); - vec_ro = (unsigned char *)realloc (vec_ro, vec_max * 4); /* could free/malloc */ - vec_wi = (unsigned char *)realloc (vec_wi, vec_max * 4); - vec_wo = (unsigned char *)realloc (vec_wo, vec_max * 4); /* could free/malloc */ + vec_ri = (unsigned char *)realloc (vec_ri, new_max * 4); + vec_ro = (unsigned char *)realloc (vec_ro, new_max * 4); /* could free/malloc */ + vec_wi = (unsigned char *)realloc (vec_wi, new_max * 4); + vec_wo = (unsigned char *)realloc (vec_wo, new_max * 4); /* could free/malloc */ + + for (; vec_max < new_max; ++vec_max) + ((uint32_t *)vec_ri)[vec_max] = + ((uint32_t *)vec_wi)[vec_max] = 0; } vec_ri [offs] |= mask; @@ -66,7 +71,8 @@ vec_wi [offs] &= ~mask; } -static void select_poll (ev_tstamp timeout) +static void +select_poll (EV_P_ ev_tstamp timeout) { struct timeval tv; int res; @@ -106,14 +112,22 @@ } } } + else if (res < 0) + { + if (errno == EBADF) + fd_ebadf (); + else if (errno == ENOMEM) + fd_enomem (); + } } -void select_init (int flags) +static int +select_init (EV_P_ int flags) { - ev_method = EVMETHOD_SELECT; method_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */ method_modify = select_modify; method_poll = select_poll; -} + return EVMETHOD_SELECT; +}