X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fio%2Fpoll.c;h=17f9384587d92e39b37e199b0f05159bb899d112;hp=95b6103287d6335b0b84e4475a7b8469a2d33b54;hb=HEAD;hpb=580457bd3e4af60d5367412589d2aa1bb0289eed diff --git a/ccan/io/poll.c b/ccan/io/poll.c index 95b61032..634f83d2 100644 --- a/ccan/io/poll.c +++ b/ccan/io/poll.c @@ -91,6 +91,10 @@ static void del_fd(struct fd *fd) pollfds = tal_free(pollfds); fds = NULL; max_fds = 0; + if (num_always == 0) { + always = tal_free(always); + max_always = 0; + } } num_fds--; fd->backend_info = -1; @@ -117,7 +121,9 @@ bool add_listener(struct io_listener *l) static int find_always(const struct io_plan *plan) { - for (size_t i = 0; i < num_always; i++) + size_t i = 0; + + for (i = 0; i < num_always; i++) if (always[i] == plan) return i; return -1; @@ -137,6 +143,12 @@ static void remove_from_always(const struct io_plan *plan) if (pos != num_always-1) always[pos] = always[num_always-1]; num_always--; + + /* Only free if no fds left either. */ + if (num_always == 0 && max_fds == 0) { + always = tal_free(always); + max_always = 0; + } } bool backend_new_always(struct io_plan *plan) @@ -277,8 +289,10 @@ static bool *exclusive(struct io_plan *plan) /* For simplicity, we do one always at a time */ static bool handle_always(void) { + int i; + /* Backwards is simple easier to remove entries */ - for (int i = num_always - 1; i >= 0; i--) { + for (i = num_always - 1; i >= 0; i--) { struct io_plan *plan = always[i]; if (num_exclusive && !*exclusive(plan)) @@ -313,10 +327,12 @@ bool backend_set_exclusive(struct io_plan *plan, bool excl) * else that we manipulate events. */ static void exclude_pollfds(void) { + size_t i; + if (num_exclusive == 0) return; - for (size_t i = 0; i < num_fds; i++) { + for (i = 0; i < num_fds; i++) { struct pollfd *pfd = &pollfds[fds[i]->backend_info]; if (!fds[i]->exclusive[IO_IN]) @@ -333,10 +349,12 @@ static void exclude_pollfds(void) static void restore_pollfds(void) { + size_t i; + if (num_exclusive == 0) return; - for (size_t i = 0; i < num_fds; i++) { + for (i = 0; i < num_fds; i++) { struct pollfd *pfd = &pollfds[fds[i]->backend_info]; if (fds[i]->listener) { @@ -446,3 +464,15 @@ void *io_loop(struct timers *timers, struct timer **expired) return ret; } + +const void *io_have_fd(int fd, bool *listener) +{ + for (size_t i = 0; i < num_fds; i++) { + if (fds[i]->fd != fd) + continue; + if (listener) + *listener = fds[i]->listener; + return fds[i]; + } + return NULL; +}