changeset 8634:86c28d14ddeb HEAD

Added io_loop_set_max_fd_count() to specify how many fds we expect to use. It's used currently only for figuring out how much space should be allocated initially to fds.
author Timo Sirainen <tss@iki.fi>
date Thu, 15 Jan 2009 16:20:09 -0500
parents 7e4c1d8b2a1a
children c8f619c13d07
files src/lib/ioloop-epoll.c src/lib/ioloop-internal.h src/lib/ioloop-kqueue.c src/lib/ioloop-poll.c src/lib/ioloop.c src/lib/ioloop.h
diffstat 6 files changed, 30 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/ioloop-epoll.c	Thu Jan 15 15:54:39 2009 -0500
+++ b/src/lib/ioloop-epoll.c	Thu Jan 15 16:20:09 2009 -0500
@@ -25,16 +25,16 @@
 	ARRAY_DEFINE(events, struct epoll_event);
 };
 
-void io_loop_handler_init(struct ioloop *ioloop)
+void io_loop_handler_init(struct ioloop *ioloop, unsigned int initial_fd_count)
 {
 	struct ioloop_handler_context *ctx;
 
 	ioloop->handler_context = ctx = i_new(struct ioloop_handler_context, 1);
 
-	i_array_init(&ctx->events, IOLOOP_INITIAL_FD_COUNT);
-	i_array_init(&ctx->fd_index, IOLOOP_INITIAL_FD_COUNT);
+	i_array_init(&ctx->events, initial_fd_count);
+	i_array_init(&ctx->fd_index, initial_fd_count);
 
-	ctx->epfd = epoll_create(IOLOOP_INITIAL_FD_COUNT);
+	ctx->epfd = epoll_create(initial_fd_count);
 	if (ctx->epfd < 0)
 		i_fatal("epoll_create(): %m");
 	fd_close_on_exec(ctx->epfd, TRUE);
--- a/src/lib/ioloop-internal.h	Thu Jan 15 15:54:39 2009 -0500
+++ b/src/lib/ioloop-internal.h	Thu Jan 15 16:20:09 2009 -0500
@@ -17,6 +17,7 @@
 
         struct ioloop_handler_context *handler_context;
         struct ioloop_notify_handler_context *notify_handler_context;
+	unsigned int max_fd_count;
 
 	unsigned int running:1;
 };
@@ -60,7 +61,7 @@
 void io_loop_handle_add(struct io_file *io);
 void io_loop_handle_remove(struct io_file *io, bool closed);
 
-void io_loop_handler_init(struct ioloop *ioloop);
+void io_loop_handler_init(struct ioloop *ioloop, unsigned int initial_fd_count);
 void io_loop_handler_deinit(struct ioloop *ioloop);
 
 void io_loop_notify_remove(struct io *io);
--- a/src/lib/ioloop-kqueue.c	Thu Jan 15 15:54:39 2009 -0500
+++ b/src/lib/ioloop-kqueue.c	Thu Jan 15 16:20:09 2009 -0500
@@ -35,7 +35,7 @@
 	ARRAY_DEFINE(events, struct kevent);
 };
 
-void io_loop_handler_init(struct ioloop *ioloop)
+void io_loop_handler_init(struct ioloop *ioloop, unsigned int initial_fd_count)
 {
 	struct ioloop_handler_context *ctx;
 
@@ -45,7 +45,7 @@
 		i_fatal("kqueue() in io_loop_handler_init() failed: %m");
 	fd_close_on_exec(ctx->kq, TRUE);
 
-	i_array_init(&ctx->events, IOLOOP_INITIAL_FD_COUNT);
+	i_array_init(&ctx->events, initial_fd_count);
 }
 
 void io_loop_handler_deinit(struct ioloop *ioloop)
--- a/src/lib/ioloop-poll.c	Thu Jan 15 15:54:39 2009 -0500
+++ b/src/lib/ioloop-poll.c	Thu Jan 15 16:20:09 2009 -0500
@@ -18,15 +18,15 @@
 	int *fd_index;
 };
 
-void io_loop_handler_init(struct ioloop *ioloop)
+void io_loop_handler_init(struct ioloop *ioloop, unsigned int initial_fd_count)
 {
 	struct ioloop_handler_context *ctx;
 
 	ioloop->handler_context = ctx = i_new(struct ioloop_handler_context, 1);
-	ctx->fds_count = IOLOOP_INITIAL_FD_COUNT;
+	ctx->fds_count = initial_fd_count;
 	ctx->fds = i_new(struct pollfd, ctx->fds_count);
 
-	ctx->idx_count = IOLOOP_INITIAL_FD_COUNT;
+	ctx->idx_count = initial_fd_count;
 	ctx->fd_index = i_new(int, ctx->idx_count);
         memset(ctx->fd_index, 0xff, sizeof(int) * ctx->idx_count);
 }
--- a/src/lib/ioloop.c	Thu Jan 15 15:54:39 2009 -0500
+++ b/src/lib/ioloop.c	Thu Jan 15 16:20:09 2009 -0500
@@ -19,6 +19,16 @@
 
 struct ioloop *current_ioloop = NULL;
 
+static void io_loop_initialize_handler(struct ioloop *ioloop)
+{
+	unsigned int initial_fd_count;
+
+	initial_fd_count = ioloop->max_fd_count > 0 &&
+		ioloop->max_fd_count < IOLOOP_INITIAL_FD_COUNT ?
+		ioloop->max_fd_count : IOLOOP_INITIAL_FD_COUNT;
+	io_loop_handler_init(ioloop, initial_fd_count);
+}
+
 #undef io_add
 struct io *io_add(int fd, enum io_condition condition,
 		  io_callback_t *callback, void *context)
@@ -38,7 +48,7 @@
 	io->fd = fd;
 
 	if (io->io.ioloop->handler_context == NULL)
-		io_loop_handler_init(io->io.ioloop);
+		io_loop_initialize_handler(io->io.ioloop);
 	io_loop_handle_add(io);
 
 	if (io->io.ioloop->io_files != NULL) {
@@ -321,7 +331,7 @@
 void io_loop_run(struct ioloop *ioloop)
 {
 	if (ioloop->handler_context == NULL)
-		io_loop_handler_init(ioloop);
+		io_loop_initialize_handler(ioloop);
 
 	ioloop->running = TRUE;
 	while (ioloop->running)
@@ -338,6 +348,11 @@
         ioloop->running = TRUE;
 }
 
+void io_loop_set_max_fd_count(struct ioloop *ioloop, unsigned int max_fds)
+{
+	ioloop->max_fd_count = max_fds;
+}
+
 bool io_loop_is_running(struct ioloop *ioloop)
 {
         return ioloop->running;
--- a/src/lib/ioloop.h	Thu Jan 15 15:54:39 2009 -0500
+++ b/src/lib/ioloop.h	Thu Jan 15 16:20:09 2009 -0500
@@ -88,6 +88,8 @@
 void io_loop_handler_run(struct ioloop *ioloop);
 
 struct ioloop *io_loop_create(void);
+/* Specify the maximum number of fds we're expecting to use. */
+void io_loop_set_max_fd_count(struct ioloop *ioloop, unsigned int max_fds);
 /* Destroy I/O loop and set ioloop pointer to NULL. */
 void io_loop_destroy(struct ioloop **ioloop);