+ /*
+ * Resize if needed before waiting. We could either expand the array or
+ * shrink it down. It's important to note that after this step, we are
+ * ensured that the events argument of the epoll_wait call will be large
+ * enough to hold every possible returned events.
+ */
+ if (events->nb_fd > events->alloc_size) {
+ /* Expand if the nb_fd is higher than the actual size. */
+ new_size = max_t(uint32_t,
+ 1U << utils_get_count_order_u32(events->nb_fd),
+ events->alloc_size << 1UL);
+ } else if ((events->nb_fd << 1UL) <= events->alloc_size &&
+ events->nb_fd >= events->init_size) {
+ /* Shrink if nb_fd multiplied by two is <= than the actual size. */
+ new_size = max_t(uint32_t,
+ utils_get_count_order_u32(events->nb_fd) >> 1U,
+ events->alloc_size >> 1U);
+ } else {
+ /* Indicate that we don't want to resize. */
+ new_size = 0;
+ }
+
+ if (new_size) {
+ ret = resize_poll_event(events, new_size);
+ if (ret < 0) {
+ /* ENOMEM problem at this point. */
+ goto error;
+ }
+ }
+