*/
#define _GNU_SOURCE
+#define _LGPL_SOURCE
#include <assert.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <common/defaults.h>
#include <common/error.h>
+#include <common/macros.h>
+#include <common/utils.h>
#include "poll.h"
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;
/* This *must* be freed by using lttng_poll_free() */
wait->events = zmalloc(size * sizeof(struct pollfd));
if (wait->events == NULL) {
- perror("zmalloc struct pollfd");
+ PERROR("zmalloc struct pollfd");
goto error;
}
current->events = zmalloc(size * sizeof(struct pollfd));
if (current->events == NULL) {
- perror("zmalloc struct current pollfd");
+ PERROR("zmalloc struct current pollfd");
goto error;
}
/* Check for a needed resize of the array. */
if (current->nb_fd > current->alloc_size) {
/* Expand it by a power of two of the current size. */
- new_size = current->alloc_size << 1UL;
+ new_size = max_t(int,
+ 1U << utils_get_count_order_u32(current->nb_fd),
+ current->alloc_size << 1UL);
ret = resize_poll_event(current, new_size);
if (ret < 0) {
goto error;
/* Ease our life a bit. */
current = &events->current;
- /* Safety check on size */
- if (new_size > poll_max_size) {
- new_size = poll_max_size;
- }
-
/* Check if we need to shrink it down. */
if ((current->nb_fd << 1UL) <= current->alloc_size &&
current->nb_fd >= current->init_size) {
* Shrink if nb_fd multiplied by two is <= than the actual size and we
* are above the initial size.
*/
- new_size = current->alloc_size >> 1UL;
+ new_size = max_t(int,
+ utils_get_count_order_u32(current->nb_fd) >> 1U,
+ current->alloc_size >> 1U);
ret = resize_poll_event(current, new_size);
if (ret < 0) {
goto error;
ret = poll(events->wait.events, events->wait.nb_fd, timeout);
if (ret < 0) {
/* At this point, every error is fatal */
- perror("poll wait");
+ PERROR("poll wait");
goto error;
}
ret = getrlimit(RLIMIT_NOFILE, &lim);
if (ret < 0) {
- perror("getrlimit poll RLIMIT_NOFILE");
+ PERROR("getrlimit poll RLIMIT_NOFILE");
return;
}