+/*
+ * Resize the epoll events structure of the new size.
+ *
+ * Return 0 on success or else -1 with the current events pointer untouched.
+ */
+static int resize_poll_event(struct compat_poll_event_array *array,
+ uint32_t new_size)
+{
+ struct pollfd *ptr;
+
+ assert(array);
+
+ /* Refuse to resize the array more than the max size. */
+ if (new_size > poll_max_size) {
+ goto error;
+ }
+
+ ptr = realloc(array->events, new_size * sizeof(*ptr));
+ if (ptr == NULL) {
+ PERROR("realloc epoll add");
+ goto error;
+ }
+ if (new_size > array->alloc_size) {
+ /* Zero newly allocated memory */
+ memset(ptr + array->alloc_size, 0,
+ (new_size - array->alloc_size) * sizeof(*ptr));
+ }
+ array->events = ptr;
+ array->alloc_size = new_size;
+
+ return 0;
+
+error:
+ return -1;
+}
+
+/*
+ * Update events with the current events object.
+ */
+static int update_current_events(struct lttng_poll_event *events)
+{
+ int ret;
+ struct compat_poll_event_array *current, *wait;
+
+ assert(events);
+
+ current = &events->current;
+ wait = &events->wait;
+
+ wait->nb_fd = current->nb_fd;
+ if (current->alloc_size != wait->alloc_size) {
+ ret = resize_poll_event(wait, current->alloc_size);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+ memcpy(wait->events, current->events,
+ current->nb_fd * sizeof(*current->events));
+
+ /* Update is done. */
+ events->need_update = 0;
+
+ return 0;
+
+error:
+ return -1;
+}
+